How to create dynamic text?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

How to create dynamic text?

Post by weeix »

I learned how to create static text from the tutorial.

But if my game have a score and I want to display it, what functions should I use?

I tried something like this in the main():

Code: Select all

	gui::IGUIFont* font = guienv->getFont("Resources/lucidahandwrite.xml");

	core::stringw str = L"You have <";
	str += score;
	str += L"> score now!";
	font->draw(str.c_str(),core::rect<s32>(20,20,0,0),video::SColor(255,100,100,100));
But It was compiled without any error (and result).

Please help me.

Thanks, V. :)
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Where are you calling "font->draw(...)"? It should be somewhere after "smgr->drawAll()" and before "driver->endScene()" in order to render the text on top of your scene objects and while the screen is still open for drawing.
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

You can use font->draw like you are, but it is better to use
str += core::stringw(score);
so it is a string, otherwise it might get added as a char which is no good. Also, it would be better to use a GUI static text element and just change its text.
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Post by weeix »

Ion Dune wrote:Where are you calling "font->draw(...)"? It should be somewhere after "smgr->drawAll()" and before "driver->endScene()" in order to render the text on top of your scene objects and while the screen is still open for drawing.
It worked :!: Thanks a lot Ion.
DtD wrote:You can use font->draw like you are, but it is better to use
str += core::stringw(score);
so it is a string, otherwise it might get added as a char which is no good. Also, it would be better to use a GUI static text element and just change its text.
I used 'core::stringw()' as you suggest. I didn't see a way how to change GUI static text. I read the 'addStaticText' Function reference, it say that I can use 'SetText()' to alter the text, but I can't find it in the reference.

Thank you :D
Brainsaw
Posts: 1177
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

setText is a member of the IGUIElement baseclass:

http://irrlicht.sourceforge.net/docu/cl ... 3ebf870656
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

weeix wrote:'addStaticText' Function reference, it say that I can use 'SetText()' to alter the text, but I can't find it in the reference.
IGUIStaticText is derived from IGUIElement, so you can also use all the functions in there (look at the inheritance diagrams on top of the class documentation).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
weeix
Posts: 23
Joined: Fri Jul 09, 2010 5:07 am

Post by weeix »

Thanks again everyone.

I love you guys.
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

weeix your avatar makes me hangry.
It look like so good :shock:
pyandrea
Posts: 11
Joined: Sun Jul 13, 2014 7:01 pm

Re: How to create dynamic text?

Post by pyandrea »

I read with interest the post but some points are still unclear to me (...being an Irrlicht newbie!).
Specifically, I have the same problem of weeix: I want to display the score (e.g. "Player 1: 3000 points"), that is, I would like to display a string consisting of a string ("Player 1:") appended to a (casted) variable ("3000") appended to a string ("points"). In this regard I am asking:

1) why is it better to use a GUI static text? Is it a question of memory usage?
2) how is it possible to change GUI static text?
3) how you solved the problem with setText (can you post some example code)?
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: How to create dynamic text?

Post by Granyte »

1 because when using a GUI statictext you can change the value of the text at any time during the scene and it will be drawn during the next guienvironement->drawall();
2 yes using settext()
3 as far as i know there is no issue about the methode only the OP could not find the documentation about it

--BONUS as far as i know

Code: Select all

str += core::stringw(score);
is not needed

Code: Select all

str += score;
works fine for most data type
Post Reply