Article
files/QUESTIONS

Finite State Machine Attacks Harmless Developer!

Alex J. Champandard on September 28, 2007

Since there were no questions about game AI this week, I’m going to ask myself one! Spare me the embarrassment of doing this next week by contacting me with your queries.

An obviously astute reader (hehe :-) asks “Why are you so harsh with finite state machines? Don’t game developers use them all the time?”

Facing the Facts

Finite state machines (FSMs) certainly have their advantages — like any other technology. Specifically, they are:

  1. Simple to implement

  2. Easy to visualize

At the same time, they’re nowhere near perfect. Finite state machines:

  1. Scale poorly to handle complex logic,

  2. Aren’t designed to deal with concurrency.

Now hierarchical FSMs (HFSM) help in this matter, but I put them in the same boat as flat FSMs.

What Do Game Developers Really Use?

If a game developer comes up to me and says:

“I intend to use a (hierarchical) FSM to implement the AI of our game.”

I will most likely ask:

“Do you plan to edit each and every transition between low-level behaviors manually?”

If they say yes, then they’ll end up with a very simple AI system, either because that’s what they wanted or because that’s all they could implement with their FSM. If they say no, then they probably have a hybrid behavior tree (BT) without knowing it — which is a good thing.

A Healthy Compromise

You can implement any finite state machine as a behavior tree, and over 90% of the time, you’ll be better off for doing so. But in the remaining situations, FSM are much more intuitive and solve the problem easier from a coding point of view.

While preparing this Sunday’s sketches, I had an epiphany which helped me put my finger on exactly how both these solutions complement each other.

FSM are best for handling exceptions and special cases.
“An enemy just appeared. Forget what you’re doing and attack.”
BT are great for following a plan with simple fallbacks.
“Attack from cover: hide, reload, throw grenade. If cover becomes invalid, stand, aim at the nearest target, and shoot.”

Now of course you can use both FSM or BT for almost any problem, but it helps to know their strengths. If anything, I hope this post prevents you from considering implementing your logic as bunch of special case transitions!

Anyway, it’s certainly a deep topic, and I will write more about how to combine the two together in future articles on AiGameDev.com.

Discussion 0 Comments

If you'd like to add a comment or question on this page, simply log-in to the site. You can create an account from the sign-up page if necessary... It takes less than a minute!