Text field SugarCube 2

Hello

I can’t find information in any way.

I want to create a text field in the game in which the player must enter the answer from the riddle.

I need this field, when entering an answer, to compare with the real answer or several answers. If the answer is correct, the player moves on, if it is not correct, the field is cleared for a new input and an inscription appears under it that the answer is not correct.

I would be very grateful for the hint.

Twine Version: 2.3.9
Story Format: SugarCube 2.31.1

1 Like

Just use the <<textbox>> macro. I’d recommend bookmarking the link to the SugarCube documentation.

Using that, you could do something like this:

<<textbox "_input" "" autofocus>><<button "Submit Answer">>
	<<if ["test", "testing", "tested"].includes(_input.toLowerCase())>>
		<<goto "Next">>
	<<else>>
		<<replace "#output">><<= either(["Nope.", "Try again.", "Wrong."])>><</replace>>
	<</if>>
<</button>>
<span id="output"></span>

That would show a textbox and a “Submit Answer” button. When you clicked the button it would check the text in the textbox to see if it was one of “test”, “testing”, or “tested” (ignoring upper/lowercase distinctions, since all input is converted to lowercase). If it was one of those, it would send you to the “Next” passage. If it wasn’t one of those, the it would randomly display one of three messages below the textbox, within the “output<span> element.

For details on exactly how any of that code works, see the SugarCube <<replace>> macro, <<print>>/<<=>> macros, .includes() method, and either() function, plus the JavaScript .toLowerCase() method.

Enjoy! :grinning:

3 Likes