Search Unity

[Inject] AND filter?

Discussion in 'Entity Component System' started by Floofloof, Aug 16, 2018.

  1. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    One of the things i find frustrating with the current implementation of ECS is that when you inject groups its an OR filter and there doesnt seem to be a way to do an AND filter.
    For Example

    struct SomeDataGroup{
    public ComponentDataArray<DataTypeOne> typeOneData;
    }

    struct SomeOtherDataGroup{
    public ComponentDataArray<DataTypeTwo> typeTwoData;
    }

    [Inject] SomeDataGroup someDataGroup;
    [Inject] SomeOtherDataGroup someDataGroup;


    The system will run if either group is present. But What if I want to require both groups to exist before running the system when they are not on the same entity. Currently we have to run the system if one is present and then check and possibly exit if the other is not there. This leads to systems running almost at all times even when they are not needed.

    You can even see this issue in the Examples give for ECS here. In the update they exit if ships aren't present.

    Im curious if this issue is going to be addressed or if it already has been. Is this the right approach to solve this problem? Is there an "ECS" way of fixing this issue?
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    The solution above, but early out if either group's length is zero. Merge as appropriate to your problem domain.
     
  3. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    Is this the ECS way of doing things or just "A" way of doing it. I have no issue getting something like this to work it just feels weird to have to do that.
     
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    If you don't expect
    DataTypeOne
    and
    DataTypeTwo
    to be on the same entity, then yes. The Injection System takes into account only entities that match the prerequisites. There can be overlapped entities in injected groups, if an entity in your situation can have one of the above or both of the above.

    You can use the groups to get pretty granular sets, however:


    Code (CSharp):
    1.  
    2. struct SomeDataGroup{
    3. public ComponentDataArray<DataTypeOne> typeOneData;
    4. public SubtractiveComponent<DataTypeTwo> NoTypeTwo;
    5. }
    6.  
    7. struct SomeOtherDataGroup{
    8. public ComponentDataArray<DataTypeTwo> typeTwoData;
    9. public SubtractiveComponent<DataTypeOne> NoTypeOne;
    10. }
    11.  
    12. struct SomeBothDataGroup{
    13. public ComponentDataArray<DataTypeOne> typeOneData;
    14. public ComponentDataArray<DataTypeTwo> typeTwoData;
    15. }
    16.  
    17. [Inject] SomeDataGroup someDataGroup;
    18. [Inject] SomeOtherDataGroup someDataGroup;
    19. [Inject] SomeBothDataGroup someBothGroup;
    This will get the distinct component sets with only one, only two, and both one and two. If you put
    EntityArray 
    in each set, you can verify all the entities are guaranteed to be distinct from each other.

    It's possible to do an Any/Or/None lookup, but only at the Archetype Chunk level. Most of the usage of that I've seen or done is for verifying test entities without scanning all entities in a test world, and it's much more verbose to work with.
     
  5. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41

    my main issue really is systems running by injecting groups that aren't all there but at least one is. Im not sure how your reply connects but the "Archetype Chunk level" stuff sounds interesting. Do you have any sources that explain that a bit more? and im assuming this would solve my multiple inject issue?
     
  6. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    There's a Transform patch test in later versions of the ECS package i used as a reference. On my phone atm so I can't look it up exactly right now.

    It seems less hacky for verifying entity data post-test.