Im trying to create a game states system based on this http://gamedevgeek.com/tutorials/managing-game-states-in-c/ using AngelScript (the engine I use is Urho3D). I made a mistake, I created the states first, and then added the state manager. Everything stopped working, and I discovered that the problem was that derived classes methods were not called, only the ancestor class methods. This is my code:
class GameState //the game states inherit from this class
{
GameState() {}
void Init() {Print("Gamestate init");}
void Leave() {}
void HandleUpdate(StringHash eventType, VariantMap& eventData) {}
void HandleKeyDown(StringHash eventType, VariantMap& eventData) {}
void HandleKeyUp(StringHash eventType, VariantMap& eventData) {}
void HandleControlClicked(StringHash eventType, VariantMap& eventData) {}
}
The game state manager
#include "Scripts/Engine/GameState.as"
class StateManager
{
StateManager()
{
states.Clear();
}
void SubscribeToEvents()
{
//SubscribeToEvent(scene, "SceneUpdate", "HandleUpdate");
SubscribeToEvent("KeyDown", "HandleKeyDown");
SubscribeToEvent("KeyUp", "HandleKeyUp");
SubscribeToEvent("UIMouseClick", "HandleControlClicked");
}
void Push(GameState st)
{
states.Push(st);
states[states.length-1].Init();
}
/**
Removes state on top
*/
void Pop()
{
//exit current state
states[states.length].Leave();
states.Pop();
}
void HandleUpdate(StringHash eventType, VariantMap& eventData)
{
states[states.length-1].HandleUpdate(eventType, eventData);
}
void HandleKeyDown(StringHash eventType, VariantMap& eventData)
{
states[states.length-1].HandleKeyDown(eventType, eventData);
}
void HandleKeyUp(StringHash eventType, VariantMap& eventData)
{
states[states.length-1].HandleKeyDown(eventType, eventData);
}
void HandleControlClicked(StringHash eventType, VariantMap& eventData)
{
states[states.length-1].HandleKeyDown(eventType, eventData);
}
Array<GameState> states;
int current;
}
So, this code:
class MainMenuState: GameState
{
******
}
MainMenuState menu;
menu = MainMenuState()
stManager.Push(menu);
Instead of callin MainMenuState members, calls GameState members.
I tried to declare the methods in derived classes as override, but didnt solved the problem. Also tried declaring ancestor as abstract, but then I cant use it to declare parameters. How can I solve this?