MySys22.DialogueSystem
ドキュメント
1.0
エンジン
0.5 BETA
エディタ
0.5 BETA
更新
2026-09-28

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);

注記