reference question

Started by
2 comments, last by MarteI 19 years, 9 months ago
For my first 'game', I am creating a space simulator which puts merchants, pirates and police ships interacting in a planet filled galaxy. As I am designing the system first, I have chosen to implement this as a galaxy object that will contain the planets and ships. The problem I am having at the moment is how can the ships knowing about each other (ie. so the merchants can avoid the pirates etc). Am I right in thinking that this will have to be done by passing a reference to the galaxy to each of the ships? In that case, when I am creating a Ship object, how do I pass a reference to the current Galaxy object to the new Ship object (I believe the equivalent to what I am asking in Java would be the 'this' operator).
- Martel -
Advertisement
Maybe you could have a ShipManager class, that contains all ships. And do all shiplogic in that manager.
Ugh. As a general rule, avoid writing any class that would have the word "Manager" in its name. :P

Anyway. The Ships are being created within a Galaxy object yes? So, within those methods, "this" refers to the current Galaxy object (whether in C++ or Java), and you can pass it as a parameter to any functions or methods you like, including Ship constructors (assuming they are prototyped to take a Galaxy* (C++) or Galaxy (Java)).

Of course, there are other solutions, assuming there is only one Galaxy. You could have, for example, a 'protected static vector<Ship *>' inside the base Ship class, which individual Ships would then automatically know about, and have Ships add themselves upon construction.

If that doesn't answer your question then I'm going to have to see some code for what you've done so far.
Thanks for the replies.

I think I'll have to give each of the ships knowledge of the Galaxy object (as opposed to the second option) as they also need to interact with the planets.

I did a short test app and it looks like this will work fine.
- Martel -

This topic is closed to new replies.

Advertisement