#ECS - How to create a system after the bakind is done

1 messages · Page 1 of 1 (latest)

tacit latch
#

What should be the [CreateAfter(typeof(XXXX))]
To be able to retrieve a singleton created from a baker in the OnCreate() method ?

Or any good practices on that is welcome 🙂

For context :
I have a baker creating an entity with a ComponentData on it at runtime ina subscene

In a system I want to initialise some state using that singleton.

Problem :
The system is being created before the bakin is done & can't find the singleton because not yet created

little epoch
#

what ive seen people do is basically do a RequireComponent<ComponentSingleton> on OnCreate and then doing their thing on OnUpdate

tacit latch
#

hummm weird that there are no other ways ...

bitter sapphire
#

OnCreate is ran at the creation of a System, this usually happens on World startup.
Subscenes are loaded async. Meaning, you actually don't know which frame they are done loading.
Systems should therefore either:

  1. Be written to only run when a ceartin singleton is ready (for this I recommend looking into OnStartRunning, which runs once, the first time a query matches)
  2. Be written to query for the existence of that singleton use TryGetSingleton for this.
  3. Or make sure to only run entire systemgroups when all scenes are loaded (I think this is Tertle's approach, if I remember right o.O)
#

Example of 1.

partial struct MySystem : ISystem, ISystemStartStop {
  public void OnCreate(ref SystemState state) {
    state.RequireForUpdate<ComponentSingleton>();
  }
  public void OnStartRunning(ref SystemState state) {
    var singletonComponent = SystemAPI.GetSingleton<SingletonComponent>();
    // run once logic here
  }
  public void OnStopRunning(ref SystemState state){}
}

Example of 2.

partial struct MySystem : ISystem {
  public void OnUpdate(ref SystemState state) {
    if (!SystemAPI.TryGetSingleton<SingletonComponent>(out var singletonComponent))
      return;
    state.Enabled = false;
    // run once logic here
  }
}
tacit latch
#

@bitter sapphire thanks for the info ! VERY helpfull

#

My structure make potentially the option 3 possible what's Tertle ?

#

Do you have any example of it ?

hoary basin
#

That's me 😄

#

I have a system that stops the world from updating until subscenes you've marked as required have loaded

#

I recently updated it use a more generic, pause system, so you can have other systems pause the game on loading (my use case is I didn't want to tick world until first pass of navmesh generation was run) but pretty much all it's doing is

ref var simulationSystemGroup = ref state.WorldUnmanaged.GetExistingSystemState<SimulationSystemGroup>();
simulationSystemGroup.Enabled = !pause;

ref var presentationSystemGroup = ref state.WorldUnmanaged.GetExistingSystemState<PresentationSystemGroup>();
presentationSystemGroup.Enabled = !pause;```
#

Disabling the simulation and presentation system groups until your requested subscenes have loaded. This means it's guaranteed OnStartRunning/OnUpdate won't be called until your subscenes have finished loading.

#

I bake all my data and configuration and I found it annoying having to have RequireForUpdate<MyConfigSingleton>() in 90% of all my systems (+ small overhead from having to do the check every frame)

#

And this is what I came up with. It's been very solid. I've used it for quite a while now and can even deal with opening/closing subscenes during runtime (it will re-pause while it swaps)

#

Note: You need to keep ticking Initialization otherwise subscenes won't ever load. You could get around this by manually ticking the SubScene systems but I personally like having initialization unpaused at all times because I can use this. I am considering not pausing Presentation because I think it makes sense to be able to interact with UI etc.

tacit latch
#

How do you actually define what scene are required in your context ?

hoary basin
#

i have a component you put it on the subscene

#

it lets you defnie load mode

#

worlds it loads into

#

(server/client/etc)

tacit latch
#

wouldn't that be possible to create a [NeedBaking] attribute ? 😄

#

a bit like the [burstcompile] one

#

that would be neat

hoary basin