Zero Labs
Industry News / AI

How a Video Game Character Decides: States, Trees And Scores

Source · Towards AI By Ramon Invarato

From the publisher’s feed, collected in the ZeroLabs newsroom.

Video game AI without learning: state machine, behavior tree, utility as in The Sims, and GOAP. AI course, 0.9.Note: Although this article can be read independently, it is part of a comprehensive interactive course — the table of contents for which can be found at https://jarroba.com/en/ai-course/—where the entire course is available for free.AI from the First Bit › Level 0 — How a machine sees the world part 9 of 9The 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 firstOpen 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 happenedThe 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 mistakeYou 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.Further readingBehavior 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.← Previous · part 8Deciding by playing at random: Monte Carlo and HexOriginally published at https://jarroba.com on November 23, 2026.How a Video Game Character Decides: States, Trees And Scores was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.