Fandom

Urho3D Wiki

Game States

69pages on
this wiki
Add New Page
Comments0 Share

How to manage different game modes.

This is a bit inspired by this blog post http://blog.nuclex-games.com/tutorials/cxx/game-state-management/ and first used extensively in USP.

Though I did game states differently. I wanted game states that can run in parallel (like a pause game mode with a menu while the scene in the background is still visible) and I didn't use virtual functions but Urho's event system.

Game state class:

/// For stuff you want to share across game states.
/// (Could also be in your main application class.) 
class globals
{
public:
...
/// The current game states, so that game states can switch to another game state.
/// Watch out that changing a game state will delete the current one.
/// Also game states can be stacked on top of each other so they run parallel. /// This is used for a pause game mode.
std::vector<std::unique_ptr<game_state>> game_states;

/// Meyer Singleton
static globals* instance()
{
static globals g;
return &g;
}
}; /// For a game state system that can run multiple game states at the same time.
class game_state : public Urho3D::Object
{
public: /// Put all your nodes in this vector so that they are removed on /// game state deletion.
std::vector<Urho3D::Node*> nodes; /// Put all your nodes here for auto deletion. std::vector<Urho3D::UIElement*> gui_elements;
// there could be other vectors here with stuff that will also // be automatically removed in the destructor
game_state() : Urho3D::Object(globals::instance()->context) {}
/// Removes all GUI elements and all nodes in the 'nodes' vector.
virtual ~game_state()
{
for(auto e:gui_elements)
e->Remove();
gui_elements.clear();
for(auto n:nodes)
n->Remove();
nodes.clear();
}
// these three functions need to be here because of Urho3D::Object
virtual Urho3D::StringHash GetType() const {return GetTypeName();} // no idea if this is as supposed to be, but it works
virtual Urho3D::StringHash GetBaseType() const {return GetTypeName();} // no idea if this is as supposed to be, but it works
virtual const Urho3D::String& GetTypeName() const {static Urho3D::String name("game_state");return name;} // this could be correct
};

Example game state for a main menu:

/// The main menu displayed when starting the game.
class gs_main_menu : public game_state
{
public:
Urho3D::Node* node_rotating_flag;
Urho3D::Window* window_menu;
Urho3D::ListView* lv_levels;

gs_main_menu();
void update(Urho3D::StringHash,Urho3D::VariantMap&);
void HandlePlayPressed(Urho3D::StringHash,Urho3D::VariantMap&);
void HandleKeyDown(Urho3D::StringHash,Urho3D::VariantMap&);
void HandleClosePressed(Urho3D::StringHash,Urho3D::VariantMap&) { globals::instance()->engine->Exit(); }
// this could be used correctly
virtual const Urho3D::String& GetTypeName() const { static Urho3D::String name("gs_main_menu");return name; }
}; ... gs_main_menu::gs_main_menu() : game_state()
{ ... // each game state is subscribing to all Urho events he wants to receive // there can be multiple classes (like game states) registered to the same event, // Urho calls every subscribed one SubscribeToEvent(E_UPDATE,HANDLER(gs_main_menu,update));
SubscribeToEvent(E_KEYDOWN,HANDLER(gs_main_menu,HandleKeyDown)); } ... void gs_main_menu::HandlePlayPressed(Urho3D::StringHash,Urho3D::VariantMap&)
{ // replaces the current game state with a different one
globals::instance()->game_states[0].reset(new gs_playing);
}

You can also add game states so that they run in parallel:

globals::instance()->game_states.emplace_back(new gs_pause);

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.