Behavior trees

03Screen
The initial behavior tree for employees. “Work” consists of staring at a screen until “GoHome”.

For Software Inc. I decided to learn a bit about implementing AI, so I read a couple of paragraphs, got impatient and started coding stuff, because I am not smart and, apparently, I like messing things up I could’ve otherwise avoided by just studying a bit.

Normally when I implement AI I end up with a 1000 lines of IF-THEN-ELSE and switch case statements. Behavior trees seemed like a cool alternative, so I went looking for implementations, because when it comes to programming, NEVER reinvent the wheel, so after seeing something to the tune of “200$”, I decided to reinvent the wheel… Poorly… And share it with you, of course, for free(Requires .NET 4.5, only works with C# files, simply select a cs file and the program will generate two new partial classes, one of which (class.impl.cs) contains the methods you need to fill in. It will not overwrite previous state methods when you export on top of it. BACKUP BEFORE USING).

So I made a small application in which I can visually draw states and connect them, and when I am done, I simply choose a C# file with my class in and it will generate all the method stubs and the code to generate the behavior tree, as seen in the picture below.

04Screen
Example of the code generated to construct the behavior tree seen earlier.
09Screen
How the behavior tree looks now. Notice “WantCoffee”, this game is going to be so stereotyped. I don’t even drink coffee…

One thought on “Behavior trees”

  1. Just wanted to say that I sympathize with the AI thing. My first game has a boss battle with 3 stages; the code looked like I had fallen asleep in logic class. Stupidly I decided to write the boss as three separate entities (with no central controlling unit) that do what seemed like choreographed dancing so that only took 12 hours to code. #FirstCodingExperience

Comments are closed.