How I do a simple Turn Based AI

I’ve been working really hard on a turn based RPG project, and we needed a simple AI solution to allow us to easily create different behaviours for different monsters.

Eventually I settled on creating a sort of swappable AI brain that we could attach to any AI character.

The way the AI brain works is pretty simple! What I’m most proud of is the extremely modular and extensible nature of the AI system I created.

Screen Shot 2016-03-23 at 4.39.24 PM.png

This is essentially the base for all the AI functions.

The way the AI works is very simple at a basic level. The GetMove function does all the hard work. I pass in the current BattleSystem, which is the encapsulation of a single battle—it holds all the information necessary to hold a battle. That is, all the party members in the fight, the special modifiers on the party members, where they’re fighting, and more.

With this simple structure, I was able to create basic AI very easily. An AI that uses a skill randomly against a random opponent was trivial.

Screen Shot 2016-03-23 at 4.44.33 PM.png

It’s so easy!

It feels so good to be able to create such a thing so quickly! I created a bunch of different ones for basic use in the game. A attacker that always attacks the lowest HP enemy, an attacker that attacked a specific class more frequently, a healer that supports its teammates… it was all very simple to create! I could add as difficult or simple logic into the GetCommand function as I wanted!

The fact that my AI brain architecture was so easily extended made more likely to play around with it. Eventually we wanted a sort of AI that could do multiple things. We wanted an AI to heal friendlies if they were low, revive friendlies if they were dead, and then dispel friendlies and enemies based on the context.

I could have written a huge AI class just like I created the SingleAttackAIRandom class, but I realized that I already had all the little parts to this semi-complex AI already! So then after much pondering I started creating a new AvAI class… that holds AvAIs!

Screen Shot 2016-03-23 at 4.55.14 PM.png

A little more complex, but still a very small, readable class.

This ConditionalAI is very simple! This AI merely holds a bunch of AvAIModules, which are wrappers for AvAIs that also hold a conditional. The module merely nullifies the wrapped AI’s output if the requisite conditionals aren’t met. Whenever the ConditionalAI is asked to return a move, it starts asking at each AI module in order. If the module returns null, then the move has been nullified by the conditional, meaning the condition wasn’t satisfied. If one of the modules is satisfied, then it will return a legitimate move, and the ConditionalAI will just spit that back out!

Screen Shot 2016-03-23 at 5.01.57 PM.png

This is fancy to me.

The result of this isn’t crazy cool or amazingly groundbreaking, and might be very obvious to some people, but it was very neat to me to be able to write AI by snapping AI blocks together. It makes it easy to program a huge variety of different behaviours, each getting more and more complex! I’m pretty proud of myself for making such a cool, extensible, and easy to program AI system for this game.

Drawing custom Gizmos in Unity

Screen Shot 2016-02-06 at 8.56.16 PM.png

I will avoid making a custom Editor script by any means necessary.

Did you know you can easily draw your own Gizmos for the scene view using a simple magic method? I sure didn’t. I had to create a tree visualization structure for the editor for the game I’m working on, and I was dreading the day I would have to get into Editor scripting just for this tiny thing.

Luckily, I don’t have to!

The magic method void OnDrawGizmos() and void OnDrawGizmosSelected() is all I needed to get this effect. OnDrawGizmos() is like an Update() function for when you’re inside the scene view. The “selected” variant is the same thing, except that it’ll only run if the object is selected in the editor.

In my OnDrawGizmos() function, all I did was loop through all child nodes and draw 9 lines for each. Why 9? Well, one line is to connect the two, and the 8 remaining lines are to draw the little arrow thing. There’s a way to use Handles.ArrowCap() to do the arrow drawing, but I like the wireframe look.

In the sample code below, you can see the rough structure (and a good one) to handle the drawing you see above. The DrawConnectedGizmos() function is nicely separated—all that does is do the maths for the arrow drawing. I used a very helpful script for the arrow drawing here.

Screen Shot 2016-02-06 at 9.02.07 PM.png

I put this as an image so you can’t copy and paste it.

For the text on each GameObject, I used the Handles.Label(Vector3, string) method. It’s rather straightforward. Just put the place you want the text to appear as the first argument, and the text you want to display in the second.

Once you finish, you’ll gasp at the results! Or at least I will.