Search Unity

Question Upgraded large project to 1.0, running into issues using GetEntity in the Baker

Discussion in 'Entity Component System' started by slims, Jan 16, 2023.

  1. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I converted hundreds of prefabs previously loaded using the old conversion workflowin .51 to all have bakers and what not, that part went fine. But now when I load them from disk and try to get the prefab entities from them in my baker responsible for loading prefabs, I'm getting weird results.

    My items are all described by Scriptables, each item's scriptable has a reference to the game object prefab that will act as the entity prefab. The baker responsible for loading prefabs lives in the subscene and loads the scriptables from disk, iterates over them, grabs the prefab reference on the scriptable, calls GetEntity on it, but the GetEntity returns some garbage that looks like a system handle. Here's the code:

    Code (CSharp):
    1. for (var index = 0; index < managedItemConfigs.Count; index++)
    2. {
    3.   var item = managedItemConfigs[index];
    4.   var itemIdentifier = index + 1;
    5.   try
    6.   {
    7.     // Create the master entity prefab that the game will use from here on out to create this entity
    8.     var itemEntityPrefab = baker.GetEntity(item.EntityPrefab);
    9.     itemPrefabs[itemIdentifier] = itemEntityPrefab;
    10.  
    11.     if (!itemEntityPrefab.TryGetComponent(out AsteroItem asteroItemComponent))
    12.       throw new UnityException($"Astero Item component not found for item {item.ManagedName}");
    13.   }
    14. }
    The exception gets thrown because the Entity that gets returned looks like a random system handle, so my AsteroItem component is not found. Here's a screenshot of the entity after GetEntity is called. The fact that in this case the Entity got named as the CometMoverSystem is just random, it's a different system everytime. If I inspect item.EntityPrefab it is the correct game object I'd expect. I'm guessing this is some kind of red herring but it's super confusing.
    help.png

    EDIT: that TryGetComponent call is just an extension method that uses the default World's EntityManager to do a HasComponent call then a GetComponent call. I'm realizing this may be an issue...looking into it.


    EDIT2: It seems like my issue may have something to do with the fact that I'm trying to store the entities in a NativeList on my component. Entities I don't store in a list appear to be working, maybe.

    This is the component data and authoring that I am trying to bake. The FFItemEntities array and ConnectorItemEntities array don't actually seem to store the data I put into them.
    Code (CSharp):
    1.   public class EntityPrefabContainerAuthoring : MonoBehaviour
    2.   {
    3.     public GameObject UnknownConnectorItemPrefab;
    4.   }
    5.  
    6.   public struct EntityPrefabContainer : IComponentData
    7.   {
    8.     public Entity UnknownConnectorItemPrefab;
    9.     public NativeList<Entity> FFItemEntities;
    10.     public NativeList<Entity> ConnectorItemEntities;
    11.   }
     
    Last edited: Jan 17, 2023
  2. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I figured it out. The problem was related to my second edit above.

    It appears you cannot store entities that you retrieve from the GetEntity method in the baker inside of Native collections on an IComponentData, they turn to garbage if you do this.

    I fixed this by simply using a DynamicBuffer instead, and using that in my Baker.
     
    Rupture13 likes this.
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Baking operate in a different world than runtime. So the entity you get from GetEntity in the baker does not have the same index and version as the final one in the runtime world. That is why Unity performs an entity remapping after baking.
    This entity remapping only works on IComponentData and IBufferElementData.
    Therefore you can't store entity reference in blob assets or native container when baking.

    see video 5 and 13 of my series for more details.
     
    lclemens likes this.
  4. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I actually watched your videos when googling around about this problem :D

    But yes, for future readers, I had two problems:

    1) No storing entities in blobs or native collections in a baker.
    2) No trying to manipulate the data on those entities you retrieved in a baker using GetEntity inside the baker itself. You need to do it in a regular system during actual runtime.
     
    lclemens likes this.
  5. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Ugh.... so many hidden restrictions!
     
  6. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    You can manipulate that data in baking systems(use

    [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    for your baking systems)
     
    Rupture13 likes this.