Simple Behaviour Trees for your game in Javascript and C#

A little while ago I tried to understand what Behaviour Trees are about, as in a couple of games I’m developing I have sets of NPCs that I have to manage and I would like to somehow decouple their behaviour from the main game flow. Here you can find a good explanation of behaviour trees.

Sample behaviour tree

Sample behaviour tree

Got this, I searched for implementations (I wanted both Javascript and C# implementations) and I didn’t like them because of the amount of code and classes required. Then on StackOverflow I found this very simple example:

Stack Overflow simple behaviour trees

Encouraged by this,  with Matteo Bicocchi we started writing our own implementations, as I simply could not believe that the code needed to be so complex 😉 . We first wrote the Javascript implementation, and the two main points we had to deal with have been:

1. Distinguish between the tree description and the instance tree being run: you need a concrete instance as you need to be independent of “tick” speed and need to be able for each instance to find what is the current node.

2. You need to handle single node completion asynchronously e.g. if you have a “chase” node, this may take a long time to resolve.

Now we have our implementation, its very simple code and can be useful in a wide variety of game development situations – surely its not a complete solution but it works fine.

Here is a simple Js Fiddle example you can play with directly in the browser.

Fiddle with behaviour trees

and here are full sources in both Javascript and C# on GitHub. The C# code is a simple port from Javascript, surely it could be refined (branch it!). It also contains two classes making the C# example run, its made with a for Unity (extends MonoBehaviour) but the code can be used anywhere.

The code is mostly commented in the Javascript version.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied games.

Social Share Toolbar

Indie Development: Have Full Versioned Backups at Zero Cost

There is a simple way to have complete backups of all your sources versions, at no cost; it’s the kind of solution a single, destructured, minimal budget indie developer may use. This without any server or complex setup: just use a couple of tricks. I suppose here you are developing on Windows.

What does not work

The simplest way one can think is simply to set up your working folder as a folder in Dropbox (or any similar service), as that syncs online you’ll be fine. For versioning, Dropbox actually does version you files.

Now this can fail disastrously, because:

1. If you want to recover work that happened yesterday before you took the wrong path in your web application using this library that does not work, or before you imported that non compatible plugin in your Unity project, you don’t need to recover just one file: you need t recover a situation, with maybe hundreds of files to be recovered and Dropbox simply does not do that: history is at single file level, not groups of files (e.g. folders).

2. Working in Dropbox continuously modifying files means a lot of syncing going on, and your IDE may not always be happy about that, you are maximizing risk of source / project corruption, and as you cannot recover whole folders, you risk losing everything.

The TortoiseSVN menu for unversioned foldersA simple way that works

What would be cool would be to work outside the syncing folder and commit to a synced versioning server. And we also would like to have complete backups of the versioning server in the form of a single file so that we also have a history of that in Dropbox. We’ll get all this.

First notice (as once pointed out to me by Daniele Giardini) that with TortoiseSVN you can create a Subversion repository just as a local folder without installing any server; so you can setup your project to commit to a local folder based Subversion repository. Just do “Create repository here”,  see here for setup details. This is the key point; the only difference with a server is that you access this local repository with urls like file:///C:/SVNRepository/.

Create that folder repository in Dropbox, and each time you commit the Subversion repository will get your files and these will be backed up online. Still you are not safe, as any little file that gets corrupted in the Subversion repository folder will break all your versions, and as Dropbox keeps only the history of single files, you will have no way to recover a coherent working repository of all your versions. For that, just schedule a daily Windows job to zip and backup your repository as a zip file in your Dropbox. From that file you can recover all your versions. Done Sorriso

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied games.

Social Share Toolbar