Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to create/instantiate entities?

Discussion in 'Project Tiny' started by Arowx, Jun 30, 2019.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Based on the examples it looks like I need to load a scene to add an entity is that right?
     
  2. AlexMasse

    AlexMasse

    Joined:
    Jun 29, 2014
    Posts:
    19
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Trying that approach and the problem is I'm getting an error, in Browser builds it just appears as a system error in Dot Net and IL2CPP builds I get an error in the console window but that shuts down.

    So how can I find out what the error is?
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Code (CSharp):
    1. EntityArchetype tile = EntityManager.CreateArchetype( new ComponentType[]
    2.                     {
    3.                         typeof(Parent),
    4.                         typeof(Translation),
    5.                         typeof(Rotation),
    6.                         typeof(NonUniformScale),
    7.                         typeof(LayerSorting),
    8.                         typeof(Sprite2DRenderer)
    9.                     }
    10.                     );
    11.  
    12.                 Entity entity = EntityManager.CreateEntity(tile);
    13.  
    14.                 EntityManager.SetComponentData(entity, new Parent { Value = Entity.Null });
    15.                 EntityManager.SetComponentData(entity, new Translation { Value = new float3(x,y,0) });
    16.                 EntityManager.SetComponentData(entity, new Rotation { Value = quaternion.identity});
    17.                 EntityManager.SetComponentData(entity, new Sprite2DRenderer { color = new Color(rnd.NextFloat(), rnd.NextFloat(), rnd.NextFloat(), 1f) });
    18.                 EntityManager.SetComponentData(entity, new NonUniformScale { Value = new float3(10, 10, 1) });
    19.                 EntityManager.SetComponentData(entity, new LayerSorting { order = 1 });
    Is there not a simpler way than this to set the data on an entity as it is just a struct after all?
     
  5. kevinmv

    kevinmv

    Unity Technologies

    Joined:
    Nov 15, 2018
    Posts:
    51
    If creating an entity in code, your approach is the correct way -- creating an entity from the EntityManager and setting the component data individually for any component data where default initialization is not desired. If default initialization is fine for all components, you can create the entity and pass in the archetype you've defined above as a parameter. If you don't need the archetype for anything else later you can even just pass the list of component types used for constructing the archetype as a parameter to CreateEntity instead:


    Code (CSharp):
    1. Entity entity = EntityManager.CreateEntity(
    2.                         typeof(Parent),
    3.                         typeof(Translation),
    4.                         typeof(Rotation),
    5.                         typeof(NonUniformScale),
    6.                         typeof(LayerSorting),
    7.                         typeof(Sprite2DRenderer)
    8.                     );

    It's good to clarify though that the explicit calls to SetComponentData are required because the entity is not 'just a struct after all'! :) Component data is laid out in chunks of memory as a struct of arrays (SOA) to allow for efficient iteration over components. So we need an explicit call to SetComponentData to know where in memory to set the component data for your specific entity since the component data is not side-by-side in memory as if the entity were a struct.

    Hope this helps!
    Kev
     
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK so it's not a struct but a struct of array elements, which with an offset index we could set ourselves, or we could have a nice interface with a getter/setter naming convention that would make it very easy to set the variables in an entity.

    After all it's just a piece of data at a memory address and computer programs have been abstracting this concept for a very long time.
     
  7. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Still getting a bug which I cannot capture/find the log for....

    In dot net mode I get a command line window that appears for a split second with an error trace, then vanishes.
    In asm mode I get a system error line in the console.

    How can I capture and trace errors when they do not appear or disappear?

    FIX (DOH)
    Running from command line ensures I see the error trace.
     
    Last edited: Jul 4, 2019