6. Custom actions
6.1 Contract
Syntax
public interface IAction
{
void Execute(DialogueActionContext context);
Task ExecuteAsync(DialogueActionContext context, CancellationToken cancellationToken);
void Cancel();
}
Parameters
| Parameter | Description |
|---|---|
context | execution context of the action |
cancellationToken | cancellation token of the asynchronous execution |
Notes
Execute(DialogueActionContext context) executes the action; ExecuteAsync(DialogueActionContext context, CancellationToken cancellationToken) executes its asynchronous variant; Cancel() requests its cancellation.
6.2 Implementation rules
| Rule | Detail |
|---|---|
| No MonoBehaviour | actions are instantiated by the engine: pure classes are required, with invocation of the application singletons |
| Mandatory attribute | [DialogueAction("Id")] — the Id is the value reported in the Function node |
| Thread | the execution occurs on the Unity main thread |
| Cancellation | Cancel() is called if the dialogue stops or changes node |
Actions are instantiated by the engine: pure classes are required, with invocation of the application singletons. The [DialogueAction("Id")] attribute is mandatory and its Id is the value reported in the Function node.
6.3 Metadata
Example
[DialogueAction("OpenDoor",
Description = "Opens a door and waits for the animation.",
RequiredParameters = new[] { "doorId" },
OptionalParameters = new[] { "delay" },
WaitsForCompletion = true)]
public sealed class OpenDoorAction : IAction
{
private CancellationTokenSource _cts;
public void Execute(DialogueActionContext ctx)
{
string doorId = ctx.GetParameter("doorId");
float delay = ctx.GetFloat("delay", 0f);
DoorSystem.Open(doorId, delay, () =>
{
ctx.SetResult(true);
ctx.Complete();
});
}
public async Task ExecuteAsync(DialogueActionContext ctx, CancellationToken ct)
{
_cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
await DoorSystem.OpenAsync(ctx.GetParameter("doorId"), _cts.Token);
ctx.SetResult(true);
}
public void Cancel() => _cts?.Cancel();
}
The parameters declared in the metadata are retrieved from the context by means of ctx.GetParameter("doorId") and ctx.GetFloat("delay", 0f).
6.4 Attribute flags
| Attribute flag | Effect |
|---|---|
WaitsForCompletion | the node is marked as "waiting" even without wait in the graph |
CompletesExternally | the function remains open: the game closes it with Dialogue.Complete(...) |
RequiredParameters / OptionalParameters | used by validation and shown in the editor |
6.5 Function closed by the game (without waiting in the node)
// in the graph: Function with Action = ExternalResult, Wait for completion = on
StartCoroutine(LoadLevelThenResume());
IEnumerator LoadLevelThenResume()
{
yield return SceneManager.LoadSceneAsync("Level2");
Dialogue.Complete(true);
}
6.6 Manual registration of an action
Example
ActionRegistry.RegisterAction("Ping", () => new PingAction(),
new ActionRegistry.ActionInfo { Description = "Test ping" });
Manual registration is indicated when the actions cannot reside in the engine assembly, due to distinct assembly definitions.
7. Variables
7.1 Static facade
Example
Dialogue.SetBool("has_key", true);
Dialogue.SetInt("coins", 12);
Dialogue.AddInt("coins", -3);
Dialogue.SetFloat("volume", 0.8f);
Dialogue.SetString("player_name", "Anna");
bool ok = Dialogue.GetBool("has_key");
int c = Dialogue.GetInt("coins", 0);
float v = Dialogue.GetFloat("volume", 1f);
string n = Dialogue.GetString("player_name", "?");
The write forms are SetBool, SetInt, AddInt, SetFloat and SetString; the read forms are GetBool, GetInt, GetFloat and GetString. The read forms accept a default value returned in the absence of the variable (0 for GetInt, 1f for GetFloat, "?" for GetString).
7.2 Evaluation of an expression
The evaluation of an expression occurs with the same grammar as the Condition nodes:
bool found = Dialogue.Evaluate("quest.rescue.state == active && coins >= 3");
7.3 DialogueVariables service
Example
DialogueVariables v = Dialogue.Variables;
v.OnChanged += (name, value) => Debug.Log($"{name} = {value}");
v.Declare(new VariableDeclaration { name = "hp", type = "int", value = "100" });
v.SetInt("hp", 80);
v.AddInt("hp", -10);
bool exists = v.Has("hp");
bool declared = v.IsDeclared("hp");
string snapshot = v.Serialize();
v.Deserialize(snapshot);
Notes
Dialogue.Variablesexposes theDialogueVariablesservice.
OnChangedis the change event, with payload(name, value).
Declare(new VariableDeclaration { name = "hp", type = "int", value = "100" })declares a variable with type and initial value.
SetInt,AddInt,Has,IsDeclared,SerializeandDeserializeoperate on the indicated variable.