Search Unity

Question How do I have global state variables?

Discussion in 'Entity Component System' started by DacunaZuke, Jun 7, 2022.

  1. DacunaZuke

    DacunaZuke

    Joined:
    Apr 30, 2022
    Posts:
    10
    I used to use a ScriptableObject which had some global state variables, such as IsBossActive, for when a boss is active.

    I realize this isn't very ECS-friendly, so what's the alternative?

    I'm thinking of having a game state component which would hold that variable, and when a boss activates, just do an Entities.ForEach to change that variable for every entity that has it. Does this make sense?

    I also know of ISharedComponentData, but from what I can tell, you can't access those types of components in a job if they are altered outside of it (in a GameObject or similar).
     
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    There is Singleton Entities for such EntitiesWorld Global data
    Or BlobAssetRef for Info that considered static for runtime
     
    DacunaZuke likes this.
  3. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    287
    I would not over complicate things. If you have a single 'Object' the performance gain of ECS won't be noticeable. Just store the data in a scriptable object, a static field, a system field, a persistent NativeHashMap or anything that works for you.

    If you have 1000 bosses and you want to do something with all active bosses, than give these entities a IsActiveBossTag component and use it to filter in the system.
     
    DacunaZuke likes this.