Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GameStateManager: how to point to and update current state

Tags:

c++

So I've been trying to follow a blank-heavy guide for an engine dev assignment at University, and I'm having trouble working out how to use my GameStateManager to access and update the state at the top of the stack, also known as the TopState in my code.

It's pretty simple, I initialise and run the engine in main and set and push the first state:

void main()
{
    EApplication::Init();

    EApplication* pApp = new EApplication();

    pApp->GetGameStateManager()->SetState("TEST", new ETestState(pApp));

    pApp->GetGameStateManager()->PushState("TEST");

    pApp->GetGameStateManager()->UpdateGameStates(ETime::deltaTime);

    EApplication::RunApp();
    delete pApp;
}

however as you can see below the UpdateGameStates function in my GameStateManager is yet to do anything because I'm not sure how to access or point to the current state, and thus call its own corresponding Update function:

GameStateManager:

#include "AppGSM\EGamestateManager.h"

EGameStateManager::EGameStateManager(){ topState = new char[8]; }

EGameStateManager::~EGameStateManager(){}

void EGameStateManager::SetState(const char *szName, EBaseGameState *state){}

void EGameStateManager::PushState(const char *szName){}

void EGameStateManager::PopState(){}

void EGameStateManager::ChangeState(const char *szName){}

const char* EGameStateManager::GetTopStateName()
{ 
    return topState; 
}

EBaseGameState* EGameStateManager::GetTopState()
{  

}

void EGameStateManager::UpdateGameStates(float deltaTime)
{


}

void EGameStateManager::DrawGameStates(){}

I'm just wondering what I should be pointing to when I try and use GetTopState(), and how to access the update function of that state?

like image 649
Jake DM Avatar asked Nov 11 '22 19:11

Jake DM


1 Answers

How do you want to store your states? With "PushState" and "TopState" etc I assume your instructor wants you to either implement your own Stack of EBaseGameState* ponters.

A lot of assumptions now follow. I assume EBaseGameState is just an abstract Data Type and you have to implement all the different states that inherit from that state. So therefore a simple way to access the update function of that state would be to have the following:

class EBaseGameState
{
   public:
     //other functions omitted
     virtual void Update(float deltaTime) = 0;

};

class MenuState : EBaseGameState
{
   public:
     //other functions and implementation of Update() omitted
     void Update(float deltaTime);
}

Then in the calling code it would be as simple as

void EGameStateManager::UpdateGameStates(float deltaTime)
{
    topStatePointer->Update(deltaTime);
}

A good way of implementing a stack is using Vector and just adding newstates to the back of it and popping states off the back of it when needed, however why you store the name of the state is beyond me. Does your instructor want you to check the names? I would ask him/her some clarification

like image 200
Lloyd Crawley Avatar answered Nov 15 '22 06:11

Lloyd Crawley