Search Unity

Conditional Query?

Discussion in 'Entity Component System' started by SubPixelPerfect, Jul 9, 2019.

  1. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    For example:
    I have a game where 100000000 black flys live in the forest. Normally each fly sits and does nothing, but if there is a man is in the forest, all flys should start following him and try to bite him.


    So I need a system that does 2 things:
    - check if man exists (query for the entity with [man] component and its [position])
    - and if the man was found iterates on all flys and call MoveTowards(man.position) function

    the problem is with "if", how to make a conditional query?
    if the man isn't in the forest, the second costly query should not happen

    Best idea I have is to split it into 2 systems: CheckForManSystem and MoveFlysSystem
    and to make CheckForManSystem enable/disable MoveFlysSystem

    What is the best way to organize this behavior in ECS?
    Is it possible to skip the second query using only one system?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Why complicate it, single system with a requirement on the man query to run

    this.RequireForUpdate(manQuery);

    or

    this.RequireSingletonForUpdate<Man>();

    if there is only ever a maximum of 1 man.
     
    Last edited: Jul 9, 2019
    florianhanke and SubPixelPerfect like this.
  3. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    wow, I've missed a moment when RequireForUpdate and singleton methods were added
     
  4. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    Perfect, thank you, works like a charm )
     
    tertle likes this.