How does an enemy decide what to do? State machines explained
How does a game guard seem to think? Usually with a finite state machine: a few states like Patrol, Chase and Search, plus simple rules for switching between them.
Play with a live guard, change how far he can see, and watch the state change as you sneak past. Then see why states beat a pile of true/false flags, how a small buffer stops twitching, and how to build your first one.

The guard that seems to think
You sneak past a guard in a game. He walks his route, spots you, runs after you, loses you, checks the last place he saw you, then shrugs and goes back to his route. It feels like he is thinking. He is not. He is following a very small, very old idea called a finite state machine, and you can build one in an afternoon.
What a state machine is
A state machine has two ingredients. States are the things the character can be doing: Patrol, Chase, Search. Transitions are the rules for switching: "if the player is inside my sight range, go from Patrol to Chase". The key rule is that the character is in exactly one state at a time, and only the code for that state runs.
"Finite" just means the list of states is short and fixed. You can draw the whole thing on a napkin: three boxes and a few arrows. That is a feature. If you can draw it, you can understand it, test it and fix it.
Why not just a pile of ifs?
Beginners often start with a handful of true/false flags: seesPlayer, heardNoise, isLookingAround, isReturning. It works for a day. Then you add a fourth behaviour and the flags start to fight: what should happen when he is looking around and hears a noise and sees you? Every new flag doubles the number of combinations you have to think about.
A state machine removes that. The guard is never "a bit chasing and a bit searching". He is in one state, and each state only needs to answer two questions: what do I do every frame, and what makes me leave?
A state machine in a few lines
Here is the heart of the demo above, simplified. The game calls this once per frame, which is the game loop doing its job:
if (state == "patrol") {
walkToNextWaypoint()
if (distanceToPlayer < sightRange) state = "chase"
}
else if (state == "chase") {
moveToward(player)
if (distanceToPlayer > sightRange * 1.4) state = "search"
}
else if (state == "search") {
goToLastSeenSpot()
if (distanceToPlayer < sightRange) state = "chase"
else if (waitedLongEnough) state = "patrol"
}
Look at the chase rule. The guard starts chasing when you are inside the sight range, but only gives up when you are 1.4 times that far away. That gap is deliberate. If both rules used the same distance, a player standing right on the edge would flip the guard between Chase and Search every frame, and he would twitch. A small buffer like this is a common fix, and you can see it in the dashed outer circle in the demo.
Where you will see state machines
Enemy behaviour is the classic use. The ghosts in Pac-Man switch between modes such as chasing you, scattering to their corners and running away when you eat a power pellet. But state machines are everywhere in games: a player character that is idle, running, jumping or falling; a door that is closed, opening, open or locked; a menu that moves from title screen to settings and back. Many engines include built-in tools for animation state machines, so the idea you just played with is already part of the editor you will probably use.
Try it in your first project
Start with three states and no more. Draw the boxes and arrows on paper first, and write the reason next to every arrow. Only add a new state when you can name a behaviour that is clearly different, such as "flee when health is low". If a state needs a long list of exits, it is probably two states hiding in one.
Then play with the demo again. Shrink the sight range to the minimum and try to sneak past. Raise it to the maximum and see how the Search state gives you a second chance. Every one of those changes is just a number in a rule, and that is how designers tune how smart an enemy feels.
More in Making Games
all Making Games →2 Dec How do games branch a conversation? Dialogue choices, flags and locked answers Dialogue in games is a map: each line of text is a node, and each answer is a road to another node. Add a few flags and numbers the game… Making Games
24 Nov How does a 2D game fake depth? Parallax scrolling and background layers explained Walk through a game level and the faraway mountains barely move while the trees rush past. That trick is parallax scrolling, and it makes a… Making Games
16 Nov Why do menus and movements feel smooth? Easing and tweening explained Press a button in a game menu and the panel slides in, slows down and settles. That feel comes from easing: changing the speed of a… Making Games
3 Nov How do games make fire, smoke and sparks? Particle systems explained Fire, smoke and sparks in games are usually not drawings or videos. They are swarms of tiny short-lived particles made by an emitter. This… Making Games
1 Nov How does an enemy find its way around a wall? Pathfinding and A* explained How does an enemy find its way around a wall? Walking straight at you works only until the first obstacle. Pathfinding treats the map as a… Making Games
30 Oct How do game mods work? Load order and why the last mod wins Ever installed two mods and watched one quietly stop working? A mod is a small package that swaps or adds the data files a game reads, and…