Problem with creating a combination lock in Twine Sugarcube2

Twine Version: 2
Story Format: Sugarcube2

Hi!

First time twine user here…

I am trying to build a combination lock in my game. I can get the variable assignments to work, and the response. The problem is even when the wrong combination is entered it still says lock opened.
I’m pretty sure I am doing something wring in the if/elseif statements but can’t figure out what.

Here is what I am working with.

You see here a lock. Can you crack the combination?

The dials read: 
<<set $dial1 = "Three">><<cyclinglink "Two" "Three" "Four">>
<<set $dial2 = "Blind">><<cyclinglink "Mute" "Blind" "Deaf">> 
<<set $dial3 = "Mice">><<cyclinglink "Goats" "Mice" "Voles">>

[[Open it!|Open]]

<<if $dial1 is "Three" and $dial2 is "Blind" and $dial3 is "Mice">> You opened the lock! Unfortunately the suitcase lock isn't actually attached to a suitcase, but it's still a decent accomplishment.

<<else>> Blast! That wasn't the combination! 

<<back>> 

<<endif>>

Also, I based the idea and the script for this on https://www.glorioustrainwrecks.com/node/5020

You need to put the variable inside the cycling link macro or else the value you’re selecting will never be assigned to anything. Like this:

<<cyclinglink $dial1 "Two" "Three" "Four">>

If you take a closer look at the link you posted, that’s also how they’re doing it.

Right now your code is basically doing this:

a = 1
if (a == 1) "You win!"

You don’t need to use the old <<cyclinglink>> macro, since the built-in <<cycle>> macro has replaced it.

Also, rather than sending them back and forth between two pages, you could do it all on one page like this:

<<set _comment = []>><<cycle "_dial1">>
	<<optionsfrom ["Two", "Three", "Four"]>>
<</cycle>> | <<cycle "_dial2">>
	<<optionsfrom ["Mute", "Blind", "Deaf"]>>
<</cycle>> | <<cycle "_dial3">>
	<<optionsfrom ["Goats", "Mice", "Voles"]>>
<</cycle>>

<<link "Open it!">>
	<<if _comment.length === 0>>
		<<set _comment = ["That didn't work.", "Still not opening.", "Blast! That wasn't the combination!"]>>
	<</if>>
	<<if _dial1 + _dial2 + _dial3 == "ThreeBlindMice">>
		<<goto "Open">>
	<<else>>
		<<replace "#comment">><<= _comment.pluck()>><</replace>>
	<</if>>
<</link>>

<span id="comment"></span>

That will randomly display one of three comments if they try the wrong combination (edit the _comment array to change the number and/or type of comments), or send them to the “Open” passage if they try the correct combination.

Please let me know if you have any questions about how that works.

Hope that helps! :slight_smile:

1 Like

Hi! Thank you… I tried that but putting the variable in with the macro wouldn’t let the macro work. That’s why I was looking for alternates.

Hi!

Thank you!! That worked perfectly!!

1 Like

Wow, nice work!

I wonder if there’s a way of putting a timer or ‘attempt 1, 2 etc’ so that you can have a sense of urgency and danger in the mix?

If one wanted to add a timer I do have some “Countdown Timer” sample code already.

And if you wanted to count attempts or limit the attempts, it would be easy enough to add that code within the above <<link>>'s code.

1 Like

Would you have to set a value to the ‘Open it!’?

Like a minus -1 per click and then an ‘if’ variable to another passage or something?

There are lots of ways to do it. For example, you could change the <<link>> code something like this:

<<link "Open it!">>
	<<if _comment.length === 0>>
		<<set _comment = ["That didn't work.", "Still not opening.", "Blast! That wasn't the combination!"]>>
	<</if>>
	<<if ndef _count>>
		<<set _count = 1>>
	<<else>>
		<<set _count++>>
	<</if>>
	<<if _dial1 + _dial2 + _dial3 == "ThreeBlindMice">>
		<<goto "Open">>
	<<else>>
		<<if _count < 4>>
			<<replace "#comment">><<= _comment.pluck()>> I've got <<= (4 - _count)>> tries left<<if _count == 1>> until the alarm triggers<</if>>.<</replace>>
		<<else>>
			<<goto "Alarm">>
		<</if>>
	<</if>>
<</link>>

If _count isn’t defined, which it won’t be initially, then it sets it to 1, otherwise it adds 1 to it (the “++” operator means “add 1”, for specifics see the increment operator). At four failures it then sends you to the “Alarm” passage.

Enjoy! :slight_smile:

1 Like