• Advertisement
Sign in to follow this  

An effective way to manage states?

This topic is 1452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a 2d rpg game in c++ with sfml, and right now I am experimenting with game states.  Right now I am using a State* state[] that will store different states (for different areas) as they are found (unless they are menus in which they will already be there).  That way if I leave an area and then come back to it later (don't worry how I do this part, it works).

 

My question is this: is there a more efficient way of managing states for this type of game?  At some point a State* array will just become to big and inefficient.

Share this post


Link to post
Share on other sites
Advertisement

What does it mean to “find” a state?  What is the relationship with menus?

 

Are you talking about uncovering part of a screen as you move over it?  Killing enemies on a certain screen?  What tasks you have accomplished on a certain screen?

What?

 

 

L. Spiro

Share this post


Link to post
Share on other sites

You are making one state by level if I understand correctly,I think it should be better to do a design to reuse a map and just switch of map.

You can use component to help on the logic of game surely and have object type who contains them and then save the object.

Then it's just a question of making the map and switch, like that it's the same as you switch of state but more efficient.

Edited by Alundra

Share this post


Link to post
Share on other sites
Echoing the last comment, I would typically expect to see states like Loading, MainMenu, Options, Pause, and Playing. The Playing state would then have a completely different layer to handle the active map/area. A pure state machine makes a lot of sense for simpler UI flows and a stack of states works best when you have lots of nested UI, like options menus or intricate inventory management or the like.

Separate levels rarely benefit from their own state. Levels are best described by data, not code. The way the game behaves does not change between levels 1 and 2. Good games usually have a consistent set of rules (code) and a progression of challenges using those rules (data). Even things like mini-games are integrated into the game rather than being separate modes/states increasingly often.

Share this post


Link to post
Share on other sites

For the levels themselves one option would be to create a class Level. And make different instances of that class for each level. This class could then keep track of all the information you want. So in a circumstances that you were to leave and then return to an area the information would still have been saved.

As to how you organize keeping track of the different Level Instances that is up to you and the game you are making. If the areas represent sections of a map, a 2d array would allow you to access each Instance by its position on the grid. If the areas represent separate levels entirely than a simple array would do. If you know before hand how many areas there are then creating and naming each one on initializing the game might be preferable.

I guess we just need more information about your game to give you more advice. But yes I agree with the above, definitely keep menu states and levels separate. You will avoid a lot of headache if you do, trust me.

Hope this helped.

Share this post


Link to post
Share on other sites

Levels are not game states, levels are content of a game state, specifically, gameplay, Game states are used to sepparate contexts of your application that have radically different logics, a menu is very different than gameplay, and maybe you have other states, like a world map and a battle stage, the logic in each of them is very different to the others and there is a connection between them, look up state machines. Between different levels, the logic of the gameplay itself is likely to be more or less the same.

 

As for levels, if your game is linear you could have a simple array, if the array is too big it might be more clear and efficient to hold a link to the next level within the current level itself, maybe in the exit, so if you have multiple exits you can use a different link in each one. As for the transition from one level to the next, you just initialize the Gameplay state with a new level as content, maybe put in a loading screen if it takes a while.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement