Search Unity

How Get One Specific Entity ?

Discussion in 'Entity Component System' started by Klausast, Jan 26, 2019.

  1. Klausast

    Klausast

    Joined:
    Nov 27, 2014
    Posts:
    7
    Hello,
    I am trying to get data from one specific entity. But for this, I need entity first.
    How to get an entity with, for example, ComponentTag like "ShipTag" ?

    I tried [Inject] but it's not getting any data

    Entity is created this way:
    Code (CSharp):
    1. var shipEntity = entityManager.CreateEntity(ComponentType.Create<Position>(),
    2.                                                         ComponentType.Create<Rotation>(),
    3.                                                         ComponentType.Create<MoveSpeed>(),
    4.                                                         ComponentType.Create<RotationSpeed>(),
    5.                                                         ComponentType.Create<CellId>(),
    6.                                                         ComponentType.Create<ShipTag>(),
    7.                                                         ComponentType.Create<PlayerInput>());

    For example, I want this ship position(on stage I have only one ship), so I created ShipData :
    Code (CSharp):
    1. public struct ShipData
    2.     {
    3.         public int Length;
    4.         [ReadOnly] public ComponentDataArray<Position> Positions;
    5.         [ReadOnly] public ComponentDataArray<ShipTag> ShipTag;
    6.     }
    And trying to access it this way in regular Monobehaviour Script:
    Code (CSharp):
    1. [Inject]ShipData shipData;
    2.  
    3. void SpawnBullet()
    4. {
    5. ... Creating Bullet Archetype...
    6.  
    7. var bulletEntity = em.CreateEntity(bulletArchetype);
    8. EntityManager.SetComponentData(bulletEntity, new Position { Value = shipData.Positions[0].Value });
    9. }
    Is there any easier way? for example GetEntities<ShipData>[0].Position ... or something else or maybe I am missing something.
    Compiler says: That kind of entity doesn't exist... Why when I am calling this function after 3 seconds from game start (ShipEntity spawns from the beginning of the game)
     
    Last edited: Jan 26, 2019
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You can use GetComponentFromData<Position>() and then retrieve the position using the ship Entity.
     
  3. Klausast

    Klausast

    Joined:
    Nov 27, 2014
    Posts:
    7
    But how to get this ShipEntity, I need it in a different system which doesn't know about the ship. I was thinking of storing it in public variable during creation but for this, I need to access Bootstrap class which, in my opinion, is not the best way...
     
    DotusX likes this.
  4. DotusX

    DotusX

    Joined:
    Sep 28, 2014
    Posts:
    5
    If this is for example, the Player Ship... Why not have a PlayerShipComponent added to only 1 ship in the game, and look for that in your other system? Components are cheap. They can be used as booleans.
     
  5. Klausast

    Klausast

    Joined:
    Nov 27, 2014
    Posts:
    7
    Yes, Exactly, and how to do this? I found posts about GetEntityArray but it seems it is not supported anymore.
     
  6. Klausast

    Klausast

    Joined:
    Nov 27, 2014
    Posts:
    7
    DotusX likes this.
  7. DotusX

    DotusX

    Joined:
    Sep 28, 2014
    Posts:
    5
    Is it not possible to have a PlayerSystem looking specifically for the PlayerComponent in your scenario?
     
  8. DotusX

    DotusX

    Joined:
    Sep 28, 2014
    Posts:
    5
    Inside your system that needs to find other components:


    Code (CSharp):
    1.        
    2.  
    3. private ComponentGroup _playerGroup;
    4.  
    5.         protected override void OnCreateManager()
    6.         {
    7.             _playerGroup = GetComponentGroup(typeof(Player));
    8.         }
    9.  
    10.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    11.         {
    12.             var players = _playerGroup.GetEntityArray();
    13.             var player = players.Length > 0 ? players[0] : Entity.Null;
    14.             var moveJob = new MovementJob
    15.             {
    16.                 DeltaTime = Time.deltaTime,
    17.                 Player = player
    18.             };
    19. ...
    20.  
    21.  
     
    Klausast likes this.
  9. Klausast

    Klausast

    Joined:
    Nov 27, 2014
    Posts:
    7
    Thank you it's even shorter and safer.
     
    DotusX likes this.