Problem
i want to implement a pause state and with a simple conditional inside the update of PlayState is very very simple. However i think that the pause has to be another state in the StateMachine so i dont know how to call that PauseState and then resume the Play State without calling the init again, i mean, i dont want to start the PlayState from zero
Code
Main
gStateMachine = StateMachine {
['title'] = function() return TitleScreenState() end,
['countdown'] = function() return CountdownState() end,
['play'] = function() return PlayState() end,
['score'] = function() return ScoreState() end,
['pause'] = function() return PauseState() end,
}
gStateMachine:change('title')
StateMachine
function StateMachine:init(states)
self.empty = {
render = function() end,
update = function() end,
enter = function() end,
exit = function() end
}
self.states = states or {} -- [name] -> [function that returns states]
self.current = self.empty
end
function StateMachine:change(stateName, enterParams)
assert(self.states[stateName]) -- state must exist!
self.current:exit()
self.current = self.states[stateName]()
self.current:enter(enterParams)
end
PlaysState
if love.keyboard.wasPressed('p') then
self.paused = not self.paused //Here i pause the game
end
Resume
Basically I want to make it more pro, and I understand that the correct way to do it according to the design patterns of video games is to incorporate a GameState that is PauseState.
My problem as I was telling you before is that when I make a change state from PlayState to PauseState, and then return to PlayState, the PlayState:init is called again, and I simply want its update not to be called and then resume correctly.
What I want to do is correct ?, Is there any way to do it right ?, I imagine something like stacking the States then Push and Pull them, but I do not know if it is correct