現在のステートを復元する方法はありますか?

Here is the forum to do the questions about how to use to Arbor developer.
Attention point:
  • We can not answer your questions about your project specific issues.
  • We can not answer your questions on Unity's specification issues.
  • Please check Arbor Documentation and ask a question if you still don't know how to use it. If the desired function is not described in the document, it is highly possible that the function does not exist from the beginning, so go to the request forum.

ここは、Arbor開発者へ使い方に関する質問を行うフォーラムです。
注意点:
  • ユーザー様のプロジェクトの仕様上の問題や設計に対する質問には答えられません。
  • Unityの仕様上の問題に対する質問には答えられません。
  • Arbor Documentationを確認の上、それでも使い方がわからない場合にご質問ください。欲しい機能の記載がドキュメントにない場合は機能が元から存在しない可能性が高いので要望フォーラムへ。

Forum rules
Here is the forum to do the questions about how to use to Arbor developer.
Attention point:
  • We can not answer your questions about your project specific issues.
  • We can not answer your questions on Unity's specification issues.
  • Please check Arbor Documentation and ask a question if you still don't know how to use it. If the desired function is not described in the document, it is highly possible that the function does not exist from the beginning, so go to the request forum.

ここは、Arbor開発者へ使い方に関する質問を行うフォーラムです。
注意点:
  • ユーザー様のプロジェクトの仕様上の問題や設計に対する質問には答えられません。
  • Unityの仕様上の問題に対する質問には答えられません。
  • Arbor Documentationを確認の上、それでも使い方がわからない場合にご質問ください。欲しい機能の記載がドキュメントにない場合は機能が元から存在しない可能性が高いので要望フォーラムへ。
hildsoft

現在のステートを復元する方法はありますか?

Post by hildsoft »

グラフにいくつかのステートがある状態で、一旦ゲームを中断するときに任意のステート(中断前のステート)にしたいのですが、標準でシリアライズする方法はありますか?

開始ステート -> A <-> B
の3つのステート
・開始ステート:初期処理(終了後すぐにAに移動)
・A:通常状態
・B:特定の処理を実行している状態
があるとします。

ゲーム中にAからBに遷移したときに中断して保存した場合、
シリアライズで復元するときは、開始ステートを実行後にBの状態でゲームを再開したいのです。

Transitionを利用して開始ステートで直接Bに遷移することはできるのですが、数が増えてくるとノードを接続するのも大変なので何か方法があったら教えていただけると嬉しいです。
User avatar
caitsithware
管理人
Posts: 493
Joined: 2015/08/17 12:41

Re: 現在のステートを復元する方法はありますか?

Post by caitsithware »

ご質問ありがとうございます。

ゲームを再開する時は、開始ステート→ステートBと遷移させたい、ということですね。

その場合は、次の手順で実現できるかと思います。
  • 中断時にstateMachine.currentState.nodeIDと中断しているフラグ等を保存。
  • 再開時に開始ステートで中断フラグをチェックし、
    中断ならstateMachine.Transition(保存していたnodeID)で遷移。
    中断でなければStateLinkを用いて通常遷移(ステートAに遷移)
コードにすると以下のような感じです。
(コード例についてはあくまで例であり細かな動作保証までは致しかねるため、参考程度とお考え下さい。)

Code: Select all

// どこからか中断するときに呼ばれる保存メソッド(仮にAbortStoreクラス。シングルトン的な存在)
public void Save()
{
    abortSaveFlag = true;
    currentStateID = arborFSM.currentState.nodeID;
}

// 開始ステートにアタッチする再開StateBehaviour(RestoreTransition.cs)
public class RestoreTransition : StateBehaviour
{
    // 通常遷移先
    public StateLInk normalLink;
    
    // 仮にOnStateBeginで書いていますが、適宜遷移させたいタイミングにしてください。
    public override void OnStateBegin()
    {
        if( AbortStore.instance.abortSaveFlag )
        {
            Transition(AbortStore.instance.currentStateID);
            AbortStore.instance.abortSaveFlag = false;
        }
        else
        {
            Transition(normalLink);
        }
    }
}
CalculatorSlotを使用している場合はCalculatorBranchも復元する必要があるかもしれません。
CalculatorBranchは以下のようなコードで保存/復元できます。

Code: Select all

// CalculatorBranchを保存するクラス
public class StoreCalculatorBranch
{
    public int branchID;
    public object value;

    public StoreCalculatorBranch(int id,object v)
    {
        branchID = id;
        value = v;
    }
}

// どこからか中断するときに呼ばれる保存メソッド。CalculatorBranch用
public void SaveBranch()
{
    for(int i = 0; i < arborFSM.calculatorBranchCount; ++i)
    {
        CalculatorBranch branch = arborFSM.GetCalculatorBranchFromIndex(i);
        if(branch.isUsed)
        {
            AddStoreCalculatorBranch( new StoreCalculatorBranch(branch.branchID,branch.value) );
        }
    }
}

// CalculatorBranchの復元メソッド
public void RestoreBranch()
{
    for(int i = 0; i < abortCalculatorBranchCount; ++i )
    {
        StoreCalculatorBranch storeBranch = GetStoreCalculatorBranch(i);
        CalculatorBranch branch = arborFSM.GetCalculatorBranchFromID(storeBranch.branchID);
        branch.value = storeBranch.value;
     }
}
ファイル等への保存方法につきましてはサポート外となりますので省いてあります。
valueにGameObjectやComponentが入っている場合もありますので、通常の方法では保存/復元できない点はご注意ください。

同様にParameterContainerを使用している場合も、必要に応じて保存してください。
コード例についてはあまり長くなりすぎるのもどうかと思いますので、今回は省きます。
必要であればお申し付けください。

以上となります。
hildsoft

Re: 現在のステートを復元する方法はありますか?

Post by hildsoft »

ご丁寧にサンプルコードまで記載いしていただきありがとうございます。

問題解決のために十分なコードです。
サンプルコードの意図は理解できたので、環境に合うように調整して使用させていただきます。
Post Reply