Jump to content
  • Advertisement
Sign in to follow this  

Global objects ... ?

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!