objects referencing other objects questions.

Started by
7 comments, last by Zakwayda 13 years, 4 months ago
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]
Boldly Going Nowhere
Advertisement
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.
Boldly Going Nowhere
I'm not a C# person, I'm afraid. Hopefully someone more familiar with it can give you some pointers.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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).
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.
Boldly Going Nowhere
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.)
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.

Boldly Going Nowhere
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.

This topic is closed to new replies.

Advertisement