A state machine keeps a number for what something is doing and runs the routine for that number. Changing the number changes the behavior from the next frame. Games use one for their overall flow and others for each player and enemy, usually through a jump table.