Quick way to decide if a WML variable is numeric?
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.
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
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
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
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.
Checking if ipart of variable is equal to variable might also work.
- Pentarctagon
- Project Manager
- Posts: 5564
- 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)
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
take one down, patch it around
-2,147,483,648 little bugs in the code
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
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
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
Re: Quick way to decide if a WML variable is numeric?
I use this [if] block:
I think it works perfectly fine for WML... and is simple enough..
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]
- Celtic_Minstrel
- Developer
- Posts: 2216
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Quick way to decide if a WML variable is numeric?
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.
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 likeRavana 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.
`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.)