Hi.
I have this issue that when i load a scene with a Subscene, usually it can take several frame before my Subscene is ready.
This is causing various issues for me. One example:
I have a singleton component GameStateComponent, in which i want to initialize a round variable.
public struct GameStateComponent : IComponentData
{
public int Round;
}
I have a monobehavoir which currently tracks the rounds between scene loads. My first approach has been to create the following:
public void Start()
{
StartCoroutine(SetGamestateCoroutine());
}
private IEnumerator SetGamestateCoroutine()
{
int count = 0;
do
{
count++;
yield return null;
} while (!SetGameRound());
Debug.Log($"Set game round after {count} frames");
}
private bool SetGameRound()
{
var _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var _entityQuery = _entityManager.CreateEntityQuery(typeof(GameStateComponent));
if (_entityQuery.TryGetSingleton<GameStateComponent>(out var gameStateComponent))
{
Debug.LogWarning("INITIALIZED GAME STATE COMPONENT");
gameStateComponent.Round = _gameStateTracker.Round;
return true;
}
return false;
}
Now i notice sometimes this coroutine has to run 13 frames before we've initialized the component.
This is just one example of my issue, I also have other components with a similar issue. Ie a PlayerTag monobehaviour which should follow the player, so i can use cinemachine.
I also tried a simple approach checking the subscenes.IsLoaded property. But isLoaded is not the same as ready.
How do you people usually solve these issues with Monobehavior scripts which depend on the subscenes being ready and loaded?