Game Design practice for accessing container of game objects

I'm working on my first video game. So far I have a few classes in the game starting with the Game class which includes a list of GameObjects (another class). There are several classes that inherit from GameObjects used to implement things like bullets, explosions, various enemy types, etc.

The game essentially iterates through the list of GameObjects to update/render them. I would like to provide access to the Game's list of GameObjects inside another class (like the Bullet class) so I can put new objects on the list. For example, when a bullet hits, I want to add an explosion to the Game's GameObject list it can be updated/rendered.

Anyone have any suggestions on how this should be setup? I was considering adding a pointer to the Game or GameObject list to the GameObject class (and methods to access it), but I was wondering if there is a better way to set this up?


I think this is a case where the Game object should be global. Either make the GameObject list public or provide methods to access it.
You should use a model-view-controller structure to design the game. This eliminates the need to focus on just one small minute aspect of the game and help you group different objects into specific classes.
http://blog.codinghorror.com/understanding-model-view-controller/

My suggestion:
Game (model) class should have a method called getListOfObjects(). And this method should do exactly what the name suggests - return all game objects in the game

Game should have a list of observers, i.e. objects to be notified when the game has changed (gameChanged). As the game is running, it should be call gameChanged on each observer in this list and they should do different things like updating the view, redrawing objects, or in your case, playing a sound for when the bullet hits something

Assuming you have a way of checking if an object collides with another, you should add a play_sound method to the GameObject class. Then when an object like a bullet for example has collided with something else, it should play it's sound.
You can make this as simple as each object having just one sound it makes, or you can make it as complex as using having different sounds for different states of the object - footsteps fading, etc

Hope that helps
Hello,

I once did something similiar to you...

There's two ways of doing this: Make your vector/string or whatever you use a public variable. Just iterate through the vector and when a bullet for example collides with an object, play its destructionanimation and the destruction Sound;


1
2
3
4
5
6
7
if (CheckCollision(Bullet,Wall) == true) //just put your vector here like GameList[n] here and Loop through it. make sure to check what Kind of Gameobject it is!!! you could also take the last element of the vector and delete it after executing the necessary things until the vector is empty again
{
    Bullet.isDestroyed = true;

    Bullet.Animate(); //<-- When isDestroyed == true play the appropriate animation
    Bullet.PlaySound("sounds/explode.wav",1) // 1 means playing one time
} 


and the second way is with an access-function like std::vector<int> Class::GetObjectList()...

So basically its the same as Smac89 suggests... If you can tell me whatAPI you use i may provide you some of my old code :)

If you got further questions about collisions dont ask me ^^' I'm stuck on tile based collision detection myself

HalfNOoB
Thanks everyone for the advice. I went the route of making the game object global with the object list being a public member.

HalfNOob, I'm using Allegro 5 for this project.
Topic archived. No new replies allowed.