Sign in to follow this  
dhartles

Question about program flow...

Recommended Posts

Ok, So I'm using C# and Managed Direct X. I've got a map loading and an animated sprite walking around. I want to incorporate a 'battle screen' into the game (think Final Fantasy). My question comes in about the program control. Currently there is a Main loop that is continually calling a render method. Let's say upon collision detection, I want the change from the currently loaded tile map into the battle screen, what's the best way to handle that? I'm thinkng right now that I start from the TestCollisons method. Do I set the battle screen up as another form? Hide the current 'map' form, show the 'battle' form, and then once the battle is over hide it and show the 'map' form? I've still got the sit down and write this up on paper, but I figured I would get some input from the forum first. TIA, Dave

Share this post


Link to post
Share on other sites
OK - I'm using C++ and not C#, but I'll give you some idea of how I do this.

My main loop only has one call : StateManager->Update().

In statemanager there is a pointer to the currentState, which is a derived class from the base State class (so I have derived StateIntro, StateGame, StatePause etc. classes). There is a virtual Update function in the state class, so when I call StateManager->Update, I run the appropriate update class.

Enough words, here's a code snippet (with only the relevant bits in):


class StateManager
{
public:
void Update();

void ChangeToStateIntro();
void ChangeToStateGame();

StateManager() {currentState_ = new StateIntro;}

private:
State* currentState_;
};

void StateManager::Update()
{
currentState_->Update();
}

void StateManager::ChangeToStateInit()
{
delete currentState_;
currentState_ = new StateIntro;
}

void StateManager::ChangeToStateGame()
{
delete currentState_;
currentState_ = new StateGame;
}

class State
{
public:
virtual void Update() = 0;
};

class StateIntro : public State
{
public:
void Update() {//do stuff}
};

//etc.




Then you would check your collision code, as suggested, then call the appropriate 'changestate' method from the statemanager. Everything else should run fine!

Hope this helps (even though it's not C#),
Jim.

Edit : forgot to add that you get state entering and exiting functionality using the ctor and dtor of the state classes.

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