:: The story of a lone developer's quest to build an online world
:: MMO programming, design, and industry commentary
[The Imperial Realm :: Miranda] [Blog] [Gallery] [About]

The One Man MMO Project: Haters Gonna Hate

By Robert Basler on 2013-02-09 12:47:09
Homepage: www.onemanmmo.com email:one at onemanmmo dot com

Singletons get a bad rap from programmers. Just go do a search for "singleton problem" and read any of the first 100 results. From all the anger you'd think they cause disease, or burn books, but no. They're globals, and programmers hate globals.

The recommended "solution" to the singleton problem is to pass the potential singleton to every class that needs it as a parameter so that you can see it is used in the declaration for the class. It's only one variable, or one parameter to a function call, so what? That's the approach I took for my entity system, dutifully passing it to every class that needed it, and having them store it internally if needed.

The problem that developed is that the ES is the central database of my game and it is used by dozens of different modules. Over time, the standard approach had amounted to a few ugly workarounds, hundreds of lines of code to maintain, tens of thousands of instances of one variable, and hundreds of thousands of function call parameters - every frame.

I'm extremely pragmatic and I've always taken other programmer's grand pronouncements on code purity with a large grain of salt. That was clearly an unacceptable trade-off just to avoid a global. Singletons are tools, use them appropriately.

By Marco Dennstädt on 2013-02-16 10:49:50
Homepage: email:M dot Dennstaedt at gmx dot net
Hi Mr. Basler,

I also think it is totally reasonable to use the singleton pattern. If it is correctly used might depend on experiences you made before. I'm excited when and why I get into trouble using the singleton pattern for my game window class.

p.s.: I came here after reading your post on gamasutra. Very interesting =)
By Justin Lloyd on 2013-02-16 13:23:41
Homepage: otakunozoku.com email:justin at imf dot la
You are an evil person and will burn in Hell for your sacrilege!

Actually, I agree with you. Singletons and globals and all other tools in a programmer's toolbox are just that... tools. Learn to use them and then use them appropriately. Any argument for or against something is just a religious argument. :)

New Comment

Cookie Warning

We were unable to retrieve our session cookie from your web browser. If reloading this page does not get rid of this message, please read this to learn more.

You will not be able to post until you resolve this problem.

Comment (You can use HTML, but please double-check web link URLs and HTML tags!)
Your Name
Homepage (optional, don't include http://)
Email (optional, but automatically spam protected so please do)
What color is a lime? (What's this?)

Admin Log In