Using Textbox-Input As Usable Numbers

Twine Version: Twine 2.3.16
Story Format: SugarCube 2.36.1

Hey,

back again with another question.
I want to use a textbox input as a number later on.
So using this wouldn’t really work:

Input a number here:
<<textbox "$yournumber" "">>

Cause I want to use the input like that later on:

<<if $yournumber gte 20>>
Show this text
<<elseif $yournumber lt 20>>
Show other text
<</if>>

How do I get my textbox-input to be saved as a usable number?
And how do I make sure the input really is a number when put in the textbox in the first place?

If you only want numbers, then instead of a <<textbox>> macro, you’d be better off using a <<numberbox>> macro.

If you’d like to limit the range of values, there isn’t an easy way to do that within the <<numberbox>>, but you could “clamp” the value when you exit the passage, like this:

[[Link text|Passage name][$numberVar = $numberVar.clamp(0, 100)]]

or like this:

<<link "Link text" "Passage name">>
	<<set $numberVar = $numberVar.clamp(0, 100)>>
<</link>>

That example uses the .clamp() method to limit the value of that variable to a minimum of 0 and a maximum of 100.

Alternately, you could display an error message if they chose something outside of the allowed range, like this:

<<link "Link text">>
	<<if $numberVar < 0 || $numberVar > 100>>
		<<replace "#errmsg">>
			''Error:'' Value must be a number from 0 to 100.
		<</replace>>
	<<else>>
		<<goto "Passage name">>
	<</if>>
<</link>>
<span id="errmsg"></span>

That will cause an error message to be displayed in the <span> under the link if they click on the link while the number is outside the allowed range. Otherwise the link takes them to the next passage.

Hope that helps! :slight_smile:

2 Likes

In addition to HiEv’s answers, you may also use a <<listbox>> (http://www.motoslave.net/sugarcube/2/docs/#macros-macro-listbox), then you’ll have complete control of the answer.

What's your age?
<<listbox "$Age">>
<<option 18>>
<<option 19>>
<<option 20>>
<<option 21>>
<<option 22>>
<<option 23>>
<<option 24>>
<<option 25>>
<</listbox>>

The reader will choose an age between 18 and 25. As I didn’t put quote marks, it’s a number. If for some reason I need the age to be a character value, I would go with <<option "23">>.

2 Likes

@HiEv
Thats exactly what I was searching for.
I looked up so much about textbox stuff without even thinking about a numberbox.
Kinda embarrassing.
Thank you!
@souppilouliouma
The listbox doesn’t really fit in for the specific thing I’m trying to do, but it will come in handy later on for sure.
Thank you