How to build a BehaviourTree at runtime?

jennal

New member
Hi, I am wondering is there a way to build a BehaviourTree at runtime? So I can bind properties, actions at runtime.
 

Justin

Administrator
Staff member
When you say bind properties and actions do you mean the reflection fields, or something else? While it is possible to create a tree at runtime the API isn't very straight forward because you need to deal with editor code as well for representing the nodes. The best solution is to not build the tree at runtime at all but instead use external trees.
 

Justin

Administrator
Staff member
I got a question about building the tree at runtime. Here's some sample code that will build a tree with one child:

Code:
    void Start()
    {
        behaviorTree = gameObject.AddComponent<BehaviorTree>();
        behaviorTree.StartWhenEnabled = false;

        var entryTask = new EntryTask();
#if UNITY_EDITOR
        entryTask.NodeData = new NodeData();
#endif
        behaviorTree.GetBehaviorSource().EntryTask = entryTask;

        var rootTask = new Sequence();
#if UNITY_EDITOR
        rootTask.NodeData = new NodeData();
#endif
        behaviorTree.GetBehaviorSource().RootTask = rootTask;

        Log log = new Log();
#if UNITY_EDITOR
        log.NodeData = new NodeData();
#endif
        log.logError = new SharedBool();
        rootTask.AddChild(log, 0);
        log.text = "try me!...";

        behaviorTree.GetBehaviorSource().HasSerialized = true;
        behaviorTree.EnableBehavior();
    }
I have also started on the next major Behavior Designer version and I'll make it easier to create trees at runtime.
 

AIGuru

New member
Awesome! Works perfectly :)

I'm investigating how I can evolve new BTs procedurally, and more specifically how to evolve different versions of subtrees and compare them.

Is there any way to save a tree created at runtime in its own ExternalBehaviorTree asset and reuse them later on?

Thanks
 

Justin

Administrator
Staff member
There really isn't a good way to save out the tree. The serialization code is within the editor so you can't rely on that at runtime, and External Trees are ScriptableObjects which require the Unity Editor namespace in order to be created.
 

AIGuru

New member
I see...any plan to support that feature in an upcoming release?

The serialization code is within the editor so you can't rely on that at runtime
Is this something that could be moved in runtime code?

Thanks!
 

Justin

Administrator
Staff member
I see...any plan to support that feature in an upcoming release?

Is this something that could be moved in runtime code?
I have started on the next major version of Behavior Designer and with that I am planning on making some of these edge cases easier to handle. Right now it's not easily doable to move the serialization code out of the editor but I will keep this use case in mind.
 

AIGuru

New member
Hi!

I would like to add breakpoints to a BT created at runtime, but the BehaviorDesigner Editor Window shows something like this :

1548

All BT nodes seemed overlap on top of each other. Anything I can do to fix this?

Thanks,
 
Top