[variable] wiki clarification

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
Pentarctagon
Project Manager
Posts: 5565
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

[variable] wiki clarification

Post by Pentarctagon »

Reading the wiki on [variable], there are two things I'm not completely clear on:

1) equals and not_equals are specified as being string comparisons, however greater_than/less_than/greater_than_equal_to/less_than_equal_to do not specify a type. Am I correct in assuming they are only intended to function with numeric values? They do not give the correct result when used with string values, in any case.

2) equals and not_equals are stated as being better performing compared to their numeric and boolean counterparts. Is this performance difference at all noticeable? It just seems odd to have this called out specifically here, especially since it would seem to be a micro-optimization at best.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: [variable] wiki clarification

Post by gfgtdf »

Pentarctagon wrote:Reading the wiki on [variable], there are two things I'm not completely clear on:

1) equals and not_equals are specified as being string comparisons, however greater_than/less_than/greater_than_equal_to/less_than_equal_to do not specify a type. Am I correct in assuming they are only intended to function with numeric values? They do not give the correct result when used with string values, in any case.
Yes the comparision operator onyl apply to numerical values.
Pentarctagon wrote: 2) equals and not_equals are stated as being better performing compared to their numeric and boolean counterparts. Is this performance difference at all noticeable? It just seems odd to have this called out specifically here, especially since it would seem to be a micro-optimization at best.
Not sure but i doubt that. Actuall I'm not even sure that it is acually faster since the wml object stores integers already as integer internally, not as strings. If you want performance use lua i'd say.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Iris
Site Administrator
Posts: 6798
Joined: November 14th, 2006, 5:54 pm
Location: Chile
Contact:

Re: [variable] wiki clarification

Post by Iris »

Regarding point 2, I’d bet my posterior bits that the page in question was written well before WML value type coercion was implemented (i.e. before version 1.9.0, released in August 2010), so back then it was most likely 100% positively faster to use string-based comparisons since all WML values were internally stored as strings. Nowadays it would certainly be slower. Performance issues aside, if someone’s handing you a screwdriver (integer/boolean comparisons) and it seems to be the most elegant tool for the job, then you don’t need the hammer (string comparisons).

Nevertheless, if you’re working with a special case where micro-optimizations make a significant contribution to your code’s performance then you should consider either using Lua or not using Wesnoth.
Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
User avatar
Pentarctagon
Project Manager
Posts: 5565
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [variable] wiki clarification

Post by Pentarctagon »

Yeah, I'm not asking #2 because it matters to anything I'm doing, I just thought it was an odd thing to include. If it's actually wrong though, I'll remove it from the wiki.
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
doofus-01
Art Director
Posts: 4131
Joined: January 6th, 2008, 9:27 pm
Location: USA

Re: [variable] wiki clarification

Post by doofus-01 »

Pentarctagon wrote: 1) equals and not_equals are specified as being string comparisons, however greater_than/less_than/greater_than_equal_to/less_than_equal_to do not specify a type. Am I correct in assuming they are only intended to function with numeric values? They do not give the correct result when used with string values, in any case.
I'm curious what that comparison would even mean for strings. String length?
BfW 1.12 supported, but active development only for BfW 1.13/1.14: Bad Moon Rising | Trinity | Archaic Era |
| Abandoned: Tales of the Setting Sun
GitHub link for these projects
User avatar
Iris
Site Administrator
Posts: 6798
Joined: November 14th, 2006, 5:54 pm
Location: Chile
Contact:

Re: [variable] wiki clarification

Post by Iris »

If you were programming in a general-purpose language like C++ or C that would be the lexicographical order function result. For example, “abc” < “abcdef” could yield true, “ABC” >= “ABC” would always yield true*, and “01234” < “#####” could yield false, depending on the exact locale and collation method used.

* As long as you don’t bring alternate Unicode representations and zero-width characters into the equation, anyway.
Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
User avatar
Pentarctagon
Project Manager
Posts: 5565
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [variable] wiki clarification

Post by Pentarctagon »

shadowm wrote:Performance issues aside, if someone’s handing you a screwdriver (integer/boolean comparisons) and it seems to be the most elegant tool for the job, then you don’t need the hammer (string comparisons).
Also due to a bit of a quirk with boolean/string types, this comparison just plain doesn't work:

Code: Select all

[event]
  name="start"
  
  {VARIABLE test "true"}
  
  [if]
    [variable]
      name="test"
      equals="true"
    [/variable]
    [then]
      [message]
        message="hi"
      [/message]
    [/then]
  [/if]
  
  [message]
    message="$test"
  [/message]
  
[/event]
The "hi" message doesn't get displayed at all, and the "$test" message displays the text "yes".
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
Ravana
Forum Moderator
Posts: 3010
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [variable] wiki clarification

Post by Ravana »

I have used this when I explicitly want strings

Code: Select all

#define ORM_BOOLEAN_FORMAT VAR
	[if]
		[variable]
			name={VAR}
			boolean_equals=true
		[/variable]
		[then]
			{VARIABLE {VAR} ORM_bool_true}
		[/then]
		[else]
			{VARIABLE {VAR} ORM_bool_false}
		[/else]
	[/if]
#enddef
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: [variable] wiki clarification

Post by gfgtdf »

Pentarctagon wrote: The "hi" message doesn't get displayed at all, and the "$test" message displays the text "yes".
Yes that's becasue in 1.13 the [set_variable] implementation was moved to lua,a dn when a wml table is moves to lua both "true" and "yes" get mapped to the lua boolean value true whihc gets mapped to "yes" when its converted back to wml.

Ravana wrote:I have used this when I explicitly want strings
Just wondering: when would you actuall want want such strings instead of bools ?
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Ravana
Forum Moderator
Posts: 3010
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [variable] wiki clarification

Post by Ravana »

I wanted to use [switch], which is implemented so that it needs string.
User avatar
Pentarctagon
Project Manager
Posts: 5565
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [variable] wiki clarification

Post by Pentarctagon »

gfgtdf wrote:
Pentarctagon wrote: The "hi" message doesn't get displayed at all, and the "$test" message displays the text "yes".
Yes that's becasue in 1.13 the [set_variable] implementation was moved to lua,a dn when a wml table is moves to lua both "true" and "yes" get mapped to the lua boolean value true whihc gets mapped to "yes" when its converted back to wml.
I figured it was something like that. I just brought it up since it's pretty counter-intuitive just from looking at it, and did actually break something in my add-on.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
Post Reply