common game practice question: singleton vs pointer passing

Discussion about everything. New games, 3d math, development tips...
Post Reply
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

common game practice question: singleton vs pointer passing

Post by Phunk »

It has come to my attention that there are 2 very different aproches to the architecture of the core of a game engine. Now I can understand that this very much relies on the type of game, but I have seen 2 mayor different aproches that do essentially the same.

Example of the singleton structure can be found in the YASS demo. There is almost every global class a singleton, so if I want to get the irrlicht device(for example in the network manager), that is hold by the game manager I type CGameManager::GetSingleton()->GetIrrlichDevice()

Example of the pointer passing is seen in Irrwizard, where almost every class is created with the gamemanager-pointer as argument.

I guess there is something to say for both methods. Are there performance issues involved?

just wondering wat is common practice among fellow irrlichters, and if there are general rules towards using the one or the other.

Greets
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

I use singletons, but I'm sure you'll get a 50/50 split response on this one.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

There should be no performance issues at this level.

With pointers you have to be a little more careful when it comes to memory management.Usually i still prefer them. One reason is that i have more control that way when initalizing them. With singletons you can have the problem that there is no guarenteed sequenze of initialisation and this can lead to bugs which are difficult to trace when there are dependencies between several singletons. Also with pointers i feel that i have a better defined access point. There's a single global variable and from there on i can trace any object in my game.

So i'm only using singletons, when i have to guarantee that there exists only a single object and even then i do sometimes wrap the access with other classes. (for example i use a singleton to initalize the random generator, but the classes i use to create random numbers do wrap that and can have any number of instances).

I've also worked on projects where everything was done with singletons and it wasn't that much of a difference. Maybe i missed a few arguments for singletons, but i just found it slightly easier to work with pointers.
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

Post by shurijo »

I've always tried to use singletons, just because it feels cleaner to code it that way. Plus, I don't need to worry about pointers or any of that :) If you code a Singleton correctly, its easily turned back into an non-singleton just in case you need to change it for some reason (just change the constructor back to public instead of protected or private and remove the getInstance static method, etc.).
Post Reply