Evaluating the destination after a link click

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.5.1
Story Format: Sugarcube 2.36.1

I want a link that can go to different passages depending on state, but not evaluate that state until right when the link is clicked. I don’t think I’m formatting my destination passages correctly, but maybe there are some other errors on top of that .

<<set $nextaction = "Explore"
<<link "Flowers" _dest>>
<<if $nextaction is "Explore">><<set _dest=xFlowers>>
<<else>><<set _dest =cFlowers>><</if>>
<</link>>

Then I also have a passage named “xFlowers” and a passage named “cFlowers”

A more general question is this: If I want to create nearly every link so that it parses different outcomes depending on the current value of $nextaction, is this really the best way to do it?

I am imagining a system where each link is an object which knows its own Explore and Cast functions. Or is that something that requires fiddling with the core javascript?

To have a link evaluate the destination on click, you want the <<goto>> macro inside a <<link>>, like this:

<<link "Flowers">>
    <<if $nextaction is "Explore">>
        <<goto "xFlowers">>
    <<else>>
        <<goto "cFlowers">>
    <</if>>
<</link>>

As for a more efficient way to do this for multiple links, it really depends on how exactly each link works. Extrapolating from this example and saying you want each link to send you to xPassage or cPassage depending on the value of $nextaction, you could do it with a widget. Create a new passage, tag it widget (and possibly nobr to avoid extraneous line breaks), and paste this in it:

<<widget macroName container>>
    <<link _args[0]>>
        <<if $nextaction is "Explore">>
            <<goto `"x"+_args[1]`>>
        <<else>>
            <<goto `"c"+_args[1]`>>
        <</if>>
        _contents
    <</link>>
<</widget>>

Now you can create a link like the one above by typing <<macroName "LinkName" "Passage">><</macroName>>.

Or, if you might want the destination passages to have different names, you can change the <<goto>> macros to <<goto _args[1]>> and <<goto _args[2]>> respectively, then create your link by typing <<macroName "LinkName" "explorePassage" "castPassage">><</macroName>>.

2 Likes

Did you test the widget thing? Because when I tried it, the goto happened immediately, not when the link was clicked. I think that’s a SugarCube bug, but…

I did, yes. Did you remember to close the <<link>> macro? That seems like the most likely explanation.

Argh, missing a >. Sorry for the confusion.

Josh, I tried the widget in my code, and it’s working. I’m passing objects to the widget with property names like “display” and “cast”.

Thanks to all who responded. But why do some sources warn of hazards associated with goto macro?

1 Like

That’s because using <<goto>> to automatically forward the player somewhere else immediately upon reaching a passage breaks history navigation. If you’re using it inside a link there’s no problem.

3 Likes