How to "kind of" make shared an object? :)

C

Crirus

I have this classes tree

GameManager 1 object
Game Multiple objects
Map 1 object
Player Multiple objects
Squads Multiple objects
Units Multiple objects

Now each Squad or Units have to know about data on it's Map

Say I have Game1 and Game 2
Game2 have different map than Game1
any unit1_1 of branch Game1 need to know about Map1
any unit1_2 of branch Game2 need to know about Map2

How to accomplish that?

Should I pass to Unit1_1 the Game1 instance?

I whould like the Map1 shared to all Game1 children objects somehow..the
same with Map2 and Game2

Right now I keep passing the map arround each time I need it way down to a
unit that move or do stuffs
 
C

Crirus

What I want to know is if it is possible to make some kind of xml tree
walking, so I can go up and access any ancestor of a node... i my case nore
is a reference to a class instance
 
C

Chris Dunaway

GameManager 1 object
Game Multiple objects
Map 1 object
Player Multiple objects
Squads Multiple objects
Units Multiple objects

I don't know fully what you are designing, but to my mind, Player should
not be on the same level as Game but rather a sub level of game:

GameManager
Game
Map
Player
Squads
Units

Player, to me is an element of a Map. It seems like it would be a child of
map. A GameManager contains Games which contain maps which contain players
which have squads of units.

If this is the case, then a single unit can traverse up through it parents
to get the map info.

Just my thoughts.
 
C

Crirus

the players are indeed childs of game.. was a TAB missing there
Map is a separate class only with data of the terrain
Players join to game not to map in my design



How to traverse up such a graph?
 
C

Chris Dunaway

the players are indeed childs of game.. was a TAB missing there
Map is a separate class only with data of the terrain
Players join to game not to map in my design



How to traverse up such a graph?

My only thought on that would be that each object would have a "parent"
property or perhaps a "ContainedBy" proprty that contains a reference to
the object that contains them. As each object is created, its parent
object could be set. I don't know how a heirarchy like this might affect
performance.

So a Unit could get the map data by using something like

Squad = Unit.Parent 'Get the squad the unit is in
Player = Squad.Parent 'Get the Player that owns the squad
Map = Player.Parent 'Get the map object for the player
Game = Map.Parent 'Get the game object for that map

There may be better ways, though
 
C

Crirus

Yes, that can be a solution, or even better to make a parent or grand parent
reference only in the classes that need it....
But I was wander if there could be a way to get this automatically without
my concern, but I guess a child class have no ideea about a parent that hold
it's reference
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top