I'm trying to build a basic platforming game in C++ using SDL, and I've encountered this issue enough to know that there's something I'm missing.
What I'm working with, at the most basic level, is two classes, a GameState
class that manages everything, and a Player
class, that will move around.
The main GameState
object will have a list of obstacles that the Player
cannot move into, so I want my Player
move()
function to take a GameState*
in order to move based on various components of it.
However, I'm hitting a problem I've seen a bunch of times -- a circular dependency issue. The problem is that the GameState
object needs to have a Player
member variable to update as the game runs (so I need to import the Player
header to it), but I need to import the GameState
header into the Player
header in order to have my move function take a GameState*
as a parameter.
This doesn't work. Normally I'd just settle and have the Player
's move()
function just take a list of obstacles, but I'm kind of hitting this issue all over the place and there are multiple instances where I want to access other variables stored in the GameState
. Is this kind of behavior even possible? I've been racking my brain for a solution but I don't really know what the best way to fix this is, so I thought I'd ask.
Thanks in advance!
Edit: a simple example in code of what I'm talking about:
//in GameState.h
class GameState{
public:
// ...
Player* plr;
};
//in Player.h
class Player{
public:
// ...
move(GameState* state);
};