Search Unity

Linking An Entity To A Collection Of Entities?

Discussion in 'Entity Component System' started by aeldron, Apr 15, 2019.

  1. aeldron

    aeldron

    Joined:
    Feb 12, 2013
    Posts:
    32
    Hello.

    I'm wondering how to save a reference to a collection of entities. In a traditional OOP game, you could have something like:

    Code (CSharp):
    1.  
    2. public class Ship : MonoBehaviour
    3. {
    4.     private Dictionary<int, GameObject> enemiesInRange;
    5.     void AddEnemy(int enemyID, GameObject enemy)
    6.     {
    7.         enemiesInRange.Add(enemyID, enemy);
    8.     }
    9.     void RemoveEnemy(int enemyId)
    10.     {
    11.         if (enemiesInRange.ContainsKey(enemyId))
    12.         {
    13.             enemiesInRange.Remove(enemyId);
    14.         }
    15.     }
    16. }
    17.  
    What is the best approach for something like that on an ECS framework? Can I use a NativeHashMap on ComponentData to keep track of a bunch of entities? Should I be using a dynamic buffer instead?

    To be more specific about my problem: I am using a hybrid approach and using a MonoBehaviour with OnTriggerEnter and OnTriggerExit to detect collisions with gameobjects in the scene. I then attach a CollisionData component to my colliding object, and I need to save a reference to all GameObjectEntity intersecting with that object. A Hashtable with Allocation.Persistent could do the trick, but is it better to use a dynamic buffer in this case?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Using DynamicBuffer is a reasonable approach.
     
    aeldron likes this.