0
\$\begingroup\$

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

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

In resume we want to keep the status of GameState when we go to PauseState, imagine like they are instances so, like i said in the question we can work with an array and push or pop, however in that case we just want to integrate the pause state with this characteristics, i develop something more specific for that with an auxiliar to keep the status

StateMachine definition

function StateMachine:init(states)
...
    self.puaseState = self.empty
end

Pause Function

function StateMachine:statePause(stateName)
    assert(self.states[stateName])
    self.pauseState = self.current
    self.current = self.states[stateName]()
    self.current:enter()
end

Resume Function

function StateMachine:stateUnPause()
    self.current = self.pauseState
    self.pauseState = self.empty
    self.current:enter()
end
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .