C++ GameStateManager:如何指向和更新当前状态
因此,我一直在努力遵循大学引擎开发任务的空白沉重指南,我很难想出如何使用我的C++ GameStateManager:如何指向和更新当前状态,c++,C++,因此,我一直在努力遵循大学引擎开发任务的空白沉重指南,我很难想出如何使用我的GameStateManager访问和更新堆栈顶部的状态,在我的代码中也称为TopState 这很简单,我在main中初始化并运行引擎,然后设置并按下第一个状态: void main() { EApplication::Init(); EApplication* pApp = new EApplication(); pApp->GetGameStateManager()->SetS
GameStateManager
访问和更新堆栈顶部的状态,在我的代码中也称为TopState
这很简单,我在main
中初始化并运行引擎,然后设置并按下第一个状态:
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;
}
但是,正如您在下面看到的,myGameStateManager
中的UpdateGameStates
函数尚未执行任何操作,因为我不确定如何访问或指向当前状态,因此调用其相应的Update
函数:
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(){}
我只是想知道当我尝试使用
GetTopState()
时应该指向什么,以及如何访问该状态的更新函数?您想如何存储状态?使用“PushState”和“TopState”等,我假设您的讲师希望您实现自己的EBaseGameState*Ponter堆栈
现在有很多假设。我假设EBaseGameState只是一种抽象数据类型,您必须实现从该状态继承的所有不同状态。因此,访问该状态的更新函数的简单方法是:
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);
}
然后在调用代码中,它将像
void EGameStateManager::UpdateGameStates(float deltaTime)
{
topStatePointer->Update(deltaTime);
}
实现堆栈的一个好方法是使用Vector,只需将newstates添加到堆栈的后面,并在需要时从堆栈的后面弹出状态,但是我无法理解为什么要存储状态的名称。你的讲师是否希望你检查姓名?我会问他/她一些澄清哇,正确格式的代码,而不是一堆废话。从第一次用户那里还没见过。祝贺你不是僵尸;)我想从确定性状态方法(即
SetState
,PushState
等)开始是个好主意。。。还是我没有领会你的意思?为什么pApp->GetGameStateManager()->随便什么()代码>。。。非常详细,为什么不:GameStateManager&gs=*pApp->GetGameStateManager();gs.Whatever();gs.Whatever2()代码>我认为它更整洁,您键入的内容更少:)