formula with set_variable

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
IIIO_METAL
Posts: 202
Joined: January 18th, 2017, 5:03 pm
Location: japan

formula with set_variable

Post by IIIO_METAL »

It may be a stupid question, but I'm in trouble because I don't know the answer.
I want to addition using formula with set_variable, but it don't work.

Code: Select all

[set_variable]
	name = answer
	formula = $(1 + 2)
[/set_variable]
I want answer = 3, but the actual value is empty.
Also, if the number part is replaced with a variable, the character strings will be concatenated.

Code: Select all

a = 1
b = 2
[set_variable]
	name = answer
	formula = $($a| + $b|)
[/set_variable]
The value is 12.
I don't know what's wrong. Please tell me how to addition. :cry:
Creator of "Mountain Witch" & "Castle of evil spirit"
User avatar
Pentarctagon
Project Manager
Posts: 5564
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: formula with set_variable

Post by Pentarctagon »

Always put quotes around formulas.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
IIIO_METAL
Posts: 202
Joined: January 18th, 2017, 5:03 pm
Location: japan

Re: formula with set_variable

Post by IIIO_METAL »

Thank you, Pentarctagon!
I arrived at the correct way of writing.

Code: Select all

[set_variable]
	name = answer
	formula = "$(1+2)"
[/set_variable]
I got answer 3. :D
Creator of "Mountain Witch" & "Castle of evil spirit"
User avatar
Celtic_Minstrel
Developer
Posts: 2211
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: formula with set_variable

Post by Celtic_Minstrel »

If you're using [set_variable]formula= then the correct way to write it is formula="1+2" - you don't need the $ at all. Your example is no different from writing value="$(1+2)".

The difference between value="$(...)" and formula="..." is that in the latter you can easily reference the current value of the variable using self, whereas in the former you would need to write out the variable's full name preceded by $ in order to reference it. The following two tags are equivalent, for example:

Code: Select all

[set_variable]
	name=some_container[$i].sub_container[$j].some_value
	value="$($some_container[$i].sub_container[$j].some_value + 1)"
[/set_variable]
[set_variable]
	name=some_container[$i].sub_container[$j].some_value
	formula="self + 1"
[/set_variable]
Obviously there's no reason to use a formula for that specific example (just write add=1), but if you have a very complicated formula you want to calculate, and/or if the variable's name is very long, then using formula="" may be more convenient than value="$()".
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
IIIO_METAL
Posts: 202
Joined: January 18th, 2017, 5:03 pm
Location: japan

Re: formula with set_variable

Post by IIIO_METAL »

Thank you Celtic_Minstrel.
Your commentary was very easy to understand and was helpful.
Sure, 1 + 2 isn't as difficult as using mathematical formulas, but I thought I should practice with simple ones before trying complex math.
And I made a big mistake in the syntax. :oops:
But I'm not so shocked.
Well .. I think it's a lot less damaging than finding this mistake after writing thousands of lines of code. :lol:
Creator of "Mountain Witch" & "Castle of evil spirit"
Post Reply