The code you posted works fine, so I’m going to guess that that code isn’t exactly the code that’s failing for you. Thus, I’m going to have to guess what code you’re actually using.
If you’re generating the links within a loop and using the loop variable to determine what data gets pushed into the array when the user clicks the link, then you’re going to need to use the <<capture>>
macro on that loop variable. That macro makes it so that the code encompassed within that macro will use the values of the variables passed to that macro that they had at the time the macro was being executed.
For example, if you’re doing something like this (where $zones
is an array of zone names):
<<for _i = 0; _i < $zones.length; _i++>>
[["Go to " + $zones[_i]|$zones[_i]][$days += 1, $tarray.push($zones[_i])]]
<</for>>
That .push()
code in the links won’t work, because the value of _i
at the time the user clicks on any of those links will be set to $zones.length
, which will be out of bounds of the $zones
array, thus causing an error when it’s executed, thus nothing gets pushed into the $tarray
array.
To correct that, you’d need to use the <<capture>>
macro like this:
<<for _i = 0; _i < $zones.length; _i++>>\
<<capture _i>>\
[["Go to " + $zones[_i]|$zones[_i]][$days += 1, $tarray.push($zones[_i])]]
<</capture>>\
<</for>>
With that change, the value of _i
within the <<capture>>
macro will be fixed at the current value that _i
had within the loop at the time that each of those links were generated.
If you need a bit more help understanding how the <<capture>>
macro works, you might want to take a look at the “<<capture>>
Macro Help” section of my Twine/SugarCube sample code collection.
If that doesn’t solve your issue, then we’ll need to see the actual code that you’re using.
Hope that helps!