According to the “Fundamental Datatypes” page of the System Manual, “This lets you determine a given value’s type at any time…” But I don’t know where to find the code that one would use for this determination.
I need to determine whether a given chunk of stuff is an Integer, or whether it’s something else. My code uses this:
local words = str.split(' ');
local x = new BigNumber(words);
But it’s quite possible that the string being passed to the BigNumber constructor is not something like ‘37’ but perhaps something like ‘monkeys’. In the latter case, it’s at least possible that the value of x won’t be of the Integer type. If it is, I’m in trouble, because I need to trap the case where the string doesn’t contain a series of digits.
That’s what I was afraid of. So I guess the answer is, in order to determine whether a string contains only digits, I need to use a regular expression … a tactic I have managed to avoid up to now. I’m sure it’s not THAT hard (I hope).
It’s a good idea to parse and validate the string anyway, otherwise you would leak the internal workings of Tads syntax to the player. For example, the player would be able to enter something like “5.15e-10” and your game would accept it as valid input.
When you need to pass player input to internal functions that are not intended directly for the player, always validate the input.
You’d have to see the code for this to be clear. I want to be able to parse a wide variety of inputs, and produce a sensible response. If the player types ‘read page monkeys’, I don’t want the output to be, “The book does not contain a page 0.”
local pageNum = new BigNumber(words);
if( pageNum == 0 && words == '0' )
"The book does not contain a page 0.";
else if( pageNum == 0 )
words + " is not a number.";
else if( pageNum > numPages )
"There aren't that many pages in the book.";