I'd like to create a passage with some links, created from an array (of strings). To create this links I have insert this code on the passage:
<<for _i to 0; _i lt $gallery.length; _i++>>
<<set _link to $gallery[_i]>>
[[_link|GalleryImage][$galleryImage to _link]]<br />
<</for>>
$gallery is an array of strings like "home", "church", "university".
All links are linked to the same passage "
GalleryImage". On this passage I want to show the images, based on the gallery array. So I want to set
$galleryImage for every link. But here is the problem: Every link sets
$galleryImage to the last element from the
$gallery array.
How can I solve
this problem?
Kind regards
from Purzelkater
Comments
For SugarCube v2.14.0 or later
The <<capture>> macro was added in SugarCube v2.14.0, so you may use that to localize _link. For example:
For SugarCube v2.13.0 or earlier
SugarCube versions prior to v2.14.0 do not have the <<capture>> macro, so you have to use the Stupid Print Trick™. For example: NOTE: This only works with primitive values—e.g. strings, numbers, booleans.
Sorry for the version issue. Hadn't noticed the new macro. I'm using Twine 2.0.11 with SugarCube 2.11.0.
For the time being, if you really wanted to use <<capture>>, you should use the <<link>> macro instead of a setter link—they're more versatile than setter links anyway. For example:
This is working like I want without red passages.
So thanks again for this amazing story format.
The Stupid Print Trick™ has some significant limitations and caveats—that's why I call it the "stupid print trick", because it's stupid and tricky. My recommendation is to only use it when you have no other choice available. In this case, you do not need to use it if you're going to use the <<link>> macro, as that allows you to use <<capture>>.
I really suggest using something like my last example:
Or something like this, which simply captures _i, instead of creating a temporary variable to hold the link: NOTE: As shown above, you must use a backtick expression to resolve the $gallery[_i] expression for use as the first argument to <<link>>—SEE: The Macro Library documentation's Passing an expression as an argument subsection (at the top) for the explanation of why.