Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Tree.ReflectClassForwards (GameObject) and Tree.SetForwards (int id, IActionClass actionClass). This is now built-in per Behave 2.6.
using UnityEngine;
using Behave.Runtime;
using Library = BLYourLibraryName;
using Tree = Behave.Runtime.Tree;
public interface IActionClass
{
bool OnForward (Tree sender);
BehaveResult Init (Tree sender);
BehaveResult Tick (Tree sender);
void Reset (Tree sender);
}
public static class BehaveExtensions
{
static readonly string s_IActionTypeName = typeof (IActionClass).Name;
const string kActionClassPostfix = "Action";
public static void ReflectClassForwards (this Tree tree, GameObject gameObject)
{
foreach (MonoBehaviour behaviour in gameObject.GetComponents<MonoBehaviour> ())
{
System.Type type = behaviour.GetType ();
if (type.GetInterface (s_IActionTypeName) == null)
{
continue;
}
string name = type.Name;
int actionIndex = name.IndexOf (kActionClassPostfix);
if (actionIndex < 1)
{
Debug.LogError (string.Format (
"Bad {0} type name (format is \"[Name]{1}\"): {2}",
s_IActionTypeName,
kActionClassPostfix,
name
));
continue;
}
name = name.Substring (0, actionIndex);
int id = 0;
try
{
id = (int)System.Enum.Parse (typeof (Library.ActionType), name);
}
catch
{
Debug.LogError ("Unknown action type: " + name);
continue;
}
tree.SetForwards (id, (IActionClass)behaviour);
}
}
public static bool SetForwards (this Tree tree, int id, IActionClass actionClass)
{
// TODO: When Tree.ActionIDs has been implemented, we should first check if tree.ActionIDs contains id - returning false if not
if (!actionClass.OnForward (tree))
{
return false;
}
tree.SetInitForward (id, actionClass.Init);
tree.SetTickForward (id, actionClass.Tick);
tree.SetResetForward (id, actionClass.Reset);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment