Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Scripting API

Events and Runners#

Code can execute designer-authored logic through an existing Event component or a serialized Runner field. Use an Event for scene-authored behavior and a Runner to expose extension points on your own component.

The Event component#

The Event component implements the runtime and cancellation contracts used by its Instructions. Its class declaration, with the body omitted, is:

public class Event : InitMonoBehaviour, ICancelToken, IRun, IEventCommand

IRun starts or schedules the sequence, while ICancelToken stops it when the Event is disabled or destroyed. IEventCommand delivers named commands to its Triggers; see Commands. Choose between immediate return and awaiting completion through these members:

Member Does
Run() Runs the Instructions with a default context. Fire and forget.
Run(Args args) Runs them with the context you supply.
Schedule() Returns the Awaitable for awaiting completion.
Schedule(Args args) Same, with a supplied context.
Stop() Cancels the running sequence.
IsRunning Whether it's mid-sequence.
RunningIndex Which Instruction is executing.

Use Schedule(args) when the caller must wait for the sequence to finish:

Event myEvent = target.GetComponent<Event>();

using Args args = Args.Get(this.gameObject, target);
await myEvent.Schedule(args);

The await waits for myEvent to finish before the caller continues. Use Run when the caller can continue immediately.

Events#

These notifications let your component observe the sequence as it runs:

Event Fires
EventInstructionStartRunning When the sequence starts.
EventInstructionEndRunning When it finishes or is canceled.
EventInstructionRun Each time an Instruction begins, with its index.

An Event that's already running ignores further requests to start rather than queueing them.

Commands#

A command runs an Event only if one of its Triggers is listening for it. Run starts the Event regardless of its Triggers, while a command passes through every Trigger deriving from TriggerCommand, and each one whose Command matches runs the Event.

Event implements IEventCommand explicitly, so call it through the interface:

private static readonly PropertyName COMMAND_OPEN = new PropertyName("open");

IEventCommand command = myEvent;
command.OnReceiveCommand(COMMAND_OPEN);

Without an Args argument, Source and Target are the Event's own game object. Pass an Args to OnReceiveCommand(command, args) to supply them. The listening side is a command Trigger.

Notes#

An Event can carry a note: a short text with an icon and a color, which the Inspector shows above its Triggers so a person can tell what the Event does without reading its Instructions. Notes are serialized for the editor and don't change how the Event runs. A tool that creates Events, such as an editor script or an AI agent, should write one so the result reads clearly to whoever opens it next.

Writing a note from code#

An Event's note is its Note property, a Note. Text is a string, and Icon and Color take the types of the icon and color to use:

Event myEvent = gameObject.GetComponent<Event>();

myEvent.Note.Text = "Opens the vault when the player has the key";
myEvent.Note.Icon = typeof(NoteIconLock);
myEvent.Note.Color = typeof(NoteColorYellow);

The note stores the icon and color as class names, so it keeps working however the types are loaded, and falls back to the defaults when a class no longer exists. Assigning null, or a type that doesn't derive from NoteIcon or NoteColor, clears that value: the Inspector then shows the Note icon in the Default color. An empty Text hides the message.

An editor script should record the Event before changing it, so the change can be undone and is saved with the scene:

Undo.RecordObject(myEvent, "Edit Note");

myEvent.Note.Text = "Opens the vault when the player has the key";
myEvent.Note.Icon = typeof(NoteIconLock);

When the Event belongs to a prefab instance, also call PrefabUtility.RecordPrefabInstancePropertyModifications(myEvent) afterward, so the new values are kept as overrides of the prefab.

Built-in icons#

Visual Script comes with a set of icons in the VisualScript.Icons namespace. Each one is a class that derives from NoteIcon, with a name that starts with NoteIcon, such as NoteIconLock.

Every icon also has a [Description] and [Keywords] that say what it's meant for, so the Skull comes up for death and enemy too. A tool choosing an icon by meaning can list them all in the editor with TypeCache.GetTypesDerivedFrom<NoteIcon>() and read those attributes, which also picks up icons other packages add.

Built-in colors#

The colors live in the VisualScript.Runtime namespace, each named NoteColor followed by its title. Each one returns a ColorTheme.Type from its Color property.

Every ColorTheme.Type has one shade for Unity's dark editor theme and another for the light one, and the editor uses the shade that matches, so each color stays readable in both. Default (NoteColorDefault) is a neutral gray, and it's the color a note uses until you choose another.

Host logic with Runners#

RunnerInstructions adds a designer-authored Instructions list to your component:

using UnityEngine;
using VisualScript.Runtime;

public class MyDoor : MonoBehaviour
{
    [SerializeField] private RunnerInstructions m_OnOpen = new RunnerInstructions();
    [SerializeField] private RunnerConditions m_CanOpen = new RunnerConditions();

    public void TryOpen()
    {
        using Args args = Args.Get(this.gameObject, this.gameObject);

        if (!this.m_CanOpen.Run(args)) return;
        this.m_OnOpen.Run(args).Fire(null, this);
    }
}

The Inspector shows Instructions and Conditions lists on MyDoor. When TryOpen runs, m_CanOpen checks whether opening is allowed; if it returns true, m_OnOpen runs the configured response.

Runner fields keep host components configurable

A hard-coded door has one response. A door with RunnerInstructions can run a sound, camera shake, or quest update without changing MyDoor.

Add a runner at the moments where designers need to attach project-specific behavior.

Running#

The Instructions runner offers overloads for cancellation and instance reuse:

Method Does
Run(args) Runs the Instructions, returning an Awaitable.
Run(args, cancelToken) Same, with an explicit cancellation token.
Run(args, poolId) Same, reusing a pooled runner instance.

RunnerConditions.Run(args) returns a bool and is synchronous. An empty Conditions list returns true.

Pooling runners#

Each execution needs a runner instance. The plain overload creates one, while the pool overload reuses an instance. Add a shared pool ID to MyDoor:

private static readonly IdString POOL_ID = new IdString("my-door");

Then pass it to Run inside TryOpen:

this.m_OnOpen.Run(args, POOL_ID).Fire(null, this);

POOL_ID groups executions that can reuse the same runner allocation.

Use the pooled overload for frequent execution, such as each projectile, hit, or frame. For a door that opens twice a session, the plain overload avoids maintaining a pool ID for infrequent allocations.

Fire#

Fire(onComplete, context) starts an Awaitable without awaiting it, attaching a Unity object as context so the framework can report errors against the right object.

Use Fire when you're starting a sequence from a non-async method and don't need the result. In an async method, you can await Run directly when the next operation depends on completion.

Cancellation#

ICancelToken is the framework's cancellation contract — a single Get property meaning stop.

Pass a token to Run when your own object's lifetime should govern the sequence. Implementing it on your component means the Instructions stop when your component decides they should, in the same way an Event's Instructions stop when it's disabled.

Always give long sequences a token

Instructions started from code with no cancellation token keep running after the object that started them is destroyed. If later Instructions access that object, they can produce a null reference error several frames after its destruction.

Where to go next#

  • Args — build the context runners need.
  • Custom Instructions — write the operations runners execute.
  • Events — inspect Event lifecycle from the designer's perspective.
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.