Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Two groups in a system: AND system execution condition

Discussion in 'Entity Component System' started by Antypodish, Sep 15, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    I think similar question was asked before, but I am unable to track it down.
    But question is just troubling me for a while, so I decided to reach forum.

    In short: I want execute system only, when both groups are valid. Not when either of them is valid.


    Lets say I got two groups in a system, to execute a job.

    Code (CSharp):
    1. struct CarsData
    2. {
    3.     public readonly int Length ;
    4.     public ComponentDataArray <CarComponent> a_cars ;
    5. }
    6.  
    7. struct SpareWheelsData
    8. {
    9.     public readonly int Length ;
    10.     public ComponentDataArray <WheelComponent> a_sparewheels ;
    11. }
    I think for this case, I want to avoid using Buffer Array.

    Now in OnUpdate

    Code (CSharp):
    1. protected override JobHandle OnUpdate ( JobHandle inputDeps )
    2. {
    3.     EntityCommandBuffer commandBuffer = compositeBarrier.CreateCommandBuffer () ;
    4.    
    5.     var jobHandle = new AddRequiredSpareCompositesJob
    6.     {              
    7.         commandBuffer = compositeBarrier.CreateCommandBuffer (),
    8.         carsData = carsData,
    9.         spareWheelsData = spareWheelsData,
    10.     } ;
    11. }
    then do some job ...

    From what I gather, I see that system is executed in OR logic manner, in respect to met group condition.
    This is, when either of group match condition. Either I have cars, or I have spare wheels, or both.

    But my question is, how I make system to execute, ONLY when both (AND), conditions are met (have car and spare wheels), but NOT when I got only wheels, or only cars?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Fastest solution is just check Length of both groups in update first, and just return if one of group is 0 length.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Yep, this is so far best thing I can think of too.
    Only that it potentially leads later in development to many systems, which are almost idle, checking of the lengths.
    I try keep such pair systems to minimum in size. Or looking a ways, to avoid when can.
    I know Enable / Disable system can be utilized. But not always is suitable.

    Do you know by any chance, if check in Update, is more performant, rather than doing check in a job itself? I suppose it may be, by small margin, as there is few less calls?
     
  4. Deleted User

    Deleted User

    Guest

    Hi. I've faced with the same problem. Check my answer here. There is also a measurement of length's check.
    https://forum.unity.com/threads/upd...if-there-is-no-injection.556174/#post-3685225
     
    Antypodish likes this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    Last edited: Sep 17, 2018
  6. Deleted User

    Deleted User

    Guest

    I think, It is better to check the length before scheduling a job. If it is zero just return from a method.
     
    Antypodish likes this.
  7. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    110
    It is expected that the system will handle early-out in this case. (You would add the code to check the requirements you have for running yourself.)
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    I understand that early-out, is nothing more, but return in if condition, in top of OnUpdate?
    Unless there is something else? I know @Joachim_Ante has mentioned it too, few months back.
     
  9. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    110
    Yes, that is it.
     
    Antypodish likes this.