Search Unity

Question about system reusability

Discussion in 'Entity Component System' started by Kiupe, Nov 6, 2019.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello,

    I have just watch Mike Geig presentation at Unite Copenhagen and now I have questions. In the video he stated that we have to think about data and what to do with that data.



    To illustrate that it created a job system to rotate and move entities toward a target. At least that is the goal, but in his code he has to clearly specify a tag component to be sure to process only enemy entities (and not the bullets for example) and in the system OnUpdate method he has to hard-coded the target position which is the player.

    In the talk Mike said that system can be used to drive bullets for example. But looking at this example I'm not sure how that would be possible. It's like you have to create as much tag component as system + target entities you have in order to be sure to only process those entities. It seems that you also need to create/duplicate as much system as variation of that system you need. ie: one system that specifically target the player another one for the bullets in which the system could for example target specifically a boss enemy.

    I'm pretty sure I'm wrong here, because it would be a very cumbersome approach. So, given that example, meaning having a system that rotate and move entities toward a target, how can I re-use the same system, so the same logic for rotate and move, to process different group of entities and having a different target for each ?

    Thanks
     
  2. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    It's entirely possible to write generic systems as long as you restrict what the system does to only one thing (ie just move, not check for collisions as well).

    For example you could write a movement system like described that would read a velocity component and add to a position. Then you would add further systems for the additional behaviors, ie detect collisions once an overlap happens.
     
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Ok. But let's say I want a system that just move toward a target. How can I re-use the same system to process different entities and have a different target ?

    In OOP I would simply instantiate the system, pass-in the entities and the target and voilà. But with DOTs system are executed automatically, I can't create an instance of a system and set its properties, or can'I ?

    Thanks.
     
  4. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    With ECS systems you rely instead of things that change the query, for example tags, or you could have dynamic data on the entities that changes how the systems behave.

    In your case I would probably write a system that looks for entities with Velocity and Target components, and set the velocity to move towards the target. Also set that system to run before the Movement system.
    Then the movement system can continue to do its thing, which is to apply velocity to its position.
     
  5. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    You could have a generic System that process every Entity with a TargetPosition and Velocity which simply adds to the Translation over time until the Translation TargetPosition are approximately the same and them removes the TargetPosition from the Entity. (Or you could have a different system only to remove the TargetPosition component depending on what you need).

    The System that would add the TargetPosition to the Entities would be a completely different system, and probably less generic.
     
  6. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Thanks guys.
     
    brunocoimbra likes this.