Game State Management in LUA

I use Playdate's object system and whenever I need a state machine, I write something like this in the init function:

-- Game States
self.states = {
    gameplay = self.gameplayState,
    pause = self.pauseState,
    gameover = self.gameoverState
}
-- Starting Game State
self.state = self.states.gameplay

Then my update looks like:

function Game:update()
	-- Perform State
	self.state(self)
end

and then I go and make function Game:gameplayState(), function Game:pauseState(), etc. in the object to hold each states' code. If I want to change states, I just write something like self.state = self.states.pause.

8 Likes