#EntityQuery not Querying entity with component I know exists

1 messages · Page 1 of 1 (latest)

drifting mirage
#

Im going to try and put this as simply as possible
I have an Entity, with Component PrefabHolder that I have placed in the Entities Subscene (<- is that of importance?)

Then, in OnCreate in a System, I have this code, which first does a standalone entity query to determine the amount of PrefabHolders there are, and then actually iterates over them (the one there should be) to get it's prefab. However the query finds 0 and therefore the foreach gets skipped too. I'm trying to figure out why the queries simply dont know about the PrefabHolder's existence. The two suspicions I have: Could it have anything to do with the prefabholder being in a SubScene? And, Could it have anything to do with the sytem being ran with [CreateAfter(typeof(BeginSimulationEntityCommandBufferSystem))]?
Any help is appreciated, I feel like I've run into a dead end.

//get prefab holder entity
Entity facePrefab;
facePrefab = Entity.Null;

EntityQuery prefabHolderQuery = new EntityQueryBuilder(Allocator.Temp).WithAll<PrefabHolder>().Build(ref state);
int phCount = prefabHolderQuery.CalculateEntityCount();
Debug.Log($"Found {phCount} prefab holders"); //to count how many prefab holders there are

foreach (RefRO<PrefabHolder> ph in SystemAPI.Query<RefRO<PrefabHolder>>()) //there should always be one of these
{
    Debug.Log("Found prefab holder"); //but this never gets called
    facePrefab = ph.ValueRO.facePrefab;
}
untold trout
#

Subscene loading is asynchronous (or at least, needs to happen in an update of one of the systems related to scene streaming). Checking for something in OnCreate is a no-go.

You basically need to design things to be able to handle “nothing’s here yet” in your code until the appropriate subscenes are loaded and data is available. Typically you store things in singletons to pass around some shared state like native collections with global data and maybe explicit readiness state.

drifting mirage
untold trout
#

Yeah, once the subscene is loaded (assuming it is indeed configured to)

drifting mirage
#

on that note if you dont mind me asking
is there a way i can use that entity the same way outisde of the subscene?
because the gameobject-entity conversion doesnt happen when its outside
sorry if that's a dumb question i just dont know if using the subscene is a neccessity

untold trout
#

Things that go into a subscene in the editor basically go into a mixing pot and you get a nice baked entity cake at the end.
You can’t easily replicate entity baking and entity references / remapping outside a subscene, so for the most part you should try and make things concerning entities happen from inside your ISystem/SystemBase. In a way, it’s easier to make things reach out from an entities world than reach in from classic Scene/MonoBehaviour land.

Entity IDs (composed of index and version) aren’t predictable, so referencing something from a MonoBehaviour isn’t possible, though entity prefabs through EntityPrefabReference are a thing which can be used (which have their own little oddities).

You can of course create an EntityQuery through EntityManager once you have a reference to the appropriate world and do the same lookup as you’re doing in your system. Also, most examples of ECS/MonoBehaviour interop default to using World.DefaultGameObjectInjectionWorld which works for the most part but gets ambiguous if you make custom worlds or use Netcode for Entities.