How to check if there ARE any open storylets?

Twine Version: Harlowe 2.1

Hey all! I’m messing around with the storylets macros in Harlowe, and had a question. Can I set up an (if:) macro to check whether any open storylets exist before serving one to the player? I imagine it would look something like the following (potential code set between asterisks):

(set: _randomStoryletDatamap to (either: …(open-storylets:))
*here's where I don't know what to do, but I suspect it would look something like: (if: the datamap has any data in it at all, run the following - if it doesn't, because there are no open storylets, hit the else statement below)[*
(set: _passageName to _randomStoryletDatamap’s name)
You look (either:“to the left”, “to the right”, “directly in front of you”) and see a door.
(link: “Go through the door”)[(go-to: _passageName)][[Pick a different door->RoomPick]]
*]*
(else:)[[[Your hand clasps the knob of the only door left.->FinalDoor]]]

Any pointers?

Looks like you’ll get an error if you pass nothing to (either:), so you probably want to check before choosing one, with (if: (open-storylets:)'s length > 0)?

1 Like

The (open-storylets:) macro returns an Array, that contains zero or more Passage representing Data-maps. The Array data-type includes a numerical length property that indicates how many values the Array contains.

The following shows one way to test the Array before trying to do something with its contents.

(set: _active to (open-storylets:))
(if: _active's length > 0)[
   (set: _storylet to (either: …_active))
   (set: _passageName to _storylet’s name)
]

That does it! Thank you so much.