# Automaty stanów dla postaci (FSM)

EN: **Finite state machines for game characters** · id `gamefsm` · aliases: FSM, finite state machine, automat stanów skończonych, automat skończony, maszyna stanów

Path: [AI Atlas](https://aiatlasgraph.com/llms.txt) › [AI w grach](https://aiatlasgraph.com/t/gameai.md) › [Zachowanie postaci w grach](https://aiatlasgraph.com/t/gamecharacters.md) › Automaty stanów dla postaci (FSM)

## Description

- PL: Model sterowania, w którym postać jest zawsze w dokładnie jednym stanie, a zdarzenia wyzwalają zdefiniowane przejścia; warianty hierarchiczne i ze stosem dodają współdzielenie i pamięć.
- EN: A control model where a character is always in exactly one state and events trigger defined transitions; hierarchical and stack variants add sharing and memory.

## In one sentence

- PL: Postać ma kilka wyraźnych trybów – patroluje, goni, ucieka – i przełącza się między nimi, gdy coś się wydarzy.
- EN: A character has a few clear modes – patrolling, chasing, fleeing – and switches between them when something happens.

## Problem it solves

- PL: Pisanie zachowania postaci jako jednego wielkiego bloku warunków szybko zamienia się w plątaninę, której nikt nie umie poprawić.
- EN: Writing a character's behaviour as one big block of conditions quickly turns into a tangle nobody can fix.

## What it does

- PL: Działa jak schemat na kartce: kółka to tryby, strzałki to zdarzenia, po których postać przechodzi do innego kółka.
- EN: It works like a diagram on paper: circles are modes, arrows are events that move the character to another circle.

## Everyday example

- PL: Strażnik w grze skradankowej spaceruje po korytarzu, słyszy hałas, idzie sprawdzić, a gdy cię zobaczy, zaczyna pościg.
- EN: A guard in a stealth game walks the corridor, hears a noise, goes to check, and starts a chase once he spots you.

## This concerns you if you…

- PL: zaczynasz programować przeciwników w swojej pierwszej grze
- PL: chcesz zrozumieć, skąd biorą się przewidywalni wrogowie
- PL: projektujesz menu lub proste roboty o kilku trybach
- EN: are coding enemies for your first game
- EN: want to know why some enemies feel predictable
- EN: design menus or simple robots with a few modes

## Keywords

finite state machine · hierarchical state machine · State pattern · pushdown automaton · statecharts · Animator state machine · enter exit actions

## How it works

- PL: Każdy stan ma akcje wejścia, wyjścia i aktualizacji oraz listę przejść wywoływanych zdarzeniami; zdarzenie bez przejścia jest ignorowane. Maszyna hierarchiczna przekazuje nieobsłużone zdarzenia do nadstanu, a automat ze stosem pozwala odłożyć bieżący stan i wrócić do niego po np. animacji strzału.
- EN: Each state has enter, exit and update actions and a list of event-triggered transitions; an event with no transition is ignored. A hierarchical machine passes unhandled events up to a superstate, while a pushdown automaton stacks the current state and returns to it after, say, a firing animation.

## Limits

- PL: Liczba przejść rośnie bardzo szybko wraz z liczbą stanów, więc złożone zachowania stają się nieczytelne. Automat nie planuje ani nie waży opcji, dlatego bogatsze AI przechodzi na drzewa zachowań, punktację lub planowanie.
- EN: The number of transitions grows very quickly with the number of states, so complex behaviour becomes unreadable. A state machine neither plans nor weighs options, which is why richer AI moves to behaviour trees, scoring or planning.

## Key terms

- PL: **przejście**: Reguła: w tym stanie, po tym zdarzeniu, przejdź do tamtego stanu.
- PL: **nadstan**: Stan grupujący kilka podobnych stanów, które dzielą wspólne reakcje.
- PL: **automat ze stosem**: Maszyna pamiętająca poprzednie stany, by móc do nich wrócić.
- EN: **transition**: A rule: in this state, on this event, go to that state.
- EN: **superstate**: A state grouping several similar states that share reactions.
- EN: **pushdown automaton**: A machine that remembers previous states so it can return to them.

## Related topics

Untyped, symmetric "see also" links.

- [Drzewa zachowań (Behaviour trees)](https://aiatlasgraph.com/t/behaviortrees.md)

## Sources

- [Game Programming Patterns – State](<https://gameprogrammingpatterns.com/state.html>) `nystrom-gpp-state` · by Robert Nystrom

---

AI Atlas revision 122 (2026-10-30) · HTML: https://aiatlasgraph.com/t/gamefsm · index: https://aiatlasgraph.com/llms.txt · everything: https://aiatlasgraph.com/llms-full.txt · updated 2026-10-30
