O'Reilly logo

Learning Game AI Programming with Lua by David Young

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a finite state machine

To build a finite state machine for our agent, we'll create the initial states that wrap each possible action. As the state machine needs a starting point, we'll set the state explicitly to the idle state to begin with. Once the idle state has finished, our state machine will automatically pick the most relevant action to execute afterward:

SoldierLogic.lua:

function SoldierLogic_FiniteStateMachine(userData) local fsm = FiniteStateMachine.new(userData); fsm:AddState("die", DieAction(userData)); fsm:AddState("flee", FleeAction(userData)); fsm:AddState("idle", IdleAction(userData)); fsm:AddState("move", MoveAction(userData)); fsm:AddState("pursue", PursueAction(userData)); fsm:AddState("randomMove", RandomMoveAction(userData)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required