## Quick way to decide if a WML variable is numeric?

Moderators: Forum Moderators, Developers

Forum rules
• Please use [code] BBCode tags in your posts for embedding WML snippets.
WhiteWolf
Forum Regular
Posts: 396
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

### Quick way to decide if a WML variable is numeric?

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
Author of the Underness Series, consisting of V parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
Standalone works: The Ravagers - now for 1.14, with new bugs!

Ravana
Moderator
Posts: 1925
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

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

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.

Pentarctagon
Posts: 3659
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

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

Code: Select all

``````function wesnoth.wml_conditionals.is_num(cfg)
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

WhiteWolf
Forum Regular
Posts: 396
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

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

Thanks
Though in the meantime, I found a better way to handle numeric input with lua sliders: viewtopic.php?f=58&t=47728
Author of the Underness Series, consisting of V parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
Standalone works: The Ravagers - now for 1.14, with new bugs!

enclave
Posts: 927
Joined: December 15th, 2007, 8:52 am

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

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..

Celtic_Minstrel
Developer
Posts: 1291
Joined: August 3rd, 2012, 11:26 pm
Contact:

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

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.
Maintainer of Steelhive.