3 ms·
In libraries and many kinds of software, yes, especially on the web. However in games it can often be quite reasonable to use singletons. He gives a great examp
by Mithaldu 12y ago
In libraries and many kinds of software, yes, especially on the web. However in games it can often be quite reasonable to use singletons. He gives a great example with TheVoip. You're extremely unlikely to have a game that actually needs to handle multiple voip connections at the same time, nevermind voice inputs.
- eddd 12y agoSingletons in multicore environments are dangerous, one bottleneck to rule them all.
- ryanthejuggler 12y agoAlso, singletons are great in robotics control software. It removes a lot of complexity if you have one singleton corresponding to one device.
- wnoise 12y agoWhat happens if you add a second camera? The actor/CSP model makes each device a separate process, which can be hooked together with processing nodes works wonderfully.
- meepmorp 12y ago> What happens if you add a second camera? You call it TheSecondCamera, duh.
- sp332 12y agoTheCameraArray[1]
- ryanthejuggler 12y agoIf there's the potential for multiples then I'll have a CameraManager (TheCameraManager) that finds out what's connected to the system and provides enumeration and access. I'll generally try to make the cameras look like a singleton and hide the Camera class. Sort of like a factory, except you'll only ever get N instances of your Camera class for N cameras.
- midnightclubbed 12y agoUntil you port your multiplayer game to the Xbox One where each controller has it's own mic/headset.
- Zikes 12y agoI would imagine John Carmack ought to know how to handle and prepare for videogame ports by now.
- midnightclubbed 12y agoI'm sure he does, his games have been ported to a good number of platforms! That said he tends to be very PC-centric and in pushing that platform makes decisions that make for difficult ports (eg megetextures). For programmers who have not been through the process of evolving game design and engine requirements getting the balance of flexibility and productivity is tricky. Hoping the fact JC uses singletons does encourage them to be sprinkled more liberally than they already are!
- mbrock 12y agoSo then you change the code. Or make the singleton handle several things.
- midnightclubbed 12y agoYeah. Trade off is whether changing the code (which may have been referenced everywhere in the codebase) every time this happens cost more (in dev and debugging time) than not using the singleton in the first case. It's things like this that turn a 'sure, we can make this game split-screen' into 'you didnt tell us you wanted split screen, that's gonna take a while'.
- mbrock 12y agoWell, this is very hypothetical. I go by "you aren't gonna need it" and "do the simplest thing that could possibly work." Maybe I'll have to change a bunch of stuff when someone suddenly comes up with a split-screen requirement. I probably will have to change a bunch of stuff anyway. As long as the code isn't impossible to understand and change, I would in most cases prefer to add split-screen support once I know for sure I need it.
- qwer 12y agoThat still doesn't explain how the singleton is better / easier. It just explains why it's not that bad.