ビヘイビアーツリー外から変数を取得したい

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

ビヘイビアーツリー外から変数を取得したい

Post by namnum3785 »

オブジェクトAのビヘイビアーツリー内にある変数をオブジェクトB内のスクリプトから取得、変更したいです。
(こちらの質問と反対のことをしたいですviewtopic.php?f=3&t=2566

この場合、どうすれば良いでしょうか。

初歩的な質問で申し訳ありませんが、よろしくお願い致します
User avatar
caitsithware
管理人
Posts: 493
Joined: 2015/08/17 12:41

Re: ビヘイビアーツリー外から変数を取得したい

Post by caitsithware »

ビヘイビアツリー内のノード用スクリプトの変数を参照したい、ということですね。

流れは「オブジェクトBのスクリプトからオブジェクトAのスクリプトを参照する」という形ですので
他のオブジェクトの変数の参照と基本的な流れは同じとなります。

例スクリプト : ObjectAAction.cs

Code: Select all

using UnityEngine;
using Arbor.BehaviourTree;

public class ObjectAAction : ActionBehaviour
{
	public int testInt = 0;

	protected override void OnExecute() 
	{
		if (Input.GetKeyDown(KeyCode.Space)) // スペースキー押すたびに1増やすテスト
		{
			testInt++;
		}
	}
}
例スクリプト : ObjectBBehaviour.cs

Code: Select all

using UnityEngine;

public class ObjectBBehaviour : MonoBehaviour
{
	public ObjectAAction objectA;

    void Update()
    {
		int testInt = objectA.testInt;
		Debug.Log(testInt); // testIntの値をコンソールにデバッグ出力してみるテスト。
	}
}
流れ

上記2つのスクリプトがあるとして、
  • オブジェクトAのビヘイビアツリーにObjectAActionを追加。
    ObjectA_BehaviourTree.png
    ObjectA_BehaviourTree.png (19.64 KiB) Viewed 3748 times
  • オブジェクトBにObjectBBehaviourを追加。
  • オブジェクトBのInspectorのobjectAフィールドに、ビヘイビアツリーに追加しておいたObjectAActionをドラッグアンドドロップ。
     ※ArborEditorWindowで表示しているグラフ内からスクリプトのタイトルバー部分(ノード名部分ではなく)をドラッグアンドドロップできます
    ObjectB_Inspector.png
    ObjectB_Inspector.png (20.45 KiB) Viewed 3748 times
※ノード用スクリプトのタイトルバー部分をオブジェクト参照フィールドへドラッグアンドドロップは、StateBehaviourなどの他スクリプトでも可能です(ただし演算ノードはタイトルバー部分がないため非対応)
namnum3785
Posts: 3
Joined: 2021/11/20 13:42

Re: ビヘイビアーツリー外から変数を取得したい

Post by namnum3785 »

丁寧な対応ありがとうございます。
追加の質問で申し訳ないのですが、AgentMoveToTransform内の変数_TargetTransformを参照するにはどうしたらよいでしょうか。

エラー CS0029 型 'UnityEngine.Transform' を 'Arbor.FlexibleTransform' に暗黙的に変換できません

というエラーが出てしまい困っています。
User avatar
caitsithware
管理人
Posts: 493
Joined: 2015/08/17 12:41

Re: ビヘイビアーツリー外から変数を取得したい

Post by caitsithware »

まずAgentMoveToTransformの_TargetTransformはprivateですので外部から参照できません。
publicに書き換えるかReflectionで無理やりに参照することもできますが今後の更新による互換性も考慮するとお勧めはできません。
どちらも自己責任でお願いいたします。
namnum3785 wrote: 2021/11/21 02:14 エラー CS0029 型 'UnityEngine.Transform' を 'Arbor.FlexibleTransform' に暗黙的に変換できません
もしpublicに書き換えたうえで上記のようなエラーが出る場合は、FlexibleTransformのキャストオペレーターかvalueを参考にしてみてください。

具体的には、

Code: Select all

Transform transform = _TargetTransform.value;
もしくは

Code: Select all

Transform transform = (Transform)_TargetTransform;
という形で取得できます。
(逆にFlexibleTransformが参照するTransformを外部スクリプトから書き換えるのは想定外の使用方法ですのでここでは割愛します)

この辺りはC#の基本的な文法に関する部分ですので、一通り基礎的なところも学ばれることをお勧めいたします。
また、ノード内スクリプトの変数を直接参照しなければならない設計で本当に良いのかの見直し(ParameterContainerを介すなど)も一度考えてみた方がよろしいかと思います。
namnum3785
Posts: 3
Joined: 2021/11/20 13:42

Re: ビヘイビアーツリー外から変数を取得したい

Post by namnum3785 »

大変わかりやすい説明ありがとうございました。
今度ともよろしくお願い致します。
Post Reply