How a video game character decides: states, trees and scores
Video game AI without learning: state machine, behavior tree, utility as in The Sims, and GOAP. AI course, 0.9.

AI from the First Bit › Level 0 — How a machine sees the world part 9 of 9
Course modules
- 1Everything is a number (yes, even your name)
- 2An image is a table of numbers
- 3A corrupted bit: how to fix it without knowing which one it is
- 4What is a model (and why a straight line already counts as one)
- 5Finding a path: an AI that learns nothing
- 6Playing against a machine: minimax and alpha-beta pruning
- 7How far a machine looks: the horizon effect and deciding with less
- 8Deciding by playing at random: Monte Carlo and Hex
- 9How a video game character decides: states, trees and scores
The characters in a video game seem to think: they get hungry, they get tired, they run away or they look for you. Almost none of them learns anything. They follow rules that someone wrote. What changes from one game to another is how those rules are organized, and that decides whether the character seems intelligent or gets stuck and seems glitched.
Try this first
Open the video game AI tool on “The house”. It is a house with a fridge, a bed and a shower, and a character with three needs that grow on their own: hunger, sleep and hygiene. Each bar has three marks: the black one is the threshold (50), where the character starts to attend to that need; the green one, where it considers the need dealt with and stops; and the red one, urgent, which makes it drop whatever it is doing. Click Play.
Above the character, a speech bubble says what it is doing, and above each object, the need it attends to with its value (this is the “Decision” layer of the “See what the AI is thinking” row, which is on by default). Below the bars, a strip of colors records what it has done at each moment (in the tool, each moment is called a tick). And on the right is its brain: below the list of the four brains, “It is in wander”, the drawing of its states with their 12 arrows and the three it can take now: “→ eat if Hunger > 50 (13), → sleep if Sleep > 50 (39), → shower if Hygiene > 50 (18)”.
Now open the same moment with the utility brain. The house and the seed are the same, but this time the character is at the fridge and the speech bubble says “eat · 0.87”. Above the bed and the shower, the labels no longer say the need but the score of each action: “sleep 0.00”, “shower 0.26” (the fridge has no label: the character’s speech bubble says it, because the character is standing there). On the right there are no longer arrows but one curve per action, with a dot on the need it decided with, before acting in that tick (the bar below shows the current one).
What just happened
The tool has four brains for the same character. All four know the same about the house; only how they decide changes.
State machine. The character is always in one state, which is an action: wander, eat, sleep or shower. From each state, arrows with a condition come out, and the first one that holds takes it to another state. It is the easiest to read: the tool draws it as it is. The problem comes when more states are added. Turn on Add fun (the sofa with the TV) and look at the drawing: a new need forces you to draw arrows from the states that were already there to the new one, and from the new one to all the others. The table “What each brain needs to add for fun” sums it up for all four.
Behavior tree. At each tick, a list of questions is walked from the top, and the first one that holds wins: “Is any need urgent? → deal with it”, “Was it doing something that has not gone down yet? → keep going”, “Is any above the threshold? → the highest one”, “Otherwise, wander”. A new need does not touch the branches that were already there. Behavior trees were widely used in games such as Halo, BioShock and Spore (Behavior tree, Wikipedia).
Utility. Each action is scored with a curve of its need, and the one with the most points wins. The shape of the curve is decided by whoever designs the game: the hunger one rises slowly and then quickly; the sleep one barely rises until it suddenly shoots up. This is the idea of The Sims (2000), where, in addition, the objects in the house advertise what they give. Its author, Will Wright, explained it like this: “every object in the environment is sending out an ‘advertisement’ of pheromones in a particular flavor”, and each flavor is one of the Sims’ eight basic needs (Engadget, 2010). A new object brings its advertisement, and the character uses it without anyone touching its brain.
GOAP (planning). The fourth brain does not choose an action but a chain: it looks for the cheapest sequence of actions that brings the most urgent need below the threshold. It is a search like the one in Finding a path, but between situations of the house instead of between cells. F.E.A.R., from 2005, was the first video game to use it for its enemies (F.E.A.R., Wikipedia).
Go back to the house with the factory values and open Compare the four brains: the same character, in the same house, with the same seed and the four brains advancing together for 500 ticks, each one in its own small house. None has an accident, which is what the tool counts when a need reaches 100 without the character attending to it: it falls asleep on the floor or eats the first thing it finds.
Planning a mission. In the house, GOAP is barely distinguishable from the others: the plans have one or two steps. It shows when the goal calls for a long chain. Open “The dungeon”: a hero has to take a key, open a door, reach the chest and leave, with two guards on patrol. Each step has a cost: walking costs the length of the path, plus a surcharge for each cell a guard watches, and fighting a guard costs whatever the Cost of fighting slider says. On the right, the cheapest plan it has found. With the factory cost, 30, it fights both guards: the plan costs 131. With fighting at 200, the plan changes entirely: it goes around both by a longer path (82 steps to the chest and 57 to the exit), which costs 151.
Nobody wrote those two sequences. The planner finds them with a search like the A* of Finding a path, but its cells are situations: where the hero is, whether it has the key, whether the door is open. And, like A*, it goes straight to what looks promising: 28 world states for the fighting plan and 11 for the one that goes around. And since the plan comes from a search, it can be repeated when something changes. Advance to tick 14: the guards patrol and the plan is already different, with no fight with guard 1: “go to guard 2 → fight guard 2 → go to the chest”. The panel explains it: “last plan: the world changed”. The yellow line is the Path layer, one of those that are on by default in the “See what the AI is thinking” row, above the map: the route it is going to take to guard 2. Nobody has seen it yet. At tick 78 it leaves with the treasure, after 12 changes of plan: along the way it has been seen 5 times and caught once.
The guards, on the other hand, run a state machine, and the tool draws it below: patrol, suspicion if they hear something, chase if they see the hero and search at the last place they saw it. So in the same dungeon there are two different brains: the hero plans with GOAP and the guards follow a state machine. That is normal in a game: each character carries the simplest brain that is enough for it, and a guard on patrol does not need to plan.
There is another way of planning that is widely used in video games: HTN (hierarchical task network). Instead of searching among loose actions, as GOAP does, it goes down through recipes that someone has written: “to take the fort, go in through the gate, climb the wall with a rope or break in by force”, and each one is split into smaller tasks. Open it in the HTN planner. With the guard awake and standing sentry, the plan has 7 steps, tries 19 recipes and the hero takes the flag in 33 turns; if you check The guard is asleep, you get 5 steps, 7 recipes and 22 turns. It is more predictable than GOAP, at the price of not inventing anything that is not in the recipes. It is explained in depth in Planning before acting, from the agents track.
The idea has not stayed in video games. Embabel, a framework from the creator of Spring for programming agents with language models, plans with GOAP by default. Each action can be a call to a language model or ordinary code, but the order of the actions is decided by a classical planner, with no model, which plans again after each one (embabel-agent, on GitHub). The language model does what a rule cannot do, such as reading an email or summarizing it, and the sequence of steps stays in view, like the character’s plan.
The classic mistake
You might think that one clear rule is enough: “eat when hunger goes above 50”. Open the state machine with hysteresis at 0 and look at the strip. As soon as hunger drops just below 50, it stops eating; a few ticks later it is above 50 again, and back to the fridge it goes: it is trapped in a loop that lets it do nothing else. Meanwhile, the other needs keep rising, and since it is trapped going back and forth it has no time to attend to them: it risks having accidents. In 500 ticks, the tool counts 39 back-and-forths, 164 ticks in distress (with some need above 80) and 3 accidents. With the hysteresis it ships with, 40, there are 0 back-and-forths, 9 ticks in distress and no accidents.
Hysteresis is what a thermostat does: it does not switch the heating on and off at exactly the same temperature, but leaves a margin. Here, the character starts to eat when it goes above 50 and does not stop until it drops below 10.
Each brain needs its own brake against that oscillation: the tree, the “finish what it started” question; utility, inertia, a few extra points for what it is already doing. The simple rule is not badly written: it lacks the memory of what it was doing.
Try it yourself
Test yourself
In the state machine with hysteresis at 0, change the Seed to 2. How many back-and-forths come out now in 500 ticks?
There are ___.
With seed 1 it was 39. Look at “Back-and-forths” below the strip.
See the answer
Answer: 23.
With another seed the needs rise at a different pace and the count changes, but without hysteresis the oscillation is still there: 23 back-and-forths, 154 ticks in distress and 2 accidents. The problem is not the seed, it is the rule without memory.
Test yourself
Before turning on Add fun, think: with four states and 12 arrows, how many arrows will the state machine have with a fifth state, watching TV? Then check it in the house with fun.
It will have ___ arrows: ___ new ones.
Each old state needs an arrow to the new one, and the new one an arrow to each old one.
See the answer
Answers: 20 and 8.
Four arrows to watching TV and four back: 8 more. With a sixth state it would be 10 more, and so on every time. The tree only needs 3 new pieces and utility, 1: one more curve.
Test yourself
Take the brakes off all of them: open the house with no hysteresis, no “finish what it started” and no inertia and open Compare the four brains. Which brain spends the fewest ticks in distress over the 500 ticks?
The winner is [State machine / Behavior tree / Utility / GOAP], with ___ ticks in distress.
Wait for the counter to reach “Tick 500 / 500”.
See the answer
Answers: GOAP and 84.
GOAP plans again at every tick and does not oscillate: 0 back-and-forths. Utility, without inertia, hardly goes back and forth (5 times), but it is the one that spends the most time in distress, 206 ticks, and has 4 accidents: it changes its mind halfway and wastes time walking.
Test yourself
In “The dungeon”, with the factory surcharge (6), move the Cost of fighting to find the cost from which the plan no longer fights any guard. Then set the Surcharge per watched cell to 3 and repeat.
With surcharge 6, from ___; with surcharge 3, from ___.
Set the cost by hand and check whether any “fight” is left in the plan. With 47 it still fights one.
See the answer
Answers: 48 and 24.
Going around is expensive because the long path steps on watched cells, and each one adds the surcharge. With the surcharge halved, going around costs much less, and half the fighting cost is enough to make fighting not worth it.
- Change the house to With two fridges and open Compare the four brains again. All four go to the fridge that is closest to them, and none has needed a change: the new object brings what it gives, and the brains use it as it is.
- Switch to You play and take the treasure without any guard seeing you: the alcoves in the gallery are for hiding. If a guard is not chasing you, you can knock him out by surprise by clicking on him.
Further reading
- Behavior tree, Wikipedia: the types of nodes and how they combine.
- Will Wright explains what The Sims and an ant colony have in common, on Engadget: intelligence spread across the objects instead of packed into the character. The ants are in the Ants tab of the same tool.
- Finding a path, from the same course: the search underneath GOAP.
- Learning from reward, from the same course: instead of writing the rules, letting the character learn what to do from rewards.
“How a video game character decides: states, trees and scores” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

You may also like
Deciding by playing at random: Monte Carlo and Hex
How a machine decides when it cannot evaluate a position: thousands of random games, the Monte Carlo tree (MCTS) and Hex. AI course, 0.8.
How far a machine looks: the horizon effect and deciding with less
Why a machine that looks only a few moves ahead gives pieces away, how quiescence search saves it and how engines decide by looking at less. AI course 0.7.















Leave a comment