Giter Site home page Giter Site logo

Comments (7)

tertle avatar tertle commented on September 6, 2024 1

It'll be easier for me to walk through an example if you jumped on discord but let me try answer some stuff.

So yeah, you need to register all of the possibly states via StateAPI.Register and this needs to be done in OnCreate so they exist before your GridCellStateSystem OnStartRunning triggers.

If you're using StateModel which is adding/removing state components - you shouldn't be baking these components on your entity. Only the GridCellVisualState and GridCellVisualState Previous components should be added.

As for changing states, one of the ideas of this state system is that you can write changes to the state component without having to use ECB - you should just be able to write directly to GridCellVisualState and when the system runs it will fix up your states, otherwise you risk it being delayed.

from com.bovinelabs.core.

tertle avatar tertle commented on September 6, 2024 1

StateAPI.Register adds a component StateInstance to the system.
If you call it more than once per system it'll just override the previous ones meaning only the last will be actually registered.
I've just added an error now if you try to do this.

The idea of this is that each system has it's own state component and the system won't run unless the state component exists.

If you need to have 1 system register multiple states, you need to create a new entity with StateInstance instead of assigning it to the system.

from com.bovinelabs.core.

tertle avatar tertle commented on September 6, 2024 1

I've never had a single system responsible for multiple states. I always map 1 system to 1 state. But you'd just do something like this

var stateTypeIndex = TypeManager.GetTypeIndex<GridCellVisualState>();

var e0 = state.EntityManager.CreateEntity(typeof(StateInstance));
e.SetComponentData(e0, new StateInstance
    {
        State = stateTypeIndex,
        StateKey = (byte)GridCellVisualStates.Arena,
        StateInstanceComponent = TypeManager.GetTypeIndex<ArenaGridCellVisualState>(),
    });

var e1 = state.EntityManager.CreateEntity(typeof(StateInstance));
e.SetComponentData(e1, new StateInstance
{
    State = stateTypeIndex,
    StateKey = (byte)GridCellVisualStates.Workshop,
    StateInstanceComponent = TypeManager.GetTypeIndex<WorkshopGridCellVisualState>(),
});

from com.bovinelabs.core.

Darth-Carrotpie avatar Darth-Carrotpie commented on September 6, 2024

Thank you for response, cleared up a few things. Hadn't time to sit on this problem until now.

So after a bit of testing around, it seems to me that I can register only a ginel state component. I am actually trying to register 5, instead only the last one gets applied. My example OnCreate:

    public void OnCreate(ref SystemState state)
    {
        //Registering injects a query filter to the system State. This can act similar to RequireForUpdate if argument is true
        StateAPI.Register<GridCellVisualState, ArenaGridCellVisualState>(ref state, (byte)GridCellVisualStates.Arena, false);
        StateAPI.Register<GridCellVisualState, WorkshopGridCellVisualState>(ref state, (byte)GridCellVisualStates.Workshop, false);
        StateAPI.Register<GridCellVisualState, BarracksGridCellVisualState>(ref state, (byte)GridCellVisualStates.Barracks, false);
        StateAPI.Register<GridCellVisualState, KitchenGridCellVisualState>(ref state, (byte)GridCellVisualStates.Kitchen, false);
        StateAPI.Register<GridCellVisualState, ClearGridCellVisualState>(ref state, (byte)GridCellVisualStates.Clear, false);
    }

Then I am applying the state within an Update like so:

            byte newState = BuildingToCellState(building.buildingType);
            ecb.SetComponent(gridCellEntity, new GridCellVisualState { Value = newState });

If I switch around the order at which I run StateAPI.Register, I get a different result, i.e. if I make StateAPI.Register<GridCellVisualState, KitchenGridCellVisualState> as the last one from the all 5, then as a consequence I get only KitchenGridCellVisualState component applied by State Manager onto the gridCellEntity.

from com.bovinelabs.core.

Darth-Carrotpie avatar Darth-Carrotpie commented on September 6, 2024

I just noticed I'm getting a warning (had them turned off heh):

State 252 not setup
UnityEngine.Debug:LogWarning (object)
BovineLabs.Core.States.StateModel/StateJob:Execute (Unity.Entities.ArchetypeChunk&,int,bool,Unity.Burst.Intrinsics.v128&) (at ./Library/PackageCache/[email protected]/BovineLabs.Core/States/StateModel.cs:118)

Probably actually indicates that setup was wrong. No clue how though..

from com.bovinelabs.core.

Darth-Carrotpie avatar Darth-Carrotpie commented on September 6, 2024

Could you give a simple example of how to handle multiple states within a system? In current example there's just a single state for camera, but for a state machine it kind of makes sense to have at least a few...

from com.bovinelabs.core.

Darth-Carrotpie avatar Darth-Carrotpie commented on September 6, 2024

Thank you for quick and informative replies! I will try out the approach as soon as I get the chance.
Prolly my approach being different and not-as-intended suggests that I might be not understanding a best practice of using states in this case, but oh well, not the first 🥲
(but if it's fine, I can paste working code after testing, to add to the examples)

Moving from event-based OOP paradigm via which we made our previous game to DOTS is kind of mind boggling by it's own 🤯
But I feel like "this is the way" 🚀

from com.bovinelabs.core.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.