adding variables in if statements

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
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

adding variables in if statements

Post by The_Gnat »

I have the following code. However I apparently do not know the correct syntax for addition in wml because it thinks it is just a string.

I want to check a variable compared to the turn number + 5.

Code: Select all

		[if]
			[variable]
				name=explosion_count
				equals=$turn_nummber|+5
			[/variable]
			[then]
				
				# when it reaches 5 then the mine explodes
				{EARTHQUAKE (
				[message]
					speaker=You
					message=_"Retreat!"
				[/message]
				)}
				{FLASH_WHITE ()}
				
				[endlevel]
					result=victory
				[/endlevel]
				
			[/then]
		[/if]
Thanks for you help! :D
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: adding variables in if statements

Post by Sapient »

Code: Select all

  equals="$($turn_number|+5)"
Note: the quote marks are important for this.

See https://wiki.wesnoth.org/SyntaxWML#Spec ... ute_Values
key = "$(formula-expression)"
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
Celtic_Minstrel
Developer
Posts: 2211
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: adding variables in if statements

Post by Celtic_Minstrel »

The problem here is that + has a meaning in basic WML - it concatenates two parts of a string together. While this is generally used only on quoted strings, it actually applies to unquoted strings as well. So when you write equals=$turn_number|+5, the WML parser reads this as if it were equals="$turn_number|" + "5" which means the value of equals ends up being "$turn_number|5" (and after variable substitution, if you're on turn 4 you'd be comparing to 45 which will obviously never work).

You're also not using the correct syntax here (in order to add things in WML you need a formula substitution, not just a plain variable substitution), but what I said above still applies if with a formula substitution — equals=$($turn_number|+5) would be parsed into the string "$($turn_number|5)" which after variable substitution has the exact same effect. This is why it's recommended to always enclose formulas in quotes even though it isn't strictly required - the quotes protect the + from the WML parser, allowing it to be passed through to the formula parser.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Ravana
Forum Moderator
Posts: 3002
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: adding variables in if statements

Post by Ravana »

Also note that you wrote instead turn_nummber.
Post Reply