他のオブジェクトの変数の参照

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を確認の上、それでも使い方がわからない場合にご質問ください。欲しい機能の記載がドキュメントにない場合は機能が元から存在しない可能性が高いので要望フォーラムへ。
Guest

他のオブジェクトの変数の参照

Post by Guest »

初歩的な質問ですみません。

オブジェクトAにアタッチされたC#スクリプトのpublic変数をオブジェクトBのFSMから参照したいのですが、どのような方法がありますか?

例えばオブジェクトAのC#スクリプトにpublic int testInt;があり、Update()毎に内容が変化するとして、
オブジェクトBのFSM内でそのtestIntの内容によって挙動を変化させたいというようなケースです。

よろしくお願いします。
User avatar
caitsithware
管理人
Posts: 493
Joined: 2015/08/17 12:41

Re: 他のオブジェクトの変数の参照

Post by caitsithware »

メンバー変数への参照方法についてですね。

主に以下のような流れで参照することになります。
  • ArborFSMで使用できるスクリプト(StateBehaviourかCalculator)を自作しオブジェクトAに追加しているスクリプトのインスタンスを参照。

    Code: Select all

    public class ObjectBStateBehaviour : StateBehaviour
    {
        public ObjectABehaviour objectA; // ObjectABehaviour型のメンバー変数を宣言。
    }
    
  • インスタンスからメンバー変数の値を取得。

    Code: Select all

    public override void OnStateUpdate()
    {
        int testInt = objectA.testInt;
        Debug.Log(testInt); // testIntの値をコンソールにデバッグ出力してみるテスト。
    }
    
  • 値を使ってステートを遷移するなどなど

    Code: Select all

    public StateLink nextState; // StateLink型のメンバー変数を宣言
    
    public override void OnStateUpdate()
    {
        int testInt = objectA.testInt;
        if( testInt >= 10 ) // testIntを比較してtrueなら遷移
        {
            Debug.Log("testIntが10以上なので遷移します : testInt "  + testInt);
            Transition(nextState);
        }
    }
    
  • 例コード(ObjectABehaviour.cs)全文

    Code: Select all

    using UnityEngine;
    
    public class ObjectABehaviour : MonoBehaviour
    {
    	public int testInt = 0;
    	
    	void Update ()
    	{
    		if (Input.GetKeyDown(KeyCode.Space)) // スペースキー押すたびに1増やすテスト
    		{
    			testInt++;
    		}	
    	}
    }
    
  • 例コード(ObjectBStateBehaviour.cs)全文

    Code: Select all

    using UnityEngine;
    using Arbor;
    
    [AddComponentMenu("")]
    public class ObjectBStateBehaviour : StateBehaviour
    {
    	public ObjectABehaviour objectA; // ObjectABehaviour型のメンバー変数を宣言。
    	public StateLink nextState; // StateLink型のメンバー変数を宣言
    
    	public override void OnStateUpdate()
    	{
    		int testInt = objectA.testInt;
    		if (testInt >= 10) // testIntを比較してtrueなら遷移
    		{
    			Debug.Log("testIntが10以上なので遷移します : testInt " + testInt);
    			Transition(nextState); // 遷移
    		}
    	}
    }
    
    例として挙げたコードについては参考程度に、適宜、型名や処理内容は変えてください。
  • ArborEditorウィンドウでFSMのステートに自作した挙動を追加。
  • 追加した挙動のオブジェクトAインスタンスへの参照を設定。
  • 遷移先などを設定。
    ObjectBFSM.png
    ObjectBFSM.png (43.47 KiB) Viewed 3771 times
Arbor独自の型やメソッド(今回の例ではStateBehaviourやOnStateUpdate、StateLinkやTransitionなど)以外は
UnityC#の基本的な書き方になりますので、一度UnityC#入門の書籍などで学ばれることをお勧めいたします。
Guest

Re: 他のオブジェクトの変数の参照

Post by Guest »

とても分かりやすい解説で、なんとか理解できました。
狙い通りの挙動ができて感動しております。
ありがとうございました。
Post Reply