Sponsored By

Opinion: A Singleton Tale

In this reprinted <a href="http://altdevblogaday.com/">#altdevblogaday</a>-opinion piece, former KmmGames senior game programmer Michael Carr-Robb-John shares a story about tracking down a particularly annoying singleton bug.

Michael Carr-Robb-John, Blogger

November 23, 2011

2 Min Read

[In this reprinted #altdevblogaday-opinion piece, former KmmGames senior game programmer Michael Carr-Robb-John shares a story about tracking down a particularly annoying singleton bug.] It was a Thursday afternoon in early spring and to put it quite simply, I was irritated! The game I was working on had a noticeable CPU spike when a specific GUI button was pressed on the front-end menu. If it was a fraction of a second, it probably wouldn't have bothered me so much, but this specific pause was in the magnitude of two to three seconds (debug build). It wasn't in a time-critical part of the game so it was ignored for weeks, which eventually turned into months. The "GUI guy", a junior programmer, was being kept busy in other areas, and as such this pause was not given any attention. Until, that is, one Thursday afternoon in late spring when I got so annoyed by it I took a look at what was happening with that button. Naturally with GUI controls, pressing a button triggers a callback, and this specific callback was straightforward enough it simply executed a script. The script looked innocent, it set some global flags and modified some world data values. Tracing the code line by line. The scripts set the global flags with no problem however when it tried to set the first world data value interesting things started to happen. The game realized that the World Manager (a singleton) was not currently in existence, and so it created the default world. This process involved also instantiating other singleton managers including loading default scene data, a physics world, the navigation system, etc. Ah ha, gotcha. A few hours later, I had rewired the world data values to be buffered for use when we really want the world to come into existence and the button no longer generated a two to three second pause. And I felt calm descend upon the world again. The moral of this story is don't ignore odd strange pauses that don't make sense, and beware bloody singletons that do what they say on the tin even if it's wrong. [This piece was reprinted from #AltDevBlogADay, a shared blog initiative started by @mike_acton devoted to giving game developers of all disciplines a place to motivate each other to write regularly about their personal game development passions.]

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like