---
title: How a video game character decides: states, trees and scores
url: https://jarroba.com/en/how-a-character-decides/
author: Ramón Invarato
date: 2026-11-23
modified: 2026-11-23
language: en
license: CC BY-NC-SA 3.0 (https://creativecommons.org/licenses/by-nc-sa/3.0/) — attribution: "How a video game character decides: states, trees and scores", Ramón Invarato, jarroba.com
categories: Artificial intelligence
tags: ai, AI course, Artificial Intelligence, behavior tree, state machine, utility AI, video games
series: Level 0 — How a machine sees the world (part 9 of 9)
---

# 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.

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”](https://jarroba.com/tools/en/game-ai?tab=house&tick=150)**. 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)”.

[![The house in the video game AI tool at tick 150: at the top, the "See what the AI is thinking" row with Path and Decision on; the character in a doorway with the speech bubble "wander", the fridge at the top left with the label "Hunger 13", the bed at the top right with "Sleep 39", the shower at the bottom with "Hygiene 18" and the sofa with the TV in the living room; below, the bars for hunger (13), sleep (39) and hygiene (18) with their three marks and the strip of colors. On the right, the list of the four brains with the state machine chosen and, boxed, its brain: "It is in wander", the drawing of the four states joined by 12 arrows and the three it can take now: → eat if Hunger > 50 (13), → sleep if Sleep > 50 (39), → shower if Hygiene > 50 (18)](https://jarroba.com/wp-content/uploads/2026/11/casa-maquina-de-estados-www.jarroba.com_-1-1024x784.png)](https://jarroba.com/wp-content/uploads/2026/11/casa-maquina-de-estados-www.jarroba.com_-1-scaled.png)

Now open [the same moment with the utility brain](https://jarroba.com/tools/en/game-ai?tab=house&brain=utility&tick=150). 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).

[![The same moment with the utility brain: the character at the fridge with the speech bubble "eat · 0.87" with the drawing of the fridge, which has no label; above the bed, "sleep 0.00", and above the shower, "shower 0.26"; below, hunger at 64. On the right, boxed, the cards of the four actions, with the breakdown underneath: eat 0.87, highlighted, with the dot at Hunger 76 ("curve 0.57 + 0.30 of inertia"); sleep 0.00, with Sleep 9 ("curve 0.00"); shower 0.26, with Hygiene 31 ("curve 0.31 × 0.84 (far)"), and wander 0.10, with no curve](https://jarroba.com/wp-content/uploads/2026/11/casa-utilidad-www.jarroba.com_-1-1024x784.png)](https://jarroba.com/wp-content/uploads/2026/11/casa-utilidad-www.jarroba.com_-1-scaled.png)

## 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.

[![The state machine brain with fun added, at tick 60: "It is in watch TV" and the drawing of five states, wander, eat, sleep, shower and watch TV, with 20 arrows; the 8 that go into and out of watch TV, in green and dashed. Below: "20 arrows. In green, the ones that had to be added for fun: 8"](https://jarroba.com/wp-content/uploads/2026/11/casa-diagrama-diversion-www.jarroba.com_-1.png)](https://jarroba.com/wp-content/uploads/2026/11/casa-diagrama-diversion-www.jarroba.com_-1.png)

**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](https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control))).

[![The behavior tree of "The house" walked at tick 23, with Hunger 51, Sleep 35 and Hygiene 29 and the character wandering. From a root that reads top to bottom come four numbered branches: 1, Is any need urgent? → deal with it, crossed out because none is above 85; 2, Was it doing something that has not gone down yet? → keep going, crossed out because it was wandering; 3, Is any above the threshold? → the highest one, highlighted in green because Hunger 51 is above 50, and the character starts to eat; 4, Otherwise, wander, in gray and dashed because it is never reached](https://jarroba.com/wp-content/uploads/2026/11/arbol-de-comportamiento-www.jarroba.com_-1-1024x576.png)](https://jarroba.com/wp-content/uploads/2026/11/arbol-de-comportamiento-www.jarroba.com_-1.png)

**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](https://www.engadget.com/2010-11-08-will-wright-explains-what-the-sims-and-an-ant-colony-have-in-com.html)). A new object brings its advertisement, and the character uses it without anyone touching its brain.

[![The idea of utility in a drawing. On the left, the curves of two actions at tick 150: eat, for hunger, rises slowly and then quickly; sleep, for sleep, stays almost flat until it passes the halfway mark and then shoots up. An orange dot on the eat curve, at Hunger 76 (the need it decided with, before acting in that tick), is much higher than the blue dot of sleep, at Sleep 9, almost at zero: eat wins. On the right, the objects in the house advertise what they give with a speech bubble each: the fridge, "I take away your hunger"; the bed, "I take away your sleep"; the shower, "I leave you clean"; and the sofa, with a dashed border because it is a new object, "I take away your boredom", which brings its own advertisement](https://jarroba.com/wp-content/uploads/2026/11/utilidad-curvas-y-anuncios-www.jarroba.com_-1-1024x547.png)](https://jarroba.com/wp-content/uploads/2026/11/utilidad-curvas-y-anuncios-www.jarroba.com_-1.png)

**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](https://jarroba.com/en/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](https://en.wikipedia.org/wiki/F.E.A.R._(video_game))).

[![GOAP as a search between situations, in the house with two fridges at tick 23. From the starting situation, with Hunger 51, which is above 50, two paths lead to the goal, hunger dealt with. Above, in gray and dashed: go to the kitchen fridge, cost 17, and eat, cost 4; in total 21, discarded. Below, highlighted in green: go to the bathroom fridge, cost 5, and eat, cost 4; in total 9, which is the chosen plan](https://jarroba.com/wp-content/uploads/2026/11/goap-busqueda-www.jarroba.com_-1-1024x503.png)](https://jarroba.com/wp-content/uploads/2026/11/goap-busqueda-www.jarroba.com_-1.png)

Go back to [the house with the factory values](https://jarroba.com/tools/en/game-ai?tab=house) 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.

[![Compare the four brains at the end of the 500 ticks, with the factory values: four small houses, one per brain, each with its strip of colors. State machine: 0 accidents, 9 ticks in distress, 0 back-and-forths. Behavior tree, utility and GOAP: 0, 0 and 0](https://jarroba.com/wp-content/uploads/2026/11/casa-cuatro-cerebros-www.jarroba.com_-1-1024x327.png)](https://jarroba.com/wp-content/uploads/2026/11/casa-cuatro-cerebros-www.jarroba.com_-1.png)

**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”](https://jarroba.com/tools/en/game-ai?tab=dungeon&tick=1): 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](https://jarroba.com/tools/en/game-ai?tab=dungeon&fight=200&tick=1), 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.

[![Two plans in "The dungeon". On the left, fighting costs 30: go to the key, take it, go to the door, open it, go to guard 1, fight him (30), go to guard 2 (33), fight him (30), go to the chest, take the treasure, go to the exit and leave; 28 world states looked at. On the right, fighting costs 200: the same up to opening the door and then go to the chest (82), take the treasure, go to the exit (57) and leave, going around the guards; 11 world states looked at](https://jarroba.com/wp-content/uploads/2026/11/mazmorra-planes-www.jarroba.com_-1-1024x585.png)](https://jarroba.com/wp-content/uploads/2026/11/mazmorra-planes-www.jarroba.com_-1-scaled.png)

Nobody wrote those two sequences. The planner finds them with a search like the A* of [Finding a path](https://jarroba.com/en/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](https://jarroba.com/tools/en/game-ai?tab=dungeon&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 dungeon at tick 14: at the top, the "See what the AI is thinking" row with Path, Replans and Decision on (Explored, off), and above the map the label "Opens the door", which remembers the last thing it did; the hero at the top left with the speech bubble "go to guard 2 → fight guard 2 → go to the chest" and a yellow line that crosses the gallery at the top, where the other guard patrols, and goes down the corridor to the guard at the bottom; the two guards, with the speech bubble "patrol". Below, "Lives 3/3 · Seen 0 · Replans 1". On the right, boxed, the new plan: go to guard 2 (38), fight guard 2 (30), go to the chest (12), take the treasure (1), go to the exit (15) and leave; 21 world states looked at, last plan: the world changed](https://jarroba.com/wp-content/uploads/2026/11/mazmorra-replan-www.jarroba.com_-1-1024x542.png)](https://jarroba.com/wp-content/uploads/2026/11/mazmorra-replan-www.jarroba.com_-1-scaled.png)

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](https://jarroba.com/tools/en/game-ai?tab=htn). 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 HTN planner with the factory world. On the left, the plan of 7 steps: go to the barracks, pick up the key, go to the storehouse, pick up the rope, go to the wall, climb the wall and go in, with "Recipes tried to make the plan: 19 · replans 0" boxed, and below the world at the start, with the guard awake and a key and a rope available. On the right, the decomposition of Take the fort: through the gate, over the wall and by force are not met at the start; first it gets the key from the barracks, tries again, fails through the gate because it cannot get the guard out of the way, gets the rope from the storehouse and goes in over the wall. In green, the recipes chosen; in gray, those that did not meet their conditions; in red, those that failed on the way down](https://jarroba.com/wp-content/uploads/2026/11/htn-recetas-www.jarroba.com_-1-976x1024.png)](https://jarroba.com/wp-content/uploads/2026/11/htn-recetas-www.jarroba.com_-1-scaled.png)

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](https://github.com/embabel/embabel-agent)). 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](https://jarroba.com/tools/en/game-ai?tab=house&tick=500&hysteresis=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.

[![Two time strips of the state machine, of 500 ticks each. Above, with hysteresis 40: long blocks of one color, 0 back-and-forths. Below, with hysteresis 0: the blocks get shorter and, about three quarters of the way along, the strip flickers between sleep, eat and shower; 39 back-and-forths, 164 ticks in distress and 3 accidents](https://jarroba.com/wp-content/uploads/2026/11/tira-con-y-sin-histeresis-www.jarroba.com_-1-1024x371.png)](https://jarroba.com/wp-content/uploads/2026/11/tira-con-y-sin-histeresis-www.jarroba.com_-1-scaled.png)

**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.

[![Two graphs of hunger over time. Above, without hysteresis, with a single threshold at 50: hunger rises to 50, it eats, it drops a little, stops eating and soon goes above 50 again; the line zigzags right at the threshold, marked eats, stops, eats, stops, over and over. Below, with hysteresis: it starts to eat when it goes above 50 and does not stop until it drops below 10, with the band between 50 and 10 shaded in green; the line is a long sawtooth, with only two meals in the same time](https://jarroba.com/wp-content/uploads/2026/11/histeresis-hambre-www.jarroba.com_-1-1024x737.png)](https://jarroba.com/wp-content/uploads/2026/11/histeresis-hambre-www.jarroba.com_-1.png)

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](https://jarroba.com/tools/en/game-ai?tab=house&tick=500&hysteresis=0), change the **Seed** to 2. How many back-and-forths come out now in 500 ticks?

There are ___.

**Hint:** 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](https://jarroba.com/tools/en/game-ai?tab=house&tick=60&fun=true).

It will have ___ arrows: ___ new ones.

**Hint:** 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](https://jarroba.com/tools/en/game-ai?tab=house&hysteresis=0&remember=false&inertia=0) 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.

**Hint:** 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 ___.

**Hint:** 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](https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control)): the types of nodes and how they combine.
- [Will Wright explains what The Sims and an ant colony have in common](https://www.engadget.com/2010-11-08-will-wright-explains-what-the-sims-and-an-ant-colony-have-in-com.html), 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](https://jarroba.com/en/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.

## Series: Level 0 — How a machine sees the world

1. [Everything is a number (yes, even your name)](https://jarroba.com/en/everything-is-a-number.md)
2. [An image is a table of numbers](https://jarroba.com/en/an-image-is-a-table-of-numbers.md)
3. [A corrupted bit: how to fix it without knowing which one it is](https://jarroba.com/en/when-a-bit-gets-corrupted.md)
4. [What is a model (and why a straight line already counts as one)](https://jarroba.com/en/what-is-a-model.md)
5. [Finding a path: an AI that learns nothing](https://jarroba.com/en/finding-a-path.md)
6. [Playing against a machine: minimax and alpha-beta pruning](https://jarroba.com/en/playing-against-a-machine.md)
7. [How far a machine looks: the horizon effect and deciding with less](https://jarroba.com/en/how-far-a-machine-looks.md)
8. [Deciding by playing at random: Monte Carlo and Hex](https://jarroba.com/en/deciding-by-playing-at-random.md)
9. [How a video game character decides: states, trees and scores](https://jarroba.com/en/how-a-character-decides.md)

