6. カスタムアクション
6.1 コントラクト
構文
public interface IAction
{
void Execute(DialogueActionContext context);
Task ExecuteAsync(DialogueActionContext context, CancellationToken cancellationToken);
void Cancel();
}
パラメータ
| パラメータ | 説明 |
|---|---|
context | アクションの実行コンテキスト |
cancellationToken | 非同期実行のキャンセルトークン |
注記
Execute(DialogueActionContext context) はアクションを実行します。ExecuteAsync(DialogueActionContext context, CancellationToken cancellationToken) はその非同期版を実行します。Cancel() はそのキャンセルを要求します。
6.2 実装規則
| 規則 | 詳細 |
|---|---|
| MonoBehaviour の禁止 | アクションはエンジンによってインスタンス化されるため、純粋なクラスが必要であり、アプリケーションのシングルトンを呼び出します |
| 必須の属性 | [DialogueAction("Id")] — Id は Function ノードに記載されている値です |
| スレッド | 実行は Unity のメインスレッドで行われます |
| キャンセル | ダイアログが停止した場合、またはノードが変わった場合に Cancel() が呼び出されます |
アクションはエンジンによってインスタンス化されるため、純粋なクラスが必要であり、アプリケーションのシングルトンを呼び出します。属性 [DialogueAction("Id")] は必須であり、その Id は Function ノードに記載されている値です。
6.3 メタデータ
例
[DialogueAction("OpenDoor",
Description = "ドアを開き、アニメーションを待機します。",
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();
}
メタデータで宣言されたパラメータは、ctx.GetParameter("doorId") および ctx.GetFloat("delay", 0f) によってコンテキストから取得されます。
6.4 属性のフラグ
| 属性のフラグ | 効果 |
|---|---|
WaitsForCompletion | グラフに wait がなくても、ノードは「待機中」としてマークされます |
CompletesExternally | 関数は開いたままになります。ゲームが Dialogue.Complete(...) で終了させます |
RequiredParameters / OptionalParameters | 検証で使用され、エディタに表示されます |
6.5 ゲームが終了させる関数(ノードで待機しない場合)
// グラフ内: Action = ExternalResult、Wait for completion = on の Function
StartCoroutine(LoadLevelThenResume());
IEnumerator LoadLevelThenResume()
{
yield return SceneManager.LoadSceneAsync("Level2");
Dialogue.Complete(true);
}
6.6 アクションの手動登録
例
ActionRegistry.RegisterAction("Ping", () => new PingAction(),
new ActionRegistry.ActionInfo { Description = "テスト用 Ping" });
手動登録は、別々のアセンブリ定義のためアクションをエンジンのアセンブリに配置できない場合に適しています。
7. 変数
7.1 静的なファサード
例
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", "?");
書き込み形式は SetBool、SetInt、AddInt、SetFloat、SetString です。読み取り形式は GetBool、GetInt、GetFloat、GetString です。読み取り形式は、変数が存在しない場合に返される既定値を受け取ります(GetInt は 0、GetFloat は 1f、GetString は "?")。
7.2 式の評価
式の評価は、Condition ノードと同じ文法で行います。
bool found = Dialogue.Evaluate("quest.rescue.state == active && coins >= 3");
7.3 DialogueVariables サービス
例
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);
注記
Dialogue.VariablesはDialogueVariablesサービスを公開します。
OnChangedは変化のイベントであり、ペイロードは(name, value)です。
Declare(new VariableDeclaration { name = "hp", type = "int", value = "100" })は、型と初期値を持つ変数を宣言します。
SetInt、AddInt、Has、IsDeclared、Serialize、Deserializeは、指定された変数を操作します。