Escape commas

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
willis
Posts: 11
Joined: March 1st, 2012, 2:17 am

Escape commas

Post by willis »

I'm trying to make a character that yells random insults during attacks. I'm having trouble with strings that include a comma:

Code: Select all

[set_variable]
  name=insult
  rand="Have at you!","Come on then, you walking sack of meat!","Stand down, you fat-kidneyed knave!"
[/set_variable]
My problem is that the WML parser seems to use all of the commas as delimiters, so the above rand value appears to be a list of five strings instead of three. How do I escape the commas that are inside of the quotes?

I'm running Wesnoth 1.14
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Escape commas

Post by WhiteWolf »

Maybe there's a way to escape them inline, I don't know about that, but otherwise you can just do this:

Code: Select all

{RANDOM 0..3}
[switch]
    variable=random
    [case]
        value = 0
        {VARIABLE insult ("Have at you!")}
    [/case]
    [case]
        value = 1
        {VARIABLE insult ("Come on then, you walking sack of meat!")}
    [/case]
    ... same for 2 and 3
[/switch]
{CLEAR_VARIABLE random}
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
willis
Posts: 11
Joined: March 1st, 2012, 2:17 am

Re: Escape commas

Post by willis »

WhiteWolf wrote: March 2nd, 2021, 9:38 pmotherwise you can just do this
That worked, thanks!
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Escape commas

Post by Celtic_Minstrel »

There are a lot of places in the game where you can escape commas by enclosing the string in parentheses, like so:

Code: Select all

rand=("Have at you!"),("Come on then, you walking sack of meat!"),("Stand down, you fat-kidneyed knave!")
Unfortunately, I don't think that works here… but it probably should, so I think I'll implement it for 1.15.11. By the way, you probably also want these strings to be translatable:

Code: Select all

rand=(_"Have at you!"),(_"Come on then, you walking sack of meat!"),(_"Stand down, you fat-kidneyed knave!")
Or rather, since that doesn't actually work:

Code: Select all

{RANDOM 0..3}
[switch]
    variable=random
    [case]
        value = 0
        {VARIABLE insult (_"Have at you!")}
    [/case]
    [case]
        value = 1
        {VARIABLE insult (_"Come on then, you walking sack of meat!")}
    [/case]
    ... same for 2 and 3
[/switch]
{CLEAR_VARIABLE random}
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Escape commas

Post by vghetto »

It might be more efficient to put all the string texts in a [set_variables] then get a random index for one of them.
spares you the headache of a long [switch].
Post Reply