# Organizing of game classes!

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

## Recommended Posts

Hello!

I've been programming a game in C++/SDL for the last week and I've actually made a lot of progress. Anyways, I've got some problems that i need some help solving since this is my first ever attempt at making a game.

I've got a Game class which basically had access to everything and sort, draw and handles events.

Then i got a class called "Entity" which is a interface for everything that moves, the classes "player", "enemy" and "projectile" inheritances this interface.

I want both the player and enemy class to be able to create a projectile at any time. At the time only the class player can. In the projectile class i then need to check for collisions so that i know if the projectile hit something.

The problem is that if the player and enemy should be able to create a projectile i need to include that header file. Then if i want to check for collisions on the player and enemy i need to include those files. This in turn creates a loop where i in player.h includes projectile.h and i projectile.h i include player.h

I hope i make myself clear.

Greatly appreciates help!

I'm going to go ahead and post a file of how my program is built atm. You can open the program in Dia (http://dia-installer.de/)

##### Share on other sites
You can declare a class without including its headers, just ensure to refer it, don't use any constructors or methods of it:

File A.h:
[source lang="cpp"]
// declare B
class B;

class A {
B* myClassBRef;
}
[/source]

File B.h:
[source lang="cpp"]
// declare A
class A;

class B {
A* myClassARef;
}
[/source]

File A.cpp
[source lang="cpp"]
#include "A.h"
#include "B.h" // include B now to access constructor and methods

void A::myMethod() {
this->myClassBRef = new B();
}
[/source] Edited by Ashaman73

##### Share on other sites
I see. So i should just declare the class and then include it's code in the .cpp file instead of the header one.

Going to try that out asap! Thank you.

I've actually got another question if that's ok

The enemies needs to check for collisions on the environment and player.
The player needs to check for collisions on the environment, enemies.
The projectile needs to check for collisions on the environment, enemies and player.

In my entity class I've got a functions that looks like this:
 virtual void movement(parameters) = 0; 

Can i somehow override the parameters since every class needs receive something different in order to function correctly.

Thank you very much for the speedy response once again!

##### Share on other sites
Just pass in a pointer to an array/structure/class. Each type of class can handle it how it wants then. Edited by mark ds

##### Share on other sites
Yeah, at the moment one of the parameters goes something like this
 (vector<Tile*> &mapTiles); 

so basically what you are saying is that i should pass on every vector onto every class and then just chose to ignore some of them when necessary? Edited by Tallkotten

##### Share on other sites
I've done everything as you'd suggested and it all worked!!!

and

THANK YOU VERY MUCH!

Everything works nicely now!

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632580
• Total Posts
3007189

×