Game Development Community

GuiConsoleTextCtrl bug? Can't show metrics?

by Keith Johnston · in Torque Game Engine · 06/07/2005 (5:51 pm) · 2 replies

This is really weird - I know this used to work, and it scares me to see this not working, as it makes me wonder if something really bad is happening that I may have caused.

Basically, in the preRender method of GuiConsoleTextCtrl, I can see that mResult is set to a string that contains what you would expect during a call to display metrics.

Here is that function:

void GuiConsoleTextCtrl::onPreRender()
{
   if (mConsoleExpression) {
      mResult = Con::evaluatef("$temp = %s;", mConsoleExpression);
   }
   calcResize();
}

But when the GuiConsoleTextCtrl::render method is called, mResult is now set to "18" - yes, always the number 18! What the heck is going on here?

Thanks,
Keith

#1
06/08/2005 (7:48 pm)
Found the problem, though I'm still not sure if this is a bug I introduced.

I changed mResult to be a char array and I used dSprintf to copy the result from evaluatef into the char array. It turned out that a call to GuiControl::resize in calcResize wiped out the result.
#2
04/28/2009 (3:54 pm)
Yeah I ran into the same issue, I believe Con::evaluatef creates a string temporarily in memory so mResult is pointing to memory that won't for sure stay the same.

You can create an array of certain size and then use something to copy it like you said or dStrncpy.

What I did so it stays as char* and can be as large as it needs to be (sorta) is change the code from:
void GuiConsoleTextCtrl::onPreRender()
{
   if (mConsoleExpression)
   {
	mResult = Con::evaluatef("$temp = %s;", mConsoleExpression);
   }
   calcResize();
}
to
void GuiConsoleTextCtrl::onPreRender()
{
if (mConsoleExpression)
{
Con::evaluatef("$temp = %s;", mConsoleExpression);
mResult = Con::getVariable("$temp");
}
calcResize();
}
[/code]

This should work as well just as long as $temp doesn't change between onPreRender and onRender (shouldn't), by pointing char* at the same value (address) that $temp is holding.