Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamestate.leave/enter functions are called immediately inside update function #15

Closed
viviicat opened this issue Apr 24, 2013 · 2 comments

Comments

@viviicat
Copy link

I was just working on a game where I have a call to gamestate.switch, and the leave function tries to clean up a repeating sound, but the update function starts it again. So the code execution is like this:

[states.game:update]
if weShouldSwitchStates then
  State.switch(states.menu)
    [states.game.leave]
    sound:stop()
end
...
sound:play()

[states.menu:update]
.... (sound is playing)

You could just tell me to not do something like that, but if my update function is complex and enters a lot of other classes it's going to be hard not to have conflicts.

I'd suggest waiting until right before the new update/draw functions are called before calling leave and enter. That way I can be sure that leave is going to clean up all the resources I'm using in the gamestate.

@viviicat
Copy link
Author

I realized that the switch function returns the results of enter on the new gamestate, so maybe that can still be called right away, with only leave being called in the next loop?

EDIT: but this results in the potential for multiple enters and inits to be called, with only one leave being called, which could also cause resource leaking.

@vrld
Copy link
Owner

vrld commented Apr 27, 2013

See comment to #16

@vrld vrld closed this as completed Apr 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants