adding variables in if statements
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.
adding variables in if statements
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.
Thanks for you help!
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]
Creator of: The Reign of The Lords Era,The Gnats Franken Dungeon.
Re: adding variables in if statements
Code: Select all
equals="$($turn_number|+5)"
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."
- Celtic_Minstrel
- Developer
- Posts: 2222
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: adding variables in if statements
The problem here is that
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 —
+
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.Re: adding variables in if statements
Also note that you wrote instead turn_nummber.