-1

I am working on a game, and right now I have followed a GameState manager tutorial but I'm not sure why isn't the code working. It might be because it's an older version of c++ in the tutorial but I can't seem to find how can I fix it.

 #pragma once
 #include "GameEngine.h"
 class GameState
 {
 public:

virtual void Init() = 0;
virtual void Cleanup() = 0;

virtual void SplashScreen() = 0;

virtual void Pause() = 0;
virtual void Resume() = 0;

virtual void HandleEvents(GameEngine *game) = 0;
virtual void Update(GameEngine *game) = 0;
virtual void Draw(GameEngine *game) = 0;

void ChageState(GameEngine* game,
    GameState* state) {

    game->ChangeState(state);
}

  protected: GameState() {}
};

If I don't game the GameEgine *game and GameState *state and remove every line that uses them my program works okay, my window appears but I can't change the game states which is really important. If nobody has an answer that's okay :)

EDIT: I forgot to show the GameEngine.h file

#pragma once
#include "GameState.h"
#include "include.h"
class GameEngine
{
  public:
   Recources recource;
   void Init(std::string name, int x, int y);
   void Cleanup();

   void SplashScreen();

   void ChangeState(GameState* state);
   void PushState(GameState* state);
   void PopState();

   void HandleEvents();
   void Update();
   void Draw(sf::RenderWindow &widnow);

   bool Running() { return m_running; }
   void Quit() { m_running = false; }

private:
// the stack of states
std::vector<GameState*> states;

bool m_running;
};
  • To be fair, we are going to need a little bit more code than this to figure out the reason it is not working out for you. See if you can procure a [MCVE](https://stackoverflow.com/help/mcve) – Carl May 11 '19 at 05:59
  • You have circular includes, you need to forward declare one class in one of the headers and not include it – Alan Birtles May 11 '19 at 06:01
  • You didn't show the code that is trying to use `GameState` and `GameEngine`, so how can we possible tell you what is wrong with that code? That said, one thing I do see wrong is your two header files have a circular reference to each other. You need to break that circle, by using [forward declarations](https://en.cppreference.com/w/cpp/language/class#Forward_declaration). – Remy Lebeau May 11 '19 at 06:01

1 Answers1

1

Variable game(which you pass to function) in ChangeState must be object GameEngine(which has function ChangeState,which is different from ChangeState of GameState), so you can call game->ChangeState, so if GameEngine object which you use have neccessary function and you pass correct arguments, all will be fine.

Timur Kukharskiy
  • 303
  • 3
  • 16