Jump to content

New line character in string.format


MaddDogg

Recommended Posts

Hi!

I just ran into a problem, I didn't notice ever before.

I have a script, which is at one point supposed to send the player a message.

The text for this message is first read from an xml file, then put into a string.format function and finally used in guiCreateLabel.

It looks like this:

xml file:

<text catg="CHARACTER" identifier="LEVEL_UP_TEXT" text="You just leveled up!\nNew level: %d" />

 

Lua script:

GUI_LevelUp["text"] = guiCreateLabel(1606,480,148,32,string.format(getLanguageText("CHARACTER", "LEVEL_UP_TEXT"), getElementData(getLocalPlayer(), "level")),false)

My problem now is that the new line character ('\n') is output not as a new line but rather as the characters '\n' itself.

This is caused by string.format for some reason.

Now I don't really know, how to get the new line into the final string.

Can someone please help me with this issue?

And no, I can't just leave the xml stuff out of it and put the string directly into string.format ;)

Thanks in advance,

Madd

Link to post

try doing something like:

GUI_LevelUp["text"] = guiCreateLabel(1606,480,148,32,string.gsub(string.format(getLanguageText("CHARACTER", "LEVEL_UP_TEXT"), getElementData(getLocalPlayer(), "level")), "\\n", "\n"),false)

i didnt test it, but maybe it will work :P i had same problem when i was scripting in another programming lang few days ago

Link to post

Thanks! It worked!

But why is string.format escaping the new line character?

I mean, in the original string there is this character. But as soon as string.format processes it, it does that.

And it only happens, when I pass the string through a variable to it instead of directly writing it into the function.

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...