Search Unity

Does ECS have a timer/scheduler for running jobs/systems?

Discussion in 'Entity Component System' started by mentics, Jan 11, 2020.

  1. mentics

    mentics

    Joined:
    Aug 18, 2015
    Posts:
    10
    I know that inside a given system, I could check the current time and determine whether to run a job or not, but having a lot of systems that do that is inefficient. A proper scheduler that we can schedule future tasks and periodic tasks would be preferable. MonoBehavior InvokeRepeating/WaitForSeconds and such would do something like that. I'm just asking if that already exists in ECS, or is planned. I suppose we could use a MonoBehavior to enable a system, but would be cleaner to not have to mix things like that. If it's not planned, it would be somewhat straightforward to build one (I've done it before).

    Here's an example that works for real time, but not game time. If there were an event fired whenever Time.timeScale was modified (or create app level one and trust you always use it), one could use delayed execution in real time to make an extremely efficient scheduler by waiting until "next scheduled event" * Time.timeScale and adding a thread safe interrupter for when new things are scheduled. If something like this doesn't already exist and is not planned, I can write it up and make it available to those interested.

    Is enabling/disabling a system the best way to run a job occasionally like this, or is there some other better mechanism for scheduling a job in this situation?

    Code (CSharp):
    1.  
    2. using System.Threading;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5.  
    6. public class TestTimerSystem : JobComponentSystem {
    7.     Timer timer;
    8.  
    9.     protected override void OnCreate() {
    10.         timer = new Timer(o => this.Enabled = true, null, 0, 100);
    11.     }
    12.  
    13.     protected override JobHandle OnUpdate(JobHandle inputDependencies) {
    14.         // Schedule job
    15.         UnityEngine.Debug.Log("ran timed thing at " + UnityEngine.Time.time);
    16.         this.Enabled = false;
    17.         return inputDependencies;
    18.     }
    19. }
    20.  
     
  2. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    You could use

    RequireSingletonForUpdate<PauseSystemsCompTag>();

    I put that in the Oncreate method of the system that I want to control, that then requires the tag to exist for the update part of the system to run. So you can easily just create or destroy an entity with that tag from another system. Its an easy way to make a pause system, but could just as easily be used for timed events.
     
  3. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    I was just thinking about this, I wonder if at some point we'll get an ECS equivalent of coroutines