Scheduler

The Scheduler allows for delayed execution of methods. As you are going through the code you’ll see that this is used in many ways, from scheduling when a projectile should deactivate to checking that a controller is connected. The scheduler also accepts up to three parameters if the calling method requires it. When a new method is scheduled to occur in the future a ScheduledEventBase object is returned. This object allows you to cancel events before they are scheduled to occur.

An event can be scheduled with:

Scheduler.Schedule(float delay, Action action)

Where delay specifies the number of seconds that the action should execute. If a value of -1 is supplied for the delay then the event will be executed every frame. This is extremely useful when you want to update a particular method without having to manually update it within the standard MonoBehaviour loop. A great use case of this is the Spring system and it ticking every frame.

When the event in the above example is executed it is executed within Unity’s Update loop. If instead you’d like the event to be executed within the FixedUpdate loop you can schedule the event with the following:

Scheduler.ScheduleFixed(float delay, Action action)

Abilities and items are generally updated within the FixedUpdate loop so they mostly use the ScheduleFixed variant, while everything else is updated within Update so they use the regular Schedule variant.

Scheduled events can later be cancelled with:

Scheduler.Cancel(ScheduledEventBase scheduledEvent)

ScheduledEventBase objects can be checked to determine if they are currently scheduled with the Active property. A full example of using the Scheduler API is below.

// The scheduler class is in the Game namespace.
using Opsive.UltimateCharacterController.Game;

// Schedule the Explode method to occur in 1.5 seconds. The first parameter of Explode will have a value of true.
var scheduledEvent = Scheduler.Schedule(1.5f, Explode, true)

// Check the active state of the scheduled event.
Debug.Log("Is active: " + scheduledEvent.Active);

// Cancel the scheduled event
Scheduler.Cancel(scheduledEvent);