Using a variable from a listbox as a destination for a link

Twine Version: 2.4.1

I am trying to code something that lets the player select which exit they want to take based on them selecting a room name from a listbox rather than having a link for each room. I have the code below but it doesn’t work. Is there an error in my code or a better way to do this please?

<<set _exits=["room1","room2"]>>
<<link "Run for the doorway through to" _exit>><</link>> <<listbox "_exit" autoselect>><<optionsfrom _exits>><</listbox>><br>

If you swap the location of the link with the listbox, you should not get an error message. (The passage loads top to bottom, left to right, and doesn’t update the earlier parts if you use an interactive option).

To make it less weird text wise, you could do:

<<set _exits=["room1","room2"]>>
Run for the doorway through to <<listbox "_exit" autoselect>><<optionsfrom _exits>><</listbox>>
<<link "Run!" _exit>><</link>> 

EDIT: My link is wrong. Check the answer below.

That code will always navigate to the first option. This is a rare case of the <<goto>> macro being the best solution:

<<set _exits = ["room1", "room2"]>>
Run for the doorway through to \
<<listbox "_exit" autoselect>>
  <<optionsfrom _exits>>
<<link "Run!">>
  <<goto _exit>>
1 Like

And one more bit of explanation:

With <<link "Run!" _exit>> the problem is that it gets called to make the link while SugarCube is building the page. So it’ll see the initial value of `_exit’ (nothing/unset). The user hasn’t chosen an exit yet when the page is being built.

But the code in the body of the link (everything between the <<link>> and <</link>> tags) doesn’t get run until the user clicks the link. So in this case, the user can select an exit from the list box, then click “Run!”, and then the <<goto>> will run, so it will see the user’s choice of _exit.

1 Like