Torque Console Messages and Static Initialization

I figured I’d write this up for the one other person in the world that is going to run into this. Might save someone some time.

Several years ago I used to work at GarageGames on the Torque game engine. I worked on an internal version of the engine that was used for several of our games and by a few third-party games. While this version of the engine was never released, a whole bunch of our work ended up in TGEA at some point. So what I’m describing applies to at least some versions of Torque Game Engine Advanced (TGEA) as well [I have 1.8.2]. It may apply to the current iteration of the Torque engine – Torque 3D – but I don’t have it so I don’t know for sure.

Recently I was reviving a game that was written using an early version of this internal engine. I was updating the FMOD library and ran into a loading problem. While tracking it down, I found myself in sfx/fmod/SFXFMODProvider.cpp where the SFXFMODProvider constructor had a bunch of calls to Con::warnf(), but I wasn’t seeing anything in the console log. The problem was that the console log hadn’t been created yet because the SFXFMODProvider constructor is called during static initialization.

Taking a look at console/console.cpp, Con::printf(), Con::warnf(), and Con::errorf() ultimately call a static function _printf() which starts like this:

Guess that’s why I wasn’t seeing anything – we’re just throwing away all messages if we aren’t active yet!

So the obvious way to fix this is simply to save our messages instead of throwing them away and then to output them when we do become active. Easy as pie.

Now any messages that are sent using Con::printf(), Con::warnf(), or Con::errorf() during static initialization or before the console is active will show up at the top of console.log.

Hope the one guy who finds this useful in the future buys me a beer…