Using Variables and (if:) macros in sidebar to perform (go-to:)

Twine Version: 2.9.2
Harlowe: 3.3.9

Forgive me if this has been asked somewhere else, I have looked but can’t seem to find the answer I’m looking for.

I’d like to add a ‘search’ function into my story’s sidebar. Ideally I’d like the player to use a prompt macro to enter a number and then whatever that number is takes them to the appropriate passage in twine.
(I would like for this to be in the sidebar but if that’s not possible then I can just add it to each passage.)

I’ve got the below code into a header tagged passage. The ‘search’ function shows up in the sidebar and works with (prompt:) but I can’t seem to get it to then use the variable produced ($selection) to work with the (if:) and (else-if:) macros to perform the (go-to:) function.

Any ideas?

{
(append:?sidebar)[
(link:"Search",(button:))[
(set: $selection to (prompt: [Search], "Item Number", "Cancel" , "Confirm"))]
(if: $selection is '1')[(goto:'passage 1')]
(else-if: $selection is '2')[(goto:'passage 2')]
]
}

I also can’t get the (prompt:) macro to appear again once entered once but haven’t had time to try and tackle that yet so if anyone has any suggestions for that also I’d appreciate it. :slight_smile:

Thank you!

1 Like
{
(append:?sidebar)[|search>[(link:"Search",(button:))[
	(set: $selection to (prompt: [Enter a passage number to visit:], "", "Cancel" , "Confirm"))
	(if: (num: $selection) >= 1 and <= 3)[
		(go-to: 'Passage ' + $selection)
	](else-if: $selection is '')[
		(rerun: ?search)
	](else:)[
		(dialog:"No such passage exists.")
		(rerun: ?search)
	]
]]]
}

If you go step by step through this code, the logic should be evident. Everything serves a purpose. For example, I used the (num:) macro in case a user enters text, instead of a number.

Note: The range set for passage numbers is 1 to 3.

Update: Added the '' check in case a user cancels the prompt or doesn’t enter in anything.

Let us know if you have any other questions.

Thank you! That works!

I didn’t even think to use a range - it greatly simplifies it so much as well!