Behaviour Tree Exporter

Carousel imageCarousel imageCarousel imageCarousel image
  • Lightweight script made in Python.

  • Able to export .xml-files created in Draw.io to ready-to-use C++ code.

  • Level designers are able to create complex behaviour trees visually without ever writing a line of code themselves.

Python script written in order to facilitate the creation of complex behaviour trees visually in Draw.io (exported in .xml-format) and export them into ready-to-use C++ code. Developed alongside Untitled Gnome Gname and used by our level designers to design the AI behaviour of the trolls.


  • The design side: Using the free-to-use program Draw.io, the designer creates a Behaviour Tree using nodes and edges (for more info on Behaviour Trees: Behaviour Tree Specialisation). The designer is limited to a set of standard nodes such as Sequencers, Selectors, Inverters, Repeaters etc. but can also add completely custom Leaf nodes (for instance "Open Door", "Fire at Target" etc.) as long as they follow the set naming standard of course. When satisfied, the designer then saves the tree and runs the exporter (a single .exe-file).


  • The Python side: The script itself opens up a console asking for file name and where to export the tree. The designer provides this and the script opens the .xml-file. It reads it and for each row checks wheather it can be translated to C++ or not. If not, that row is simply discarded but if it is recognized, a corresponding row is written to a .hpp-file (see the picture on the right). So in essence, we convert a cluttered .xml-file into a neat and tidy .hpp-file.


  • The C++ side: Using my previously created Behaviour Tree, the AI System in our game simply initiates the tree by calling Init() on the macro which represents the entire tree (see the picture on the right). The AI System then updates this macro and often as desired (depending on how often the AI should update for instance) by calling the previously mentioned macro. All standard nodes are already implemented such as Composite nodes and Decorators but if the designers has added any custom Leaves, these show up as errors in the .hpp-file until they have been implemented as well.

This results in the two unique troll behaviours in the game: the troll dad, Torbjorn and the troll child, Sandra. As the player progresses in the story, different Behaviour Trees are unlocked and used in order to match.

Above, the final .hpp-file which creates a Behaviour Tree on the engine side. This is a simple constructor pattern which corresponds to a visual Behaviour Tree in Draw.io. When updated, the trolls in the game tend to their garden, clean up any mess the player makes and chase the player if provoked.

This Leaf makes the troll find, and pick up a watering can. It extends the parent class Leaf in the Behaviour Tree and calls on the more generalized, static method AcquireItem.

The Behaviour Tree Exporter was used by our level designers and was essential in order to create the quite complex AI behaviours. Without it, we would have to create everything hard-coded in the engine and the level designers would have no insight at all. This would take a lot more time and be a lot harder to visualize.

I worked closely with one of the designers and tried to develop and update the exporter according to his wishes as best as I could. This cooperation turned out to be very rewarding and I learned much. As a plus, it was also a lot of fun!