Jump to content
  • Advertisement
Sign in to follow this  
stenny

State Manager

This topic is 3751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Gamedev, If you'd like to skip all information and code, go to the last paragraph (even though goto's normally are bad programming habits :p) I've got a problem with the design of my State Manager. It's for a simple engine I'll be using for multiple games, so it's gotta be flexible. Ok, listen up. I've got two classes called cStateManager and cState. In the first one is a vector<cState*>, and a push function called Push(cState *State) for pushing states on the stack:
//prototype:
cState;

// the classes
cStateManager
{
private:
  std::vector<cState*>;

public:
  Push(cState *State);
};

cState
{
  //unrelated functions
};



The problem is that cState is only a class I'm gonna use to make derived classes from. These are gonna be the gamestates. For Example
class cMainGameState : public cState
{
  // unrelated stuff
};



The problem is that cStateManager::Push() only takes cState as an argument and not it's derived classes. Is there a way to make this possible, or am I making a bad designdecision in the first place? How are other people handling this? - Stijn

Share this post


Link to post
Share on other sites
Advertisement
If I've understood correctly there shouldn't be a problem as any class inherited from cStateManager IS an instance of cStateManager. Try calling Push() on your inherited class and it should work.

Share this post


Link to post
Share on other sites
As the previous poster said, it shouldn't be a problem. You could just call push with a pointer to one of the derived classes, such as:

manager->Push(new cMainGameState);

You should be able to find some more information if you google polymorphism.

Share this post


Link to post
Share on other sites
Then I was right after all. Hmm, I guess the problem must lie somewhere else.

Ok, thanks guys :)

Share this post


Link to post
Share on other sites
Do you redefine methods inherited from CState in the subclasses and call them from CStateManager? Then don't forget to declare those methods VIRTUAL. For example:

class cState
{
virtual void update() = 0; // pure virtual method
};

class cMainGameState : public cState
{
void update()
{
// update the main game state
}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Do you redefine methods inherited from CState in the subclasses and call them from CStateManager? Then don't forget to declare those methods VIRTUAL.


Yeap, I already do. Thanks for the information, though :) (might help other people reading this).

Anyway, the problem's solved. The problem was within the header/source-files organization. Stupid me. Thanks again, guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!