View Full Version : How to change TextLabelEntity text ?

08-11-2011, 03:38 PM
Hello. I need to change the TextLabelEntity text from time to time when an action is triggered. While debugging everything is okay till setting the text of that entity. It just doesn't work. Here's the code:

meatCountTxt = CreateTextLabelEntity(pBG, "MeatCounterText", 60, 40, "0");

I've printed the intToStr(number_of_killed_pigs).c_str() to log to make sure if it's not null but everything is ok. It looks like something is wrong with that setting text method. Can anyone help?

08-12-2011, 04:32 PM
Don't know if this is necessarily correct, but, according to the sources and docs, maybe this would work ?

GetMessageManager()->SetEntityVariable(meatCountTxt, 1, "text", Variant(intToStr(number_of_killed_pigs).c_str()));

It should, technically change the text of the label after 1 millisecond.

08-12-2011, 05:34 PM
You're both close - one thing wrong though. The "text" variable is not in the Entity - it's stored in the TextRenderComponent which the CreateTextLabelEntity helper function adds to draw it.

So the correct way is (ahoy, untested code ahead):


//or, to time it to happen later like eye776 said:
GetMessageManager()->SetComponentVariable(meatCountTxt->GetComponentByName("TextRender"), 1, "text", Variant(intToStr(number_of_killed_pigs)));

Note: I removed the .c_str() part because Variant can take a string. Also, inside Proton you can use toString(number_of_killed_pigs) if you want, it's a neat way of converting any # (int, float, mem address, etc) to a string.

If you aren't sure if a variable is at the entity level or the component level, check the source for where it is used. In TextRenderComponent it inits it like this:

m_pText = &GetVar("text")->GetString(); //local to us

If it was at the entity level, it would have looked like this:

m_pText = &GetParent()->GetVar("text")->GetString(); //stored in our parent entity, we do this for things meant to be shared like pos2d, color, etc