Public Group

Global objects ... ?

This topic is 4151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So after making some OOP programs for the first time, I found myself running into some trouble with objects. Say I have 3 or 4 functions in my program, and in function 1 I create some objects. How do I allow functions 2, 3, and 4 to access those objects without having to pass through parameters (which seems ridiculous if I have alot of objects)? And then the only other thing I could think of would be a global object...which I don't know how much sense that makes. As for the parameters, if I have say 75 objects created in function 1, would I have to make an array or vector to hold all 75 of those objects, and then pass that array/vector through parameters to all of my functions?

Share on other sites
The first question to ask yourself is why you have so many objects, and why the functions need access to all of them.

Is there a way you can simplify the job of each function, so that it needs fewer objects? Is there a way to actually make the job of the functions part of the object itself, with a member function? Can you rearrange the design of the classes themselves to separate different tasks better?

If you have a lot of instances of a particular class (for example, 1200 bullets all flying around in a shooter game), then it makes sense to use a container, like a std::vector or std::list. In this case, the main question is, who "owns" the container? The container can be created either by a function, and then passed to other functions as a parameter, or as a global object.

There's nothing specifically wrong with having a few global objects in your program. However, you should try and minimize the number of places you access those global objects. Often times it is possible to eliminate globals entirely, or just make them restricted to a small module in the code. The Law of Demeter is generally a good goal to try and follow.

The other thing you might find useful is aggregation. This is where you have several related objects of different classes that should be treated as a bundle. For example, your shooter game might have a set of Bullets, a set of Guns, a set of People, and a set of Obstacles. All of those things are technically part of the same chunk of data - the game world.

Suppose you want to have many shootouts going on at the same time, each with different people involved and different obstacles, guns, and so on. What you can do is create an aggregate object, which contains your other objects:

class Shootout{public:   list<Bullet> Bullets;   list<Gun> Guns;   list<Person> People;   list<Obstacle> Obstacles;   // etc.};

Now you can pass around all of those related objects as a single coherent glob (a Shootout object) without losing any detail.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• Forum Statistics

• Total Topics
632618
• Total Posts
3007478
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!