Help With If, Else, and Go-To Code

I am using using Harlowe 3.0.2/Twine 2.3.1 browser. I am creating a password mechanic into my game using the ‘prompt’ variable e.g.

(set: $name to (prompt: “Your name, please:”, “Please Type In Name”))

If the player types in ‘James’ then the door opens. I have made this happen using this, e.g.

[(if: $name is “James”)[(go-to: “Alive”)]]

But if the player doesn’t type in 'James" I want it to go to a different passage. I have set up the code to be like this, e.g.

[(if: $name is “James”)[(go-to: “Alive”)(else:(go-to: “Death”))]]

(Sidenote: this code doesn’t bring up any ‘broken code warnings’). This does not go to the correct passage, just a blank page. Is there a way to set up an if/else that goes to separate passages?

Thanks in advance for responses.

1 Like

The correct syntax for an (if:) plus (else:) macro combination is

(if: $name is "James")[(go-to: "Alive")]
(else:)[(go-to: "Death")]

note: The two macro calls don’t have to be on separate lines, I just did that to make it easier to see that each of the macros has their own associated hook.

There is a potential issue with comparing the value entered with a mixed letter case String literal like “James”, what happens if the end-user enters “james” or some other letter case combination of the same value?

You can solve this issue by using the (lowercase: ) macro when you compare the enter value.

(if: (lowercase: $name) is "james")[(go-to: "Alive")]
(else:)[(go-to: "Death")]
1 Like

Works like a charm, thank you very much!