Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

To simplify, let's say in en engine I'm coding for a game, I have the Main class an Object class. In the Main class' update method, I loop through the array of all Objects in the game. I also have a pool in the Main class from where I can get and return Objects.

Main.update () { for (all Objects) { Object[i].update() } }

My question is what's a good way of creating new Objects from an existing object's update method?

Do I have to pass the Main class to the Objects? or make the Main a static class?

Also another question is how to handle object destruction where I return them to the unused pool.

Thanks in advanced.

share|improve this question

1 Answer 1

The easiest way to plumb the connections is to create an intermediary class that all game objects know about, much like every program knows about the OS it's running in. This class is responsible for taking requests to create or destroy a game object and passing it onto your Main class.

This intermediate communications class can be one of several things: a singleton, static methods in the Main class, or a class that's created and whenever a game Object is created it gets a pointer. That last one is cleaner, but will add storage required for each game Object and if you have tens of thousands every little bit adds up.

For your case, a small engine, I prefer the static methods version because you don't have to store pointers and while the communications API is a singleton the class it's talking to is not. And there's a clear concept of lifetime.

The reason to separate a little class to handle communicating like this is to keep it simple for all your game Objects, and to keep your Main class from getting littered with a bunch of message logic.

Your Main class is running through all the game Objects, then when the loop is done the Main class then asks the intermediary class what it needs create and destroy and then it does.

The reason to gather up all the Object requests is that it's pretty bad to add and delete list elements while you're looping through that same list.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.