How to create a passcode in Twine 2 Harlowe

Hi guys, I need some help here! You see, I’m trying to create a lock in Twine using a password (Just to clarify: When I say a lock I mean basic Twine Harlowe lock. I don´t need any super complex wheel combination lock. I just need a basic (promt:) lock!) I have been trying to create it for several days, using several variations like this:

(set: $passcode to (prompt: "What is the passcode?", ""))
You entered $passcode.
[[Passage 2]]
(if: $passcode is 8)[(set: $randomvalue to it + 100)]
(else:)[It didn't work, again.]
[[Passage 3]]
$randomvalue

I have tried using other passcode variations recommended by other Twine Harlowe users but they either don´t work, or when I try to test it my screen turns white. If you know the answer, please post it here! You´ll help one very confused Twine user! :wink: House on a Tree :house: :deciduous_tree:

The problem is that the value that the player types in is a string, but the value you’re comparing it to is a number. 8 is not the same as “8” (at least not in Harlowe, it seems.)

I got your code to work by simply changing 8 to “8”.

(if: $passcode.trim() is "8")[(set: $randomvalue to it + 100)] 

The .trim() removes any leading or trailing spaces so you’re only comparing the number.

Alternatively, you could use (if: parseInt($passcode) is 8) which turns the string into a number instead. Whichever works best for you.

1 Like

Harlowe includes a (num:) macro for converting a String based numerical value into an actual number.

warning: Harlowe uses a custom Variable Reference class when storing some value types within Story/Temporary variables, it also uses custom implementation classes for things like Array and DataMap. This combined with the fact that Harlowe has been deliberately designed with limited support for using JavaScript functions and/or object methods directly within Passage content, means that some care needs to be taken when using standard JavaScript functionality.

2 Likes