Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Best way to call system/job every N frame?

Discussion in 'Entity Component System' started by Sarletor, Aug 28, 2023.

  1. Sarletor

    Sarletor

    Joined:
    Jan 4, 2018
    Posts:
    6
    Hello, I want to ask for advice on the implementation of the tickrate.

    At the moment, the logic in my game: I have a system that adds +1 to a tick in a singleton component.
    Further, if another system/job should be called every 20 ticks, then I pull the singleton component, check for a tick, and if the tick is 20, then I call my job.

    I'm not sure if this is the most right solution and would like to know how it can be implemented in another way
     
  2. OUTTAHERE

    OUTTAHERE

    Joined:
    Sep 23, 2013
    Posts:
    656
    I'd like to know this too, I would probably do it with a singleton that has a counter.

    I would actually like to schedule some jobs that interrupt and resume after doing a predefined amount of work. That seems only possible with additional ICDs and queries on them, as well.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    It depends.
    If its per system - then custom IRateManager + custom group should do.
    If its per entity - use component data to "notify" specific systems via queries, or use enableable components.
     
    koonm likes this.
  4. Sarletor

    Sarletor

    Joined:
    Jan 4, 2018
    Posts:
    6
    IRateManager works with time, not frames. Am I wrong?
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    ShouldGroupUpdate determines whether group should be updated. How its implemented - is up to you.

    Store a tick value inside the manager and just tick it each time call is requested. Or check against UnityEngine.Time.frameCount to be more precise.

    Here's an example from the tests where only once per frame group is updated:
    Code (CSharp):
    1.         class CustomRateManager : IRateManager
    2.         {
    3.             private bool m_UpdatedThisFrame;
    4.             public int UpdateCount { get; private set; }
    5.  
    6.             public bool ShouldGroupUpdate(ComponentSystemGroup group)
    7.             {
    8.                 // if this is true, means we're being called a second or later time in a loop
    9.                 if (m_UpdatedThisFrame)
    10.                 {
    11.                     m_UpdatedThisFrame = false;
    12.                     return false;
    13.                 }
    14.  
    15.                 m_UpdatedThisFrame = true;
    16.                 UpdateCount += 1;
    17.                 return true;
    18.             }
    19.  
    20.             public float Timestep { get; set; }
    21.         }
     
  6. Sarletor

    Sarletor

    Joined:
    Jan 4, 2018
    Posts:
    6
    Thanks for example!
     
    xVergilx likes this.