Quick way to decide if a WML variable is numeric?

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
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Quick way to decide if a WML variable is numeric?

Post by WhiteWolf »

Hi,

The scenario is the following: The user will give a value, using [text_input] in a [message], and later I will want to compare that value to other variables. I wish to make the method idiot-proof. What is the best way to decide if the value the player gave is a valid number? And not, say 12ab5.
The only thing I came up with is to [if][variable] cointains="", for each character that is not numeric. Needless to say, this would be a terribly ugly and long solution.
Any other ideas?

Thanks,
WhiteWolf
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Quick way to decide if a WML variable is numeric?

Post by Ravana »

https://wiki.wesnoth.org/Wesnoth_Formula_Language#type should work, can be called from WML with if > have_unit > formula.

Checking if ipart of variable is equal to variable might also work.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Quick way to decide if a WML variable is numeric?

Post by Pentarctagon »

You could also use lua to create a conditional WML tag:

Code: Select all

function wesnoth.wml_conditionals.is_num(cfg)
  return tonumber(wml.variables[cfg.variable]) ~= nil
end
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
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Quick way to decide if a WML variable is numeric?

Post by WhiteWolf »

Thanks :)
Though in the meantime, I found a better way to handle numeric input with lua sliders: viewtopic.php?f=58&t=47728
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Quick way to decide if a WML variable is numeric?

Post by enclave »

I use this [if] block:

Code: Select all

	[if]
								[variable]
								name=variable_with_number_or_string
								greater_than=0
								[/variable]
								[and]
								[variable]
								name=variable_with_number_or_string
								less_than=100000
								[/variable]
								[/and]
								[or]
								[variable]
								name=variable_with_number_or_string
								equals=0
								[/variable]
								[/or]
								[then]
## this is number
[/then]
[else]
## the variable_with_number_or_string was NOT number
[/else]
[/if]
I think it works perfectly fine for WML... and is simple enough..
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Quick way to decide if a WML variable is numeric?

Post by Celtic_Minstrel »

WhiteWolf wrote: May 25th, 2018, 4:42 pm The only thing I came up with is to [if][variable] cointains="", for each character that is not numeric. Needless to say, this would be a terribly ugly and long solution.
It would make more sense to invert this - that is, use contains for each character that is not numeric, then enclose the whole thing in a not.

That said, the other methods mentioned in this thread are probably better.
Ravana wrote: May 25th, 2018, 5:01 pm https://wiki.wesnoth.org/Wesnoth_Formula_Language#type should work, can be called from WML with if > have_unit > formula.
I don't believe this would actually work, because in order to substitute a string variable into a WFL formula, you need to enclose it in single quotes; and if you do that, even a number will be considered to be a string by the type function. What might work is a formula something like `as_int('$var') != null()`.

The type function would work if the formula had more direct access to the variable, mind you. For example, this Lua code: wesnoth.evaluate_formula("type(self) = 'integer'", wml.variables.var). (Obviously you could also add a test for the decimal type if you want to accept real numbers.)
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply