Skip to content

"Lock and Key: Variable": SugarCube (v2.18)#


This example is affected by history changes in the story. Undoing or re-doing back to a passage containing this recipe has the potential to change its saved values.


"Lock and Key: Variable" demonstrates how to create the effect of picking up a key and unlocking a door. In this example, the key is a variable ($key) and is initially set to the value false in the Start passage.

When the link (created using a <<linkreplace>> macro) "Pick up the key" is clicked, $key is changed to the value true and the door link changes from its initial response of "Locked Door" to a link to the passage Exit.



Twee Code#

:: StoryTitle
Lock and Key: Variable in SugarCube

:: Start
<<set $key to false>>

[[Back Room]]
[[Front Room]]

:: Back Room
<<if $key is false>>
    <<linkreplace "Pick up the key">><<set $key to true>>You have a key.<</linkreplace>>
    There is nothing here.

[[Front Room]]

:: Front Room
<<if $key is true>>
    Locked Door

[[Back Room]]

:: Exit
You found the key and went through the door!

Twee Download

See Also#

Setting and Showing Variables