Search Unity

Using jobs for timed sequences?

Discussion in 'Entity Component System' started by Alverik, Apr 22, 2019.

  1. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Hi, new to ECS here. I'm prototyping a few systems for a turn based RPG right now, and I wanted to start getting used to ECS if possible. But, after reading a lot and watching several videos, l'm still wondering a few things... First, What would be the proper way to deal with timed sequences when using ECS? Is something like the following acceptable?

    Code (CSharp):
    1. public class SeqSystem : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     private struct SequenceJob : IJobForEachWithEntity<SeqData, Translation>
    5.     {
    6.         public float dt;
    7.         public void Execute(Entity entity, int index, ref SeqData sequenceData, ref Translation pos)
    8.         {
    9.             pos.Value.x += sequenceData.Distance * dt;
    10.         }
    11.     }
    12.  
    13.     [BurstCompile]
    14.     private struct SequenceJob2 : IJobForEachWithEntity<SeqData, Translation>
    15.     {
    16.         public float dt;
    17.         public void Execute(Entity entity, int index, ref SeqData sequenceData, ref Translation pos)
    18.         {
    19.             pos.Value.x -= sequenceData.Distance * dt;
    20.         }
    21.     }
    22.  
    23.     private float _time;
    24.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    25.     {
    26.         _time += Time.deltaTime;
    27.  
    28.         JobHandle job;
    29.         if (_time <= 2)
    30.         {
    31.             job = new SequenceJob { dt = Time.deltaTime }.Schedule(this, inputDeps);
    32.  
    33.         } else if (_time > 2 & _time <= 4)
    34.         {
    35.             job = new SequenceJob2 { dt = Time.deltaTime }.Schedule(this, inputDeps);
    36.         }
    37.         else
    38.         {
    39.             _time = 0;
    40.             return inputDeps;
    41.         }
    42.  
    43.         return job;
    44.     }
    45. }
    It works, but I wonder if doing it this way could lose some performance, or if it goes against the ECS mindset... I basically just used the OnUpdate() as I'd use the regular Update() method, haha. I know another way would be to add/remove component tags to an entity, but it kinda feels overly wordy, specially since I want to use this in a combat system (a la Persona 5) and there are ton of steps in the combat progression where things need to wait for the UI to fade out or an animation to be done before continuing. Right now ECS somehow feels better suited to real time stuff, like real time AI (which I'm going to try using it for too after I'm done with combat, since I want a town full of real time agents all wandering around), will probably not have many characters in a battle, so I was wondering if it even makes sense to use ECS in this case? I mean, in the regular way, I could simply have a coroutine with a loop, which runs a series of other coroutines using different types of yield statements... But one of the points of me doing this is trying to do it all using pure ECS if possible.

    Also, what's the equivalent of disabling/enabling the Renderer in ECS? I would like to know what's considered the right way to do it.

    Anyway, sorry for the noob questions, I feel like when someone gives you a new powerful toy and are a little afraid to use it wrong or break it, haha...
     
    Last edited: Apr 23, 2019
  2. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    The Hybrid renderer package has no Docs at all (or is the link in the package manager just broken)?...