Sign in to follow this  
Gravy

objects referencing other objects questions.

Recommended Posts

Gravy    100
This has got my mind running in circles.

This is just an example so I can't post my code. I haven't figured it out yet.

I've got a class that builds my game world. It calls other classes to do so. It all works fine. Now I'm working on other class for objects (like NPCs and such) in the world. I want them to access some of the worlds objects. The only way I know how to have object2 use object1's variables or objects is to send object1 to object2.

for example:

class WorldTime
{
public WorldTime(gameTime gameTime)
{
//this takes the game time and turns it in to world time
}
}

class TheWorld
{
WorldTime theTime = new WorldTime(gameTime);
//huge class that generates the world
//using theTime to do stuff like shadows
//or whatever its just an example so I'm winging it

}





Okay so I have the world now. I need some people in it, but they need to know what time it is. The only way I know how to access the world time is to send the world to that person.



class Person
{
//variables
TheWorld myWorld;


//constructor
public Person (TheWorld myWorld)
{
this.myWorld = myWorld;
}

public string myWatch()
{
return TheWorld.theTime.toString();
}

}




Now does each person create a "world" for them self's or does it just point to the world. I read an article sometime ago, that said there is 2 ways to do this; one way points to that object, and another way copies it. One way you need to add something to tell it what your doing. I don't want 100 people with 100 copies of the world running around.
I forgot the article's name and I have no clue what this is called, so google isn't much help.
Can anyone help.

[Edited by - Gravy on December 3, 2010 6:38:14 PM]

Share this post


Link to post
Share on other sites
Buckeye    10747
You're doing a lot of creating of worlds.

class Person
{
//variables
TheWorld myWorld; // create a world


//constructor
public Person (TheWorld aWorld) // create a world to send to Person
{
this.myWorld = myWorld; // delete original myWorld, create a new world and copy stuff from the aWorld
}

1. If you only need the time, avoid passing globals around, so why not Person::SetTime(time) when it's needed?

2. If there is other info needed from the global world, better to use a pointer. That doesn't involve creating worlds to send around, only requires a pointer.

E.g.,

class Person
{
public: // don't forget this
TheWorld *myWorld;
Person(TheWorld *pointerToGlobalWorld): myWorld( pointerToGlobalWorld ) {}
//...
void SomeFunction();
};

void Person::SomeFunction()
{
myWorld->theTime.toString();
//...
}

If you want to test some of your code, print out debug messages in the TheWorld constructor and destructor to see when the constructor/destructor gets called.

Also, if you really must send copies of some class around and copy it, remember the Rule of Three for classes. Google for that and understand it.

Share this post


Link to post
Share on other sites
Gravy    100
I was afraid of that.

I guess I should of started with I'm using C#, that was totally my bad. I don't understand your code, but "Rule of Three for classes" is a Cpp concept so I'm assuming your code is as well.

OOP is never what I think it is. I guess I'll have to go hit the books some more. Can I create just one world and have other things just point to it in c#?

I don't want global variables, I just want the objects to talk to each other. In my head that seems totally reasonable, but then again I'm always wrong about this sorta stuff.

If it will help I can give a example of what I'm trying to do. So maybe you can wrap your head around what I really trying to do, but the example I gave is pretty much what I'm trying to do.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by Gravy
Can I create just one world and have other things just point to it in c#?
Yes. Although the example code you posted initially is a little unclear, the field Person.myWorld does indeed point to an existing instance of the TheWorld class (which I gather is what you want).

Share this post


Link to post
Share on other sites
Gravy    100
Okay I think I got. I was doing a lot of thinking with hands and remotes and link lists.

So in a basic node:

class Node
{
private Node next;
Private Node last;

public Node Next{get; set;}
public Node Last{get; set;}
}




then I create 2 nodes temp1 and temp2
then temp1.Next = temp2 etc...
Now temp1.next points to temp2 with out creating another temp2.

so in the person class I can

class Person
{
private theWorld myWorld;

public theWorld MyWorld{get; set;};
}




Then:
Person bob = new person();
theWorld THEWORLD = new theWorld();

bob.MyWorld = THEWORLD;

Shouldn't bob.MyWorld just point to THEWORLD with out creating a new one?
Its the same concept as the linked list.


And I'm sorry about being unclear with my example. I guess I should of used a different example.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Then:
Person bob = new person();
theWorld THEWORLD = new theWorld();

bob.MyWorld = THEWORLD;

Shouldn't bob.MyWorld just point to THEWORLD with out creating a new one?
Your examples are still a bit unclear (I don't think they'll compile, for one thing), but generally speaking, the answer to your question is 'yes'.

(Just in case you're not already aware of this, I'll go ahead and mention that classes and structs have different semantics in C#, so while what you've stated would hold true for classes, it would not hold true for structs.)

Share this post


Link to post
Share on other sites
Gravy    100
Thanks for the quick reply, and to both of you thanks for the help its greatly appreciated.

And for the record its not suppose to compile, its a proof of concept kinda thing. Like a=b and b=c then a should equal c. It doesn't matter what a, b, or c are, it should hold true whatever numbers they are.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by Gravy
And for the record its not suppose to compile, its a proof of concept kinda thing. Like a=b and b=c then a should equal c. It doesn't matter what a, b, or c are, it should hold true whatever numbers they are.
Point taken, but when asking questions that are specific to a particular programming language, examples that are compilable (or at least syntactically correct) are generally more useful, IMO.

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