+1 vote
asked by (150 points)
Hi All, i am very new to twine and writing a murder mystery in Sugarcube.  At the end, I want the player to type into a box who they think the murderer is.  How do I code it so that if their answer is wrong, it goes to a passage titled ‘wrong’ and if it is correct it goes to ‘correct!;?

2 Answers

–1 vote
answered by (22.3k points)

Put something like this into the StoryInit passage:

<<set $answer to "">>
<<set $correct to "Gardener">>

Set up the textbox like this:

Who killed the butler?
It was the <<textbox "$answer" "enter your answer" "passagename" autofocus>>.

This textbox is going to change $answer to be whatever the player types in, then send them to a passage called "passagename" - change the name to be whatever you want. Instead of then sending the player to two different passages you can just use an <<if>> clause in the approbriate passage to see whether they guessed correct:

<<if $answer.toLowerCase() == $correct.toLowerCase()>>

the .toLowerCase() function is going to convert both strings to be lower case to avoid punishing players unfairly for entering the correct answer with the wrong case.

commented by (131k points)

To handle the situation where the player accidentally types a space character with before or after their answer, you can use the Javascript trim() function combined with the Javascript toLowerCase() function suggested by @idling

Change the TwineScript within the passagename passage to the following.

<<if $answer.trim().toLowerCase() == $correct.toLowerCase()>>

... you would only need to trim() the value within the $correct variable if for some unknown reason you decided to add leading/trail space characters to it with you assigned the initial value to it.

0 votes
answered by (60.1k points)

I'd use a <<button>> macro.  For example:

The murderer is?
<<textbox "_suspect" "" autofocus>> \
<<button "Name Suspect">>
	<<if _suspect.trim().toLowerCase() is $murderer>>
		<<goto "correct">>
		<<goto "wrong">>

The story variable $murderer would need to be set to the correct answer beforehand and should be written in all lowercase—e.g. <<set $murderer to "professor plum">>.  The player's answer is trimmed and lowercased to ensure a match with the value in $murderer.

commented by (150 points)
This worked perfectly!  Thank you so much!!
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.