The project that I'm working on right now is the largest scale thing I've ever done in my life. The conventions that I started out using really wound up becoming a pain in the ass when the project got bigger. I switched from C to C++ because I thought the organization and OO approach would be nicer (and it was for the most part).
My problem is class dependencies. I have several main classes in the game that are all dependent upon each other. I have a Level class that requires a Player class for several of its functions, a System class that requires the Level, Player requires System, Player requires Input, and so on. It's like a spiderweb of class dependencies.
When I was doing the project in C, I had one global object for each. I had a global Player, System, Level, and Input object. All of their members were public. So any time, in any class, I could access another class. This worked nicely, but I was sure it wasn't the best practice that I could be using.
When I switched to C++, I started trying to pass references to these member functions. For example, the Collision function in the Player class would take a reference of the Level so that it can access the map to see if the player is hitting a solid. This worked out for a few weeks, but I literally wanted to vomit when the game got larger. I was spending sooo much time just worrying about passing things around the program, that I completely scrapped that idea. Some classes and functions require a reference of both a Level and a System. Some need a Level and Input. I just got TIRED of it.
What I'm doing now is ultimately like I began with in C:
Code: Select all
class Example {
public:
static Example &GetInstance() {
static Example instance;
return instance;
}
private:
Example();
};
Code: Select all
Example &Obj = Example::GetInstance();
I am extremely interested in hearing your solutions to this sort of extreme class dependency. Sure, I could probably make things a little less dependent upon each other, but it's a major pain in the ass. Ultimately I will still always have classes that require other classes. I'd like to find a solution that is safe, easy to use, and doesn't require lots of extra work to implement.