Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question about program flow...

Started by dhartles Dec 2, 2004 at 2:25 PM 1 replies 700+ views
Original Post
dhartles
dhartles
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
JimPrice
JimPrice
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.
dhartles
dhartles
Thanks JP.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.