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 of 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 straight forward 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.
As I felt calm descend upon the world again the moral of this story, don’t ignore odd strange pauses that don’t make sense and beware bloody singleton’s they do what they say on the tin even if it’s wrong.