Best way to call system/job every N frame?

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

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.

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.

1 Like

IRateManager works with time, not frames. Am I wrong?

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:

        class CustomRateManager : IRateManager
        {
            private bool m_UpdatedThisFrame;
            public int UpdateCount { get; private set; }

            public bool ShouldGroupUpdate(ComponentSystemGroup group)
            {
                // if this is true, means we're being called a second or later time in a loop
                if (m_UpdatedThisFrame)
                {
                    m_UpdatedThisFrame = false;
                    return false;
                }

                m_UpdatedThisFrame = true;
                UpdateCount += 1;
                return true;
            }

            public float Timestep { get; set; }
        }

Thanks for example!

1 Like