Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Dynamic links with SugarCube 2

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

  • You should always state the story format you're using and its version, because advice will tend to vary based on that information.


    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 _i to 0; _i lt $gallery.length; _i++>>
    	<<set _link to $gallery[_i]>>
    	<<capture _link>>
    		[[_link|GalleryImage][$galleryImage to _link]]<br>
    	<</capture>>
    <</for>>
    


    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:
    <<for _i to 0; _i lt $gallery.length; _i++>>
    	<<print '[[' + $gallery[_i] + '|GalleryImage][$galleryImage to "' + $gallery[_i] + '"]]<br>'>>
    <</for>>
    
    NOTE: This only works with primitive values—e.g. strings, numbers, booleans.
  • Wow, thank you! Your print trick do the job.

    Sorry for the version issue. Hadn't noticed the new macro. I'm using Twine 2.0.11 with SugarCube 2.11.0.
  • I have updated to SugarCube 2.14.0 and tried the capture version. But sadly this don't work for me, have the same issue like my first post. However, so I'm using the print trick.
  • I have updated to SugarCube 2.14.0 and tried the capture version. But sadly this don't work for me, have the same issue like my first post. […]
    Ah! That would be because I didn't account for setter links, so they're not supported by <<capture>> at the moment. Oops. I'll have to try to address that in a future release. My apologies for the confusion.

    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:
    <<for _i to 0; _i lt $gallery.length; _i++>>
    	<<set _link to $gallery[_i]>>
    	<<capture _link>>
    		<<link [[_link|GalleryImage]]>><<set $galleryImage to _link>><</link>><br>
    	<</capture>>
    <</for>>
    
  • Well, the problem with the first print trick is, it creates a new passage with the wrong name. If I delete this passage, Twine will show a read passage for missing link target passage. So I have combines the print with the link macro:
    <<for _i to 0; _i lt $gallery.length; _i++>>
    		<<print '» <<link "' + $gallery[_i] + '" "GalleryImage">><<set $galleryImage to "' + $gallery[_i] + '">><</link>><br>'>>
    <</for>>
    

    This is working like I want without red passages. :)
    So thanks again for this amazing story format.
  • Twine 2's automatic passage creation and link detection code can easy be confused, yes. Using the square bracket notation for the indexing of $gallery inside the double square bracket link is likely what confused it. I should have figured that would happen.


    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:
    <<for _i to 0; _i lt $gallery.length; _i++>>
    	<<set _link to $gallery[_i]>>
    	<<capture _link>>
    		» <<link _link "GalleryImage">><<set $galleryImage to _link>><</link>><br>
    	<</capture>>
    <</for>>
    


    Or something like this, which simply captures _i, instead of creating a temporary variable to hold the link:
    <<for _i to 0; _i lt $gallery.length; _i++>>
    	<<capture _i>>
    		» <<link `$gallery[_i]` "GalleryImage">><<set $galleryImage to $gallery[_i]>><</link>><br>
    	<</capture>>
    <</for>>
    
    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.
  • Ah! That would be because I didn't account for setter links, so they're not supported by <<capture>> at the moment. Oops. I'll have to try to address that in a future release. My apologies for the confusion.
    Update: This is fixed in the repo and will be in the next release.
  • Thanks a lot!
Sign In or Register to comment.