Multi-line WML

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
User avatar
Glen
Posts: 77
Joined: August 20th, 2011, 10:59 pm

Multi-line WML

Post by Glen »

Getting unexpected characters at line start errors because of this code, I want to avoid writing a large list in one line, so how do I write this over multiple lines?

Code: Select all

[set_variable]
name = DraftRedChoicesLeader1
rand = "Pikeman", "White Mage", "Red Mage", "Longbowman", "Javelineer", "Swordsman", "Knight", "Lancer", "Shock Trooper",
"Elvish Ranger","Dwarvish Steelclad"
[/set_variable]
Last edited by Pentarctagon on August 17th, 2021, 3:10 am, edited 1 time in total.
Reason: [c] to [code]
User avatar
beetlenaut
Developer
Posts: 2825
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Multi-line WML

Post by beetlenaut »

You probably have a carriage return after the "Shock Trooper", which is why you are getting an error. The easiest thing to do is just let the line extend, and let your text editor handle the word wrap. However, I did come up with a couple ways to do what you asked:
  • You could use [set_variables] to fill an array and then use rand to choose an array index.
  • You could set two temporary variables that are each chosen from half the list, then use rand to choose which of the two temp variables to use as the final value.
  • You could use Lua for your coding. Its white-space formatting is less strict.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Spannerbag
Posts: 535
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Multi-line WML

Post by Spannerbag »

Hi,
Glen wrote: August 17th, 2021, 2:46 am Getting unexpected characters at line start errors because of this code, I want to avoid writing a large list in one line, so how do I write this over multiple lines?

Code: Select all

[set_variable]
name = DraftRedChoicesLeader1
rand = "Pikeman", "White Mage", "Red Mage", "Longbowman", "Javelineer", "Swordsman", "Knight", "Lancer", "Shock Trooper",
"Elvish Ranger","Dwarvish Steelclad"
[/set_variable]
Just my tuppence worth but if you're doing this to make your code easier to read, you could use one or more macros?
You'd still have a long line of WML but it wouldn't look long.

Otherwise, you can shorten the line by omitting the double quotes and spaces; i.e. replace
"Pikeman", "White Mage", "Red Mage", "Longbowman", "Javelineer", "Swordsman", "Knight", "Lancer", "Shock Trooper","Elvish Ranger","Dwarvish Steelclad"
with
Pikeman,White Mage,Red Mage,Longbowman,Javelineer,Swordsman,Knight,Lancer,Shock Trooper,Elvish Ranger,Dwarvish Steelclad
which might shorten it enough? It should still work (but I haven't tried it!).

Otherwise I'd go with beetlenaut's first suggestion, you could even make it into a macro and re-use it elsewhere if need be...

Afaik there is no "line continuation" syntax in WML that allows code like yours above to span multiple lines (but then I do my coding in a basic text editor so what do I know?).
I'm curious as to why you dislike long lines? Are you having problems with them?

And apologies for the long line in this post :)

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Soliton
Site Administrator
Posts: 1685
Joined: April 5th, 2005, 3:25 pm
Location: #wesnoth-mp

Re: Multi-line WML

Post by Soliton »

key1 = "value1", "value2" is the same as key1 = "value1,value2" It is not special syntax the string pieces are just concatenated. (What is special syntax is: key1, key2 = "value1", "value2" where actually two attributes are set in one line.)

You can write the value as one big string that spans multiple lines but then those newlines are actually part of the string. You can also use the + operator to concatenate explicitely and if you put it at the end of the line the next line will be concatenated without the newline being part of the string.

Code: Select all

	key1 = "value1," +
	"value2"
I would leave the commas inside the quotes since putting them outside is just misleading since they don't mean anything special unless you assign to multiple keys.
"If gameplay requires it, they can be made to live on Venus." -- scott
User avatar
Spannerbag
Posts: 535
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Multi-line WML

Post by Spannerbag »

Soliton wrote: August 17th, 2021, 2:35 pm

Code: Select all

	key1 = "value1," +
	"value2"
Wish I'd thought of that :doh:

-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2213
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Multi-line WML

Post by Celtic_Minstrel »

Just a note, you can still use + on non-quoted strings as well, eg:

Code: Select all

key1 = value +
       value2
This collapses whitespace to a single space, so the final value of the key in that example is "value1 value2".

So for example, if you prefer to keep the commas outside the quotes as in your original example, this works:

Code: Select all

[set_variable]
name = DraftRedChoicesLeader1
rand = "Pikeman", "White Mage", "Red Mage", "Longbowman", "Javelineer", "Swordsman", "Knight", +
"Lancer", "Shock Trooper", "Elvish Ranger","Dwarvish Steelclad"
[/set_variable]
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply