• 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
shaqb4

How to implement gamestates

9 posts in this topic

I'm making a pong game with c++ that currently has two states, GameState and MenuState. The way it is now, both states are methods of the Game class. The Game class has all the instance data that both methods/states use (so paddles, ball, menu buttons, etc). Is it bad practice to make a gamestate a method rather than its own class?

 

This is how my main function is right now

 

#include "Game.h"

int main( int argc, char* args[] )
{
	Game game;

	game.Run();

    return 0;
}

 

And the Run function

 

int Game::Run()
{
	if (error)
	{
		return 1;
	}

    //The frame rate regulator, passed to GameState
    Basic_SDL::Timer fps;

	//Which player scored (left or right), passed to GameState
	int scoreKeeper = 0;

	rightPaddle.setAI(true);
	
	//While the suer hasn't quit
	while (quit == false)
	{
		if (!paused)
		{
			GameState(fps, scoreKeeper);
		}
		else
		{
			MenuState();
		}
	}
	
    //Clean up
    clean_up();
}

It works fine this way, I'm just not sure if making each state a method is looked down upon. Any other advice on how to implement game states would be appreciated also smile.png

Edited by shaqb4
0

Share this post


Link to post
Share on other sites

Its simple create an enum, I used the c++11 style here.  Then do a switch over the possible enums that repesent state.  Inside each case begin the code that handles that state, a simple example is below.  Consider creating a function/method for entering and exiting a state, this can handle code that needs to be run when a state starts or ends.

 

One other possibility is using a class and inheritance/polymorphism to create State class that does something similar, to this, but using inheritance and polymorphism to handle your state.  Doing it that way would give you the ctor and dtor to handle entering and exiting state as well as the object's fields to hold related data.

 

If you have a simpler game use the enum system, if its more complex consider using a polymorphic state class.

 

 

enum class GameStates
{
State_None = 0,
State_Menu,
State_Playing,
State_Loading,
State_Paused,
};
 
class Game
{
public:
GameState mState;
};
 
// inside your Game class's Run function ( or inside the GameState() ) do this 
switch (mState)
{
case GameState::Menu
    {
    UpdateMenuState();
    DrawMenuState();
    break;
    }
 
case GameState::Playing:
    {
    UpdatePlayingState();
    DrawPlayingState();
    break;
    }
case GameState::Paused:
    {
    // don't update playing state
    DrawPlayingState();
    break;
    }
default:
    {
    // error
    break;
    }
 
}
 
Edited by EddieV223
2

Share this post


Link to post
Share on other sites

I like to actually have a base class called State and derive from that to push and pop different states, but that might be a bit much for a pong game. For pong I'd just go with the enums above haha.

 

I have a StateManager that you can push and pop states to/fro, and the Game class just calls the update function for the current state in its own main loop. A small game engine I wrote demonstrates it: https://github.com/DrSuperSocks/Fission

 

Hope that helps!

0

Share this post


Link to post
Share on other sites

Is it bad practice to make a gamestate a method rather than its own class?

 

For games with only a handful of states and simple transitions between states, it isn't bad practice at all.

 

When you start developing more advanced games with a large number of states, state machines within states, and complex interactions between states, it becomes more important to develop a more advance state machine framework.

0

Share this post


Link to post
Share on other sites

Have you looked at Lazy Foo's article on State Machines? It's uses enums and a case/switch function, and is relatively simple to understand.

Basically, you have a base GameState class that is abstract.  It has three virtual functions - handleInput(), logic(), and draw().

Then, your other states - in this case, the Menu and the actual Pong state - will inherit from this base class.  They'll inherit the three vital functions, and then you can put what you want in each respective state (e.g; a c_Player in the Pong state, or a c_Button in the Menu state).

The states are switched first by preparing the change with a set_next_state() function, then calling change_state() to actually change states.

I'd recommend going through the article, it really simplifies the whole state machine topic (at least it did for me tongue.png)

 

http://lazyfoo.net/articles/article06/index.php

 

You can even apply the same guidelines for an AI state machine too, which is pretty neat.

Edited by johnmarinelli
0

Share this post


Link to post
Share on other sites

Also, a more slimmed down version of the game state system does away with the game state manager or a similar concept. Instead, it just uses a group of GameStates, and each returns a GameState object at the end of the Update() function.

 

The game state can return itself, so it continues running every frame, or a different state if game conditions require that you switch to a different one. It's a bit harder to wrap your head around if you're still learning OOP, but for many games it can work pretty well without the overhead of coding a manager class.

1

Share this post


Link to post
Share on other sites

Thanks, all the answers are great! I don't think I'll need the more complicated state machines for a while, but it's nice to at least have an idea of how people do it. I can see from the responses that there're a lot of ways to implement game states. Are there reasons to use one way over another in specific situations, or is it more just a matter of preference?

0

Share this post


Link to post
Share on other sites

To me implementing a state-machine feels equivalent to making a Rube-Goldberg-Machine. There are variables already in a programming language, why implement a god-variable to combine various things from the whole program only to try to interpret its value in a huge switch case statement a moment later to separate the things you just combined to essentially just simulate how a variable works using a variable? Or even worse build dozens of vapor classes with virtual methods, one per case, to disguise that switch?

Why not just make decisions at those points where they naturally appear with a bit of local data?

0

Share this post


Link to post
Share on other sites
Because games naturally tend to have macro states that frequently have little shared behavior between them. There isn't that much common code between the higher logic of a loading screen and your main game play state. Not to mention mini-games, setting game options, and possible genre specific states like diplomacy screens for a 4X, inventories for RPGs and so on.
0

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