Skip to main content
GameDev.net gamedev.net
🔒 Locked

Referencing Objects When Creating a New Object

Started by DarkZlayer Nov 27, 2008 at 12:16 PM 0 replies 550+ views
Original Post
DarkZlayer
DarkZlayer
So I'm programming a game like Advance Wars so I have many different objects such as the camera, units, tiles, etc. A lot of the time I find I have to pass in a lot of the same parameters into methods of each object to do the "logic" of different objects. I was wondering, how bad of practice it is to just reference the objects in the constructor of each in order to avoid having to continuously pass in these parameters. I also ask because it feels like I am doing it incorrectly when I will call the draw method for my interface with a few things that, that methods doesn't use but rather a method it calls within itself (such as drawing a certain section of the GUI). If I'm not clear on what I mean just let me know. I am terrible with techincal names of things, but I think I used the correct terms.
Antheus
Antheus
Quote:
Original post by DarkZlayer
So I'm programming a game like Advance Wars so I hav
I also ask because it feels like I am doing it incorrectly when I will call the draw method for my interface with a few things that, that methods doesn't use but rather a method it calls within itself (such as drawing a certain section of the GUI).


Sometimes, that is an indication that you should reverse the calling order. Rather than have draw() call foo(), foo() should be calling draw.

You're not clear on responsibilities and dependencies, so your systems need to know more than necessary. There is no trivial answer, but it's a common problem when unfamiliar with a certain domain, when you don't know up-front what exactly you will need, or who is responsible for what.

This is sometimes a problem in A-uses-B-uses-C, where B is either redundant from functional perspective, and exists only because it seems it should due to OO design. Several solutions exist. One is have C depend on A: B-uses-C(A), the other is to reverse the order A-uses-C-uses-B.

Or many other options, cause of the problem is almost certainly incorrect abstractions of functionality or responsibilities.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.