Creating Shared Variables
Create a custom SharedVariable<T> when a task should expose a value type that
the built-in shared variables do not cover.
Define the shared type
The wrapper must be serializable and derive from SharedVariable<T>. An
implicit conversion keeps task declarations and assignments concise.
using BehaviorDesigner.Runtime;
using System;
using UnityEngine;
[Serializable]
public class TargetInfo
{
public GameObject Target;
public float LastSeenTime;
}
[Serializable]
public class SharedTargetInfo : SharedVariable<TargetInfo>
{
public static implicit operator SharedTargetInfo(TargetInfo value)
{
return new SharedTargetInfo { Value = value };
}
}
After Unity compiles, SharedTargetInfo can be used as a public task field and
selected in the Variables panel.
Use it in a task
using BehaviorDesigner.Runtime.Tasks;
public class HasTargetInfo : Conditional
{
public SharedTargetInfo TargetInfo;
public override TaskStatus OnUpdate()
{
return TargetInfo.Value?.Target != null
? TaskStatus.Success
: TaskStatus.Failure;
}
}
Keep the wrapper and its value type in runtime assemblies. If the type needs a
custom Inspector, place the corresponding Object Drawer
in an Editor folder or editor-only assembly.
Verify serialization
Create a variable of the new type, enter a value, save the scene or external
tree, and reopen it. If the value is missing or the type does not appear, check
that both classes are serializable and that the wrapper derives from the exact
closed SharedVariable<T> type.