Executing, Cancelling, and Restarting Actions

v1.0

Executing Actions

This page deals with executing ActionExecutor objects via code. Check the Action on Events & Action Runner page on using the ready-to-use components with ActionExecutor objects on them.

Execute(this)

Calling Execute(this) is the most common way to execute Actions on an ActionExecutor. If the executor is able to begin or restart execution, it will do so.

The method will return a CancellationTokenSource, which you can cache if you'd like, though this is optional and in most cases not needed.

_cts = myExecutor.Execute(this);

ExecuteAsync()

If you need to wait the execution's completion before doing something else, you can use the ExecuteAsync() method. This pattern allows a class like ActionRunner to cleanly stop and restart actions, check for cancellation between loops, and ensure proper cleanup.

circle-info

ActionRunner component uses this method to loop the ActionExecutor once it completes.

private CancellationTokenSource _cts;

async void StartActions()
{
    // Create a cancellation token source
    _cts = new CancellationTokenSource();
        
    // Execute with cancellation control
    var result = await myExecutor.ExecuteAsync(this, _cts.Token);
        
    if (result.WasCanceled)
        Debug.Log("Actions were canceled!");
}

Cancelling Actions

Once active, you can cancel an ActionExecutor in a few ways.

Async Cancellation

If you are running an ActionExecutor through ExecuteAsync() you can cancel it:

Dispose of the CancellationTokenSource

If you did cache the CancellationTokenSource when calling Execute(this), you can cancel and dispose of it:

Cancel All Actions

You can also call CancelAllActions() which calls OnCancel() on all tracked Actions. In most cases, this will stop the Actions immediately, though each Action may have different OnCancel() behavior.

Emergency Shutdown

Generally used OnDestroy(), this will immediately shut down lifecycle processes, coroutines, and tracked actions.

triangle-exclamation

StopActionSequenceAction

There's also an Action which stops all actions! Utilize this in an ActionExecutor flow to stop current sequences (on "this" ActionExecutor), all sequences (globally), or sequences on another specific target.

Restarting Actions

If the ActionExecutor allows restarts, and if the cooldown period has elapsed, then you can trigger a restart even if the actions have not yet completed.

Simply call Executor.RestartActions() to do this.

circle-check

Last updated