Page 1 of 1

String handling in IRRLICHT

Posted: Thu Dec 08, 2016 12:49 pm
by PixelBender
Hi all,

I have problems working with strings in irrlicht.
For instance I have a string like “model.3ds” in an XML file und want to use it as text label on a GUI element.
How can the content be transported from the xml file to the GUI label?

XML handling:

Code: Select all

            case io::EXN_ELEMENT:
            {
                if (core::stringw("startUpModel") == xml->getNodeName())
                {
                    StartUpModelFile = xml->getAttributeValue(L"file");
                }
 
In the case above StartUpModelFile hast the type

Code: Select all

 core::stringc 
while the

Code: Select all

setText() 
method expects

Code: Select all

const wchar_t*
 
When I have to enter a string hardcoded I would write: setText(L”blabla”);

But how am I expected to use the variable StartUpModelFile?

Code: Select all

setText(StartUpModelFile)
doesn’t work

Code: Select all

setText(StartUpModelFile.c_str())
neither.
Nor

Code: Select all

 setText(L(StartUpModelFile))
 
Any help appreciated

Greetings
Markus

Re: String handling in IRRLICHT

Posted: Thu Dec 08, 2016 3:14 pm
by CuteAlien
You can probaly do setText( irr::core::stringw(StartUpModelFile).c_str() );
Which is fine as long as you have only ansi strings (no international characters). If you have unicode or multibyte strings then you have to convert them (Irrlicht trunk has some functions for that).

Re: String handling in IRRLICHT

Posted: Fri Dec 09, 2016 9:17 am
by PixelBender
Hi CuteAlien,

that was exactly, what I needed.
Works like a charm :D

Thank you for your help.

From my non expert point of view those many different string types in c++ are a plaque.
I have problems to understand why in a small area like the irrlicht API it wasn't possible to concentrate on one string type
for all the function parameters instead of making endless conversions necessary.

Greetings
Markus

Re: String handling in IRRLICHT

Posted: Fri Dec 09, 2016 11:08 am
by CuteAlien
The problem is you still get all kind of different string types from all kind of places. C++ has to handle all those - it didn't create them in the first place :-)

If you know you have wide-string in your xml you can use getAttributeAsStringW instead of getAttributeValue.
Irrlicht used some places stringc (like logging and for labels). Which is a little unfortunate, but not easy to change without breaking all applications using Irrlicht.