Sign in to follow this  
MarteI

reference question

Recommended Posts

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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this