How does the EntityManager works, and how does it handles recently created Entities?
During my loading procedure, I am going through many entities being created through all the same EntityManager, except it is being accessed through many different means.
To load a save file, I will select a world to load the save into:
Save.LoadFile("Quicksave", World.DefaultGameObjectInjectionWorld);
public void LoadFile(string filename, World world) {
StaticFlags.Save_PostLoadQueue.Clear();
EntityManager em = world.EntityManager;
RuntimeEntityToGameObject[] runtimeEntities = em.CreateEntityQuery(typeof(RuntimeEntityToGameObject)).ToComponentArray<RuntimeEntityToGameObject>();
runtimeEntities.ForEach(T => Utils.ContextDestroy(T.GameObject));
world.EntityManager.DestroyEntity(world.EntityManager.UniversalQuery);
foreach (ComponentSystemBase System in world.Systems) {
if (typeof(ICleanUpSystem).IsAssignableFrom(System.GetType())) {
try {
System.Update();
} catch (Exception e) {
Debug.LogError(e);
}
}
}
em = world.EntityManager;
EntityRecoverer recoverer = new EntityRecoverer(world);
(...)
Then deep into the LoadFile method, a whole bunch of Actions and Functions are called, which did not have the reference to the EntityManager being used, so they would call World.DefaultGameObjectInjectionWorld.EntityManager, which on this specific case (and the only one on my project currently, as I only ever load files onto World.DefaultGameObjectInjectionWorld), the very same EntityManager. Yet Entities created on the EntityManager reference created in LoadFile do not exist on the supposedly the very same EntityManager accessed through World.DefaultGameObjectInjectionWorld.EntityManager. Passing the reference to the Actions and Functions has been resolving the issue. Why exactly it happens? I ask because, well, it was working just fine until very recently until it suddenly broke due to some change I can’t pinpoint.