How do you pause or stop running the job?

I have basic ECS job system up and running which is great, but they all run without any “run” or “execute” command from main script. The JobComponentSystem runs without any scripting. It has OnUpdate method that schedules the its own job but there is nothing I can do to control it.

If I want the game to stop or pause executing this JobComponentSystem, how do I go on about doing that?

public class TreeMoveSystem : JobComponentSystem
{
    [BurstCompile]
    private struct UpdateTreeMoveJob : IJobForEach<Translation, MoveSpeedComponent>
    {
        public float deltaTime;

        public void Execute(ref Translation translation, ref MoveSpeedComponent moveSpeedComponent)
        {
            translation.Value.y += moveSpeedComponent.moveSpeed * deltaTime;

            if (translation.Value.y > 10f)
            {
                moveSpeedComponent.moveSpeed = -math.abs(moveSpeedComponent.moveSpeed);
            }

            if (translation.Value.y < -10f)
            {
                moveSpeedComponent.moveSpeed = math.abs(moveSpeedComponent.moveSpeed);
            }
        }
    }

   
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        UpdateTreeMoveJob updateTreeMoveJob = new UpdateTreeMoveJob
        {
            deltaTime = Time.deltaTime
        };

        JobHandle jobHandle = updateTreeMoveJob.Schedule(this);

        return jobHandle;
    }   
}

So basically if I have this script in the project, it just runs… no matter what.

Ir runs first time always, cause you not declare EntityQuery explicitly, and IJobForEach generates it at first Schedule, after that system runs by query All: (Translation, MoveSpeedComponent) if entities match query exists.

JobComponentSystems have an Enabled property that you can set to False. (P.S: This corresponds to the checkbox left of the system in the Entity Debugger)

Oh… Enabled property seems to be promising… Also got to learn about query. It is not so easy to find info about ECS …

Look for the View documentation link in the Package Manager when looking at a package. The Entities package for example points to: Entity Component System | Package Manager UI website (On the systems page you can find the note about Enabled, see Systems | Package Manager UI website)

Personally to get a job to run once I just incremented an integer in the onupdate loop and checked if it was equal to one then set this.enabled to false. I don’t know if its a good way of doing, but its seems ok to me you can get a job simply to loop as many times as you want with this way.

I’ve also heard about using event entities where you create an entity with an component, and use that to schedule jobs on creation and stop them on destruction. I havent tried that method myself yet though.