Jump to content

State Management


oysterCrusher
 Share

Recommended Posts

With the recent release of v1.1 I'm attempting to reproduce (and improve) one of my old Ludum Dare games using Phaser. Most of Phaser makes sense to me and the core game is coming along nicely. The confusion I have is with the management of states, specifically when preload, create and destroy are called. From the pinned post on function order (http://www.html5gamedevs.com/topic/1372-phaser-function-order-reserved-names-and-special-uses/?p=9110) I got the impression that preload was called when the state was first started, followed immediately by create. The question is then what are peoples recommendations for the best way to handle starting and leaving states repeatedly, such as

 

level select menu -> play level 1 -> return to level select menu -> play level 2 ...etc...

 

where the Menu and Game states are started numerous times but really all the objects in that state only need to be created once. Would you recommend just putting all the sprite, map, etc creation in the initialisation of the state and ignore the create function?

 

Another related question: Is there any plan to implement some form of onExit call to a state (as mentioned in another thread [Edit: found it http://www.html5gamedevs.com/topic/1320-need-a-simple-demo-for-switching-state-with-phaser-and-js/?p=9877])? This would be handy for clearing up things like input bindings that would otherwise still be called while in a different state.

 

Thanks.

Link to comment
Share on other sites

Hello, I usually have a destroy() function in my states that cleans up any resources the state uses. I then recreate those objects everytime I transition back to the state. I do this because I don't want to keep any objects in memory for any longer than they need to be, especially when targeting mobile browsers.

Link to comment
Share on other sites

If your State has a function called 'shutdown' it will be called right before the State is switched to another State. So you can handle tidying up resources in there.

 

As for assets I would mostly load them all in a preloader up front, so that there are no further loads once in the game itself. This isn't a hard and fast rule, and I've broken it myself numerous times, but it's how most games run (depending on their scale).

Link to comment
Share on other sites

Thanks for the replies. I hadn't spotted that there was a shutdown function I could be using for guaranteeing a clean change of states. I'll start using that.

 

Hsaka, you're right about not keeping things in memory if they're not being used, and I've not had any issues destroying and recreating all the game objects so I'll keep doing it that way for now.

 

Rich, I already load all the assets up front in a preloader state, thanks to other posts I've seen here. I was wondering if people routinely use the create function to set all the sprites, tiles, etc and then destroy them when leaving the state for a state that is used repeatedly.

Link to comment
Share on other sites

When you leave a State the display list is cleared down, so you'd almost certainly need to run 'create' when you return to rebuild that I would have thought. But it all depends how your game is structured. Right now I've got a game where the "Game" itself is a State, but it has a menu system that I display many times through-out the game, and that's just a class (not a state) that has its own Group populated with various sprites. And I show and hide that class as needed. So the group is always part of the display list, it's just not rendered when the menu is in-active. I tend to use this approach in lots of places.

Link to comment
Share on other sites

Related question, is Phaser going to support multiple states?

 

I have seen other engines which allow States to be stacked, and they come with methods such as "obscured" and "revealed" which are triggered when another state is displayed above them.

 

This allows you to do things like a pause menu system on top of the game, where you stop updates and inputs in the "obscured" function, and handle them in the menu, then resume in "revealed" when the menu is removed.

 

(I am happy to add this in if people want it)

Link to comment
Share on other sites

Yes I've been planning out how to do this, but it's not quite as straight forward as adding a few methods to the States themselves - because really what you need is for every state to have its own implementation of things like the tween manager, physics manager, etc so that when your state pauses any tweens that were active for example are put into a safe sleep mode, and resumed properly when you switch back to the state again - something which isn't possible if you need to say use a tween in your Pause state.

 

This isn't a problem and I know how I'm going to do it, but it means it needs quite a bit of careful planning.

Link to comment
Share on other sites

I propose having those systems as part of the statemachine itself, and letting states decide how or if they want to pause Tweens themselves when they are entered or exited.

Too much over thinking here will lead to an inflexible engine, people making games know best how to handle what their game needs - forcing too strict a paradigm will make it so that as soon as you try to do something different, most of your time is spent fight with the engine.

A lot of engines fall victim to this in the quest to be as easy to use as possible, so I hope the same fate does not happen to phaser

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...