#Idiomatic way to wait for component availability

1 messages · Page 1 of 1 (latest)

dire bear
#

I often have systems, that wait for a singleton, that is created during baking. I want to cache that singleton, so I do not have to check in every update if it is available yet.

I have it as a private member in the ISystem struct, and I have state.RequireForUpdate<ThatComponent>(); in my OnCreate Method.

I thought that should be enough to delay OnUpdate until it is available.

Often that does not work, so I will check in the OnUpdate with a call to System.Api.TryGetSingleton<ThatComponent>(out _cachedComponent)) but that seems uncessary resource heavy.

Is there a correct way to do that?

feral flare
#

RequireForUpdate should be enough
if any of my systems call onupdate ignoring requires it will crash instantly

ornate rover
#

Require for update is fine, just not though it doesn't use filters so it doesn't care about enabled state etc

#

If it exists regardless if it's disabled it'll run

summer anvil
#

If you do SystemAPI.GetSingleton<ThatComponent>() then state.RequireForUpdate<ThatComponent>() will always work. Do you have an example of a time it didn't? Because you might've done something wrong

ornate rover
#

System will run, get singleton will return nothing

summer anvil
ornate rover
#

But yes you just need to be aware of how filters work

dire bear
#

But the get Singleton would be in the update method, so no caching, right? Is that performant?

elfin reef
#

Also Unity ECS is built around value types so caching doesn't help you in general.