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

Question One set of Entities affecting an other set of Entities - Best Practice

Discussion in 'Entity Component System' started by Santonian, May 3, 2021.

  1. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    Hi folks,
    I have a best practice question. I am fairly new to the DOTS world, but I got the principles and have my first things running. However, I am struggling with the concept of having different Entities with different Components affecting each other.

    Usually I write a System Based Class that does work for a couple of entities with one or more components. I get that principle and I can work with this.

    But I came to the point, where I have one kind of entity which affects an other kind of entity. I don't quite see how I can handle this, or rather whats the supposed way of doing this is.

    The only thing I can come up with, doing an entity query with the first set of entities, storing the needed information (somehow) and then doing a second query with the other type of entity and modifying them with the previously stored data. And doing all that in the update method of the same system class.

    This doesn't feel right and I think there is a better, more sophisticated way of doing this.

    I hope I could make myself clear, any help or examples are greatly appreciated.

    Cheers
    Santo.
     
  2. ErikJohnson

    ErikJohnson

    Joined:
    Jan 4, 2013
    Posts:
    4
    Sounds feasible to me. I think I have done the same (can't seem to find an example to show you). Just create an EntityQuery in your SystemBase OnCreate(), then execute the query during the OnUpdate().

    Code (CSharp):
    1.  
    2. //pseudo code...
    3.  
    4. OnUpdate() {
    5.   var others = _query.ToNativeArray();
    6.  
    7.   Entities
    8.   .ForEach( (Enity entity) = > {
    9.  
    10.   foreach (var thing in others) {
    11.    ...
    12.   }
    13.  })
    14. .Schedule();
    15.  
     
  3. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    You would use component data from entity and use a random access. Not everything in ecs has to be linear. Having one set of entities affect another is a random access problem, so don't worry about using component data from entity.

    And yea you would use two entity queries.
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Can you give an example of the problem you are trying to solve? There are multiple ways for how entities can affect each other and which solution would work best depends on the problem you are trying to solve.
     
  5. Chris-Herold

    Chris-Herold

    Joined:
    Nov 14, 2011
    Posts:
    115