Game Development Community

Bug in guiConsoleTextCtrl.cc

by Philip Engdahl · in Torque Game Engine · 01/12/2006 (11:09 pm) · 1 replies

Took me all day to track down this bug :(

There is a bug (design flaw?) in guiConsoleTextCtrl.cc in TGE 1.4 - the mResult member is a pointer to the result of an evaluatef() command, but it should be a string storing the result instead. The reason for this is that if another evaluate() is called after this one is called, the string this pointer points to will be changed. I discovered this problem by turning on the fps metrics and subsequently using the Con::getFloatVariable() commands - the metrics would be overwritten by the result of the Con::getFloatVariable.

Here's my modified guiConsoleTextCtrl.cc (changes in bold):

...
GuiConsoleTextCtrl::GuiConsoleTextCtrl()
{
   //default fonts
   mConsoleExpression = NULL;
[b]//   mResult = NULL;[/b]
[b]   mResult[0] = '[[6287c2f0b0e4b]]';[/b]
}

void GuiConsoleTextCtrl::calcResize()
{
[b]   // mResult is a member of this class - hence it will always exist.
//   if (!mResult)
//      return;[/b]

...

void GuiConsoleTextCtrl::onPreRender()
{
   if (mConsoleExpression)
   {
      [b]dStrncpy( mResult, Con::evaluatef( "%s;", mConsoleExpression ), MAX_STRING_LENGTH );
      mResult[MAX_STRING_LENGTH] = '[[6287c2f0b0e4b]]';
//      mResult = Con::evaluatef("$temp = %s;", mConsoleExpression);[/b]
   }
   calcResize();
}

...

[b]   // no need to test mResult
//   if (mResult)
//   {[/b]
      S32 txt_w = mFont->getStrWidth((const UTF8 *)mResult);
      Point2I localStart;

...

      dglSetBitmapModulation(mProfile->mFontColor);
      dglDrawText(mFont, globalStart, mResult, mProfile->mFontColors);
[b]//   }[/b]

and here's my modified guiConsoleTextCtrl.h (changes in bold):

...

protected:
   const char *mConsoleExpression;
[b]//   const char *mResult;[/b]
[b]   UTF8 mResult[MAX_STRING_LENGTH + 1];[/b]
   Resource<GFont> mFont;

...

#1
03/16/2006 (6:06 pm)
Thank you very much for this fix. Damn annoying bug, took some time to track it down, would have taken me even longer time to try to find a solution myself.