Just a little bit help with the IF code

If you are requesting technical assistance with Twine, please specify:
Twine Version: Twine 2
Story Format: Harlowe 3.3.3

(if:$key's "pickupkey" is false)[(link:"Key")[(set:$pickupitem to $key) (set:$key's "pickupkey" to true) (set:$key to 1) (alert:"You've got a key") (goto:"Pick up item")]]

The code turns red just like this:

I’m new to Twine. I looked up on a video about datamap and did exactly the same code as the video did. The “Key” passage didn’t disappear as expected when I came back.

Thank you for helping me!

1 Like

note: you make reference to a tutorial video but didn’t include a link to it, which makes it difficult to determine what other code is required to make the example code you did supply work.

1: I’m going to assume that you’ve pre-initialised the $key variable some time before you execute the code in your example, ideally within your project’s startup tagged Passage. That initialisation would likely look something like the following, except yours may have additional properties…

(set: $key to (dm: "pickupkey", false))

2: You mention a Key Passage but the code example makes no reference to such, so I will assume that the code example is contained within a Passage with that Name. Unless you meant the Key Link didn’t disappear, which may be due to the following

The (link:) macro’s Hook in your example is overwriting the Data-Map referenced by the $key variable with a Number value of 1, which will cause the data-map to be discarded. This means that if the example code is executed again later the (if:) macro’s conditional expression will cause an error to be shown, because the Number 1 doesn’t include a property named “pickupkey”.

eg. the following line is an issue because it changes the data-type of the $key variable

(set: $key to 1)

But besides the about issue, as the code currently stands a Key link will appear if the $key variable contains a Data-Map that the has a “pickupkey” property set to false, and after the end-user has selected that link and interacts with an Alert dialog the contents of a Passage named Pick up item gets shown.

Or at least that’s what happened when I ran the following TWEE Notation based test project…

:: Startup [startup]
(set: $key to (dm: "pickupkey", false))

:: Start
(if: $key's "pickupkey" is false)[
	(link: "Key")[
		(set: $pickupitem to $key)
		(set: $key's "pickupkey" to true)
		(set: $key to 1)
		(alert: "You've got a key")
		(goto: "Pick up item")

:: Pick up item
The content of the **Pick up item** passage.

I found my problem is when I get back to the room containing the key, I just reset the link to false again and that’s why it doesn’t go away. I’ve simply created another passage contains the key value and it’s solved btw.

Really appreciate for your help man, I really mean it.

1 Like