• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PureSnowX

Managing Game State/Screens without Managers and Singletons

8 posts in this topic

Hello forum!

Something that has been quite a hassle for me too wrap my head around is the managing of screens/states.
Basically I have a base class called "State" or "Screen" that each State/Screens inherit from.

Each screen has a boolean called "finished" that returns true if the state is done and should be deleted/switched with another state. The state must also have support for childs, better known as states withing states such as InventoryState that acts within the WorldState ( Actual game ) or PauseState that is another state above WorldState.

Childstates are small states and pieces of game logic inside a the State stack that tells the Game-Loop to NOT de-allocate the states before itself, ( Seeing as it's a child of it and is supposed to go back to it soon ) as well as having the ability to determine if the parent state should update and/or render itself WHILE the childstate is active! Think a small popup window when you level up inside a battlestate, the battlestate still renders but does not update logic other than animations.

This is roughly the system I'm trying to achieve, coding it isn't very hard it's more how I grant access to the states to push new states into the stacks.

I think the best way would be to pass the stack wrapped inside a class with the method only to push new states into the the stack, that way a class only have access to the feature they SHOULD and WILL use.

But I'm not sure, is this unnecessary? Is their a cleaner way to do it?
Thanks beforehand!
0

Share this post


Link to post
Share on other sites
One way is to have a class that is responsible for managing the stack of states (main, popup, pause, etc) and allocating/deallocating states and state progress.

But another possibility is to treat the main states and popup windows as different concepts, and have exactly one main state active and zero or one popup screens. This simplifies state management, but would prevent you from having popup screens in popup screens (but that might only be good thing).

Pausing could also be an state attribute rather that a state in itself. Also to give you simpler state management, although the state themselves get slightly more complex.

Anyway, the concept of a game state is kind of singular and managed, meaning the game will always be in *a* state (not none or several), and that state switches are very "global" in nature. So some form of manager or single point of access is not entirely inappropriate.
0

Share this post


Link to post
Share on other sites
I usually use state classes that use the run and return successor pattern. Basically every function called on a state object returns a smart pointer to the state that the system should be in now. This can be a pointer to the original state. This allows inter-relation of states to happen without whatever is holding the state to explicitly know what's going on. For example, a state can go to a pause mode by constructing a pause state object and passing itself to the pause state constructor and returning a smart pointer to that pause state. When the appropriate event occurs, the pause state can return the original state object to transfer control back. Similarly, your level up state could be constructed with a pointer to the previous battle state; then the level up state's render code can call the battle state's render code first before rendering on top of what the battle state displayed. This avoids needing anything like a singleton or a manager class.
1

Share this post


Link to post
Share on other sites
I think you are talking about an abstraction called "Finite State Machine" ([url="http://en.wikipedia.org/wiki/Finite-state_machine"]http://en.wikipedia....e-state_machine[/url]). If the states itself run as FSM, then you are talking about "Hierarchical FSM".

I usually prefer separating the FSM logic from the game/app logic, using a pattern called "Delegate" ([url="http://en.wikipedia.org/wiki/Delegation_pattern"]http://en.wikipedia....egation_pattern[/url]): each State delegates its execution on another object. In http://github.com/pacobarter/LibSHFSM I use (java) Interfaces for State and Transition, so being less intrusive in the Screen/Game/... object hierarchy. I mean, using Interfaces in this way, you can forget about the FSM logic and concentrate on your Game logic. Edited by pacobarter
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1346067643' post='4973749']
Basically every function called on a state object returns a smart pointer to the state that the system should be in now. This can be a pointer to the original state. This allows inter-relation of states to happen without whatever is holding the state to explicitly know what's going on. For example, a state can go to a pause mode by constructing a pause state object and passing itself to the pause state constructor and returning a smart pointer to that pause state.
[/quote]

I've done this myself and it works very well. However the one thing you want to watch for, especially on Android, is how frequently you allocate for new instances. A more efficient way could be to have state behavior classes stateless, and any state information needed is held elsewhere in a long-lived structure associated with your agents/entities/game objects/whatever you want to call them. If the state behavior class is stateless, then you can reuse the same instance during the entire simulation for any entity. This may be a premature optimization using C++ targeting x86, but it netted me quite a bit of performance on Dalvik.
0

Share this post


Link to post
Share on other sites
I I really liked your answer of the Run and Return Successor pattern!


But I have a few questions, you say that it should return a smart pointer? How would that work? Look like?
Since if you "reset()" a smart pointer it will release it's current pointer. So the passed state would be invalid, since the smart pointer will call delete on it. ( I'm currently talking about the pointer residents in the context ( "Game-class Main-loop" ).


Also I'm a bit confused overall, but I'm guessing that it means "returns a smart pointer" is basically the ( using SFML-library )
"State::update()" && "State::updateEvent()" would return a smart pointer, as a reference? or a full scaled copy of it?

Maybe you could provide a snippet of how it could "look like" game-loop wise that is, it would be great! I'v searched on google but fund very little, more so in C++ code.


EDIT:
To clarify a bit of my question!


Ex:
We go from Overworld-state into the Pause-state, it's constructed from inside the Overworld-state's like:
return unique_ptr<State>(new PauseState(this));
Then later we come back and go back to the Overworld state from the PauseState like:
return unique_ptr<State>(overworldState);
THIS TIME HOWEVER, we want Pause-state to be de-allocated, when we switch back to the Overworld-state, how can that be determined?

Kind regards, Victor Karlsson aka Moonkis
0

Share this post


Link to post
Share on other sites
This is amazing! :D
I'v tried googling the pattern but haven't find anything or very little about it, maybe you know some good resources?

Also is this possible to do without the shared pointers and still receive the same functionality?
I'd really like to know!

Thanks again for that awesome explanation!
0

Share this post


Link to post
Share on other sites
It's not a complicated pattern, which is why there isn't much literature on it. [url=http://c2.com/cgi/wiki?RunAndReturnSuccessor]This[/url] should be sufficient to explain it. You could do it without shared_ptr. As I mentioned, something like intrusive_ptr would also work, as would hand rolling your own reference counting scheme. Or you can use raw pointers and somehow deal with the ownership problems manually. I don't really see much point when there are existing solutions that work out of the box.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0