Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Broken baker with entities 1.0 when using 2 string fields in a class Component

Discussion in 'Entity Component System' started by instriker_911, Nov 25, 2022.

  1. instriker_911

    instriker_911

    Joined:
    Mar 31, 2022
    Posts:
    27
    Use case

    I have :
    • A baker creating a Map
    • A baker creating an additionnal entity referencing the Map (PrimaryEntity)
    • This additionnal entity have string metadata to load the map

    Issue


    At first, when I was using a single string metadata in my IComponentData, everything as fine, in the system the reference to the Entity is working as expected:

    upload_2022-11-25_8-19-48.png

    BUT after I added a second string to a class IComponentData, at runtime, the entity referenced is completly messed up, is this exemple the entity is a System instance AND NOT the Cube entity:

    upload_2022-11-25_8-21-13.png

    (screen shots based on code below):


    How to reproduce

    • Create a new scene with an empty subscene
    • Add a new Cube to the subscene
    • Add the DebugWrongEntityAuthoringComponent component to the cube

    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using UnityEngine;
    4.  
    5. [DisallowMultipleComponent]
    6. public class DebugWrongEntityAuthoringComponent : MonoBehaviour
    7. {
    8.     public class Baker : Baker<DebugWrongEntityAuthoringComponent>
    9.     {
    10.         public override void Bake(DebugWrongEntityAuthoringComponent authoring)
    11.         {
    12.             AddBuffer<MapData>();
    13.             var mapEntity = GetEntity();
    14.  
    15.             var extraWorkingEntity = CreateAdditionalEntity();
    16.             AddComponentObject(extraWorkingEntity, new LoadMyMapWorking { MyMap = mapEntity });
    17.  
    18.             var extraBuggyEntity = CreateAdditionalEntity();
    19.             AddComponentObject(extraBuggyEntity, new LoadMyMapBuggy { MyMap = mapEntity });
    20.         }
    21.     }
    22. }
    23.  
    24. public struct MapData : IBufferElementData
    25. {
    26.     public int DummyData;
    27. }
    28.  
    29. public class LoadMyMapWorking : IComponentData
    30. {
    31.     public Entity MyMap;
    32.     public string StringData1;
    33. }
    34.  
    35. public class LoadMyMapBuggy : IComponentData
    36. {
    37.     public Entity MyMap;
    38.     public string StringData1;
    39.     public string StringData2;
    40. }
    41.  
    42. [RequireMatchingQueriesForUpdate]
    43. public partial class LoadMyMapSystem : SystemBase
    44. {
    45.     protected override void OnUpdate()
    46.     {
    47.         var ecb = this.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(World.Unmanaged);
    48.         var getBuffer = GetBufferLookup<MapData>(isReadOnly: false);
    49.  
    50.         Entities
    51.              .WithoutBurst()
    52.              .ForEach((LoadMyMapWorking loadMyMap, in Entity loadEntity) =>
    53.              {
    54.                  ecb.DestroyEntity(loadEntity);
    55.                  var myData = getBuffer[loadMyMap.MyMap];
    56.                  myData.Add(new MapData());
    57.                  Debug.Log("LoadMyMapWorking.MyMap is working as expected");
    58.              })
    59.              .Run();
    60.  
    61.         Entities
    62.              .WithoutBurst()
    63.              .ForEach((LoadMyMapBuggy loadMyMap, in Entity loadEntity) =>
    64.              {
    65.                  ecb.DestroyEntity(loadEntity);
    66.                  try
    67.                  {
    68.                      var myData = getBuffer[loadMyMap.MyMap];
    69.                      myData.Add(new MapData());
    70.                  }
    71.                  catch (System.Exception e)
    72.                  {
    73.                      // Oopsy!! :
    74.                      // A component with type:MapData[Buffer] has not been added to the entity.Entities Journaling may be able to help determine more information.
    75.                      // Please enable Entities Journaling for a more helpful error message.
    76.                      Debug.LogError("LoadMyMapBuggy.MyMap is NOT working as expected: ");
    77.                      Debug.LogError(e.Message);
    78.                  }
    79.              })
    80.              .Run();
    81.     }
    82. }

    Context

    Unity Version:
    2022.2.0b14.112.4874

    Packages versions

     
  2. bogdancoder

    bogdancoder

    Unity Technologies

    Joined:
    Feb 6, 2017
    Posts:
    29
    Thank you for the report. I was able to repro and I'm working on the fix.

    In the mean time, you can get around this issue by declaring the Entity field last in the class component.
     
    instriker_911 likes this.
  3. instriker_911

    instriker_911

    Joined:
    Mar 31, 2022
    Posts:
    27
    Thank you, I confirm the workaround works!