Page 1 of 1

HUMP wont restart gamestate

Posted: Thu Jan 12, 2017 10:17 pm
by issuetracker
Hello,

i am currently making a game for a school project and i stumbled about a problem that has been bothering me. I can't seem to make the game restart. From the main.lua i use hump.gamestate to switch to the menu, then from there when a user presses the play button i again use the gamestate to switch to my game.lua where the game logic is. Now the problem:

From here, if switch to the menu again it all looks good and well but when i press play again it sends me back to the same state i was before when the game ended. I know i am probably doing something wrong so if anyone can guide me on how to restart the game with hump gamestate. Is there a way to dump all the state and then restart it? Because only using gamestate.switch() doesn't work for me.

Nice community you got here :)

Re: HUMP wont restart gamestate

Posted: Fri Jan 13, 2017 1:27 pm
by xNick1
I never used HUMP, but you could reset your variables when you press the "Play" button.
(ex. Every level you gain 1 hp, so you end up with 10 hp. When you enter the load function again you reset the hp to 0).
Otherwise call "love.load()" to reload the game once it ended

Re: HUMP wont restart gamestate

Posted: Fri Jan 13, 2017 1:44 pm
by zorg
"Hi, and welcome to the forums!"

Also, please post a minimal code example where it doesn't work; that helps us help you more. :3

Re: HUMP wont restart gamestate

Posted: Fri Jan 13, 2017 3:29 pm
by raidho36
I see nothing out of order here - if you switch state it does just that, but it doesn't reset it. You have to do that manually.

Re: HUMP wont restart gamestate

Posted: Fri Jan 13, 2017 5:08 pm
by Chroteus
You see, the init() method used to load data is set to nil by HUMP after it's called once.
What you can do, is to change your current init() method and rename it to loadState(), for example. Then, in the init() call loadState(). Whenever you need to restart the game, call loadState().

Re: HUMP wont restart gamestate

Posted: Fri Jan 13, 2017 9:11 pm
by s-ol
Chroteus wrote:You see, the init() method used to load data is set to nil by HUMP after it's called once.
What you can do, is to change your current init() method and rename it to loadState(), for example. Then, in the init() call loadState(). Whenever you need to restart the game, call loadState().
No, thats just making it way harder to use the state properly.

It's very simple: use gamestate:enter() not gamestate:init() since the latter runs once and only once, while enter runs each time.

The documentation is pretty clear about this actually: http://hump.readthedocs.io/en/latest/ga ... -callbacks

Re: HUMP wont restart gamestate

Posted: Wed Jan 18, 2017 7:49 pm
by Chroteus
s-ol wrote:
Chroteus wrote:You see, the init() method used to load data is set to nil by HUMP after it's called once.
What you can do, is to change your current init() method and rename it to loadState(), for example. Then, in the init() call loadState(). Whenever you need to restart the game, call loadState().
No, thats just making it way harder to use the state properly.

It's very simple: use gamestate:enter() not gamestate:init() since the latter runs once and only once, while enter runs each time.

The documentation is pretty clear about this actually: http://hump.readthedocs.io/en/latest/ga ... -callbacks

As far as I understood OP wants to restart the game to start a fresh game. So, :enter() won't cut it.
EDIT: To clarify my point, if he enters "pause" state while in game, it shouldn't restart the game with :enter() after unpausing.

Re: HUMP wont restart gamestate

Posted: Wed Jan 18, 2017 8:21 pm
by bartbes
Chroteus wrote: EDIT: To clarify my point, if he enters "pause" state while in game, it shouldn't restart the game with :enter() after unpausing.
Ah, but that's why there's a stack of gamestates! If you push a state (then pop it again), enter isn't called, resume is called.