How to create a pause system with jobs and systems?

I’m wondering how I would do this with jobs, this kind of goes with the how to I start and stop jobs but a bit more specific. I’ve heard it said that you remove the data, but that doesn’t seem like a good idea(wouldn’t I have to create extra data just for the pause system?) personally I would just set all the systems to disabled but is that not a good idea. I’m just wondering peoples opinions on this.

as my first opinion, the best approach will be to make all your concerned systems Exclude a Tag Component “Pause” in their Jobs and/or Queries.
when you want to pause those systems just add to their related Entities the “Pause” CD Tag using Batch Operations, it should be extremely fast specially in your case the Pause Logic is not something happening every frame.

Good Luck!

1 Like

also you cannot Interrupt Jobs (for the moment)
normally your jobs should execute and do stuff every Frame and you should not think about them surviving over frames.
so no need to think about stopping Jobs execution.

Don’t try to pause in the middle of a frame. Let the jobs finish the frame up and then on the next frame, disable all the systems you don’t want to update. I usually have two ComponentSystemGroups, one for the pause menu and one for gameplay, and I only ever have one enabled at a time.

Thanks for the replies. So if I’m understanding correctly, I create an entity with the a pause component tag on whatever button I want to use to pause. I then have an entity query in all the jobs that I want to pause that excludes that tag component.

I’m not sure how make sure that the jobs have finished from an external system, especially if the jobs are designed to run constantly. I have to set completed(), and then check if completed from an external system?

Your jobs usually only apply to one frame at a time. So instead of trying to pause jobs, you want to not schedule them when paused. I’m not sure why you are worried about ensuring the jobs completed. That seems like you have a bigger misunderstanding of how jobs work in ECS.

I’ll post my solution if anyone’s curious and to help anyone else or if anyone has criticisms for my method.

public struct PauseSystemsCompTag : IComponentData
{
}

[AlwaysUpdateSystem]
public class PauseGameSystem : ComponentSystem
{
    public WayPointMoveSystem WayPointMoveSystemref;
    public bool Isgamepausedornot;
  
    protected override void OnCreate()
    {
        
        Isgamepausedornot = false;
    }

    protected override void OnUpdate()
    {
        if (Input.GetKeyDown("p") && Isgamepausedornot == false)
        {

            if (!HasSingleton<PauseSystemsCompTag>())
            {
                var paused = EntityManager.CreateEntity(typeof(PauseSystemsCompTag));
                EntityManager.SetName(paused, "PausegameEntity");
                //WayPointMoveSystemref.Enabled = false;
                Debug.Log("The p key was pressed");
                Isgamepausedornot = true;
            }
            else
            {
                Debug.Log("Something shit happened with the pause system");
            }

        }
        else if(Input.GetKeyDown(("p"))&& Isgamepausedornot == true)
        {
            EntityManager.DestroyEntity(GetSingletonEntity<PauseSystemsCompTag>());
            //WayPointMoveSystemref.Enabled = true;
            Debug.Log(("The p key was unpressed"));
            Isgamepausedornot = false;
        }
    }
}

I then put the below code in the OnCreate() method of any systems I want to pause and it seems to work, except this pause system stops running after pressing the key twice, that’s why you have to have the [Alwaysupdatesystem]. I wish I understood these systems and why they do that.

RequireSingletonForUpdate();