+1 vote
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
by (23.6k 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:

<<nobr>>
<<if $answer.toLowerCase() == $correct.toLowerCase()>>
Correct
<<else>>
WRONG!
<</if>>
<</nobr>>

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.

by (159k 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.

<<nobr>>
<<if $answer.trim().toLowerCase() == $correct.toLowerCase()>>
Correct
<<else>>
WRONG!
<</if>>
<</nobr>>

... 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
by (68.6k 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">>
	<<else>>
		<<goto "wrong">>
	<</if>>
<</button>>

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.

by (150 points)
This worked perfectly!  Thank you so much!!
...