Search Unity

Creating GameObjectEntity from Archetype?

Discussion in 'Entity Component System' started by questionable_nickname, Jul 4, 2018.

  1. questionable_nickname

    questionable_nickname

    Joined:
    Jul 1, 2018
    Posts:
    10
    When I want pure Entity, I can create it from Archetype like this:
    Code (CSharp):
    1. PlayerArchetype = entityManager.CreateArchetype(typeof(Position), typeof(Heading), typeof(PlayerInput));
    2. ...
    3. entityManager.CreateEntity(Archetypes.PlayerArchetype);
    Now I wonder, is there a similar way to create GameObjectEntity from Archetype?
    Currently I'm doing this, feels wrong:
    Code (CSharp):
    1. var cameraEntity = Camera.AddComponent<GameObjectEntity>().Entity;
    2. entityManager.AddComponent(cameraEntity, typeof(Position));
    3. entityManager.AddComponent(cameraEntity, typeof(Heading));
    I've tried to do this:
    Code (CSharp):
    1. cameraEntity.Entity = entityManager.CreateEntity(Archetypes.CameraArchetype);
    But GameObjectEntity's Entity property is readonly.
     
  2. questionable_nickname

    questionable_nickname

    Joined:
    Jul 1, 2018
    Posts:
    10
    I realized GameObjectEntity doesn't automatically synchronize component data and gameobject's transform, so I actually don't need Position/Rotation/Heading in my camera entity for now, but the question is still important for more general case.
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    You want an equivalent result that GameObjectEntity produces? In that case I believe you are only missing the ability to inject that entity as `GameObjectArray` after manually adding Position and Heading.

    To do that add Transform Component to it because that is the real identity of GameObject that is coming in the injected GameObjectArray. (It is actually ComponentArray<Transform> IIRC)

    And for continuously synchronizing game object data to entity you can put CopyTransformFromGameObjectSystem mono behaviour on it.
     
  4. questionable_nickname

    questionable_nickname

    Joined:
    Jul 1, 2018
    Posts:
    10
    Thanks, works fine. Not sure why to have Position and to sync it to Transform via some special system or MonoBehaviour when I can just manipulate transform directly in system's OnUpdate. Still interested in original question for structuring code, seems a good idea to describe set of components in archetypes even if it's not pure entity. After all, I'm currently using ECS just for code structuring, I don't really need big amounts of anything anywhere.
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    The bundled system that manipulate the Position based on Heading, Speed, etc. runs in thread so that's one performance improvement. You can also create your own that exploit worker threads so it is better than OnUpdate. (Also Transform can be in thread by TransformAccessArray)

    And if you decided to go pure ECS some day (draw things from Position/Rotation data rather than Transform data) you could potentially give those data directly to the drawer. But not at the moment as MeshInstanceRenderer still copy data out of ECS in order to render.


    Can't you do like typeof(Transform) typeof(RigidBody) in the archetype? I am not sure. But there's also this way :

    Code (CSharp):
    1. var go = new GameObject("test", typeof(Rigidbody));
    2. GameObjectEntity.AddToEntityManager(m_Manager, go);
     
    questionable_nickname likes this.