Jump to content

New line character in string.format


Recommended Posts

Posted

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

lss_ad_banner.png
Posted

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

Multi theft auto tools - replace cars and peds, move your map or compile your Lua files online!

programista php rzeszów

Need free webhosting for your small site? PM me. Need help with portforwarding? PM me. Do not PM me asking for help with scripting.

Having problems with port forwarding? Send me pm, I can do whole thing for you using TeamViewer (already helped about 20 people, no worries)!

Posted

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.

lss_ad_banner.png
Posted

dont ask me :P maybe somebody knows the reason

Multi theft auto tools - replace cars and peds, move your map or compile your Lua files online!

programista php rzeszów

Need free webhosting for your small site? PM me. Need help with portforwarding? PM me. Do not PM me asking for help with scripting.

Having problems with port forwarding? Send me pm, I can do whole thing for you using TeamViewer (already helped about 20 people, no worries)!

Posted

so this is not a string format but xml.. interesting ^^

Multi theft auto tools - replace cars and peds, move your map or compile your Lua files online!

programista php rzeszów

Need free webhosting for your small site? PM me. Need help with portforwarding? PM me. Do not PM me asking for help with scripting.

Having problems with port forwarding? Send me pm, I can do whole thing for you using TeamViewer (already helped about 20 people, no worries)!

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...