Howdy, Stranger!

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

Auto-generating Links

Hi,

Absolute beginner with any of this and unable to find a solutions to the following problem.
I am working with Sugarcube 2 and have run into a bit of a hurdle I can't find a solution to. I have set up my links in a way to carry variables:
<<link [[Smell]]>>
    <<set $target = "flower">>
<</link>>

In the 'Smell'-Passage a switch statement is then checking on how to proceed with this particular input.

Since I have a lot of these kind of links, I was hoping I could automatically generate these links:
<<set $redflower to {
"name": "Red flower",
"actions": ["look", "smell", "take"],
}>>

<<set $blueflower to {
"name": "Blue flower",
"actions": ["look", "smell", "take"],
}>>

<<set $allflowers = [$redflower, $blueflower]>>

<<for _i to 0; _i lt $allflowers.length; _i++>>
    <<print $allflowers[_i]>>: 
    <<for _j to 0; _j lt $allflowers[_i].actions.length; _j++>>
        <<switch $allflowers[_i].actions[_j]>>

        <<case "look">>
        <<link [[Look]]>>
            <<set $target = $allflower[_i].name>>
        <</link>>

etc...

        <</switch>>
    <</for>>
<</for>>


This creates the basic link-structure just fine, but obviously <<link Look>><<set $target = $allflower[_i].name>><</link>> does not work ouside of the for-clause, so I get an error as soon as any of the links is clicked. Is there any way I can replace it to make this work, or do I have to fill in every link manually?

Comments

  • You need to state the name and full version number of the Story Format you are using, as answers can be different for each one. I will assume you are using the current latest version of SugarCube 2 which is v2.18.0

    You will need to use the <<capture>> macro to associate the value of _i with the links setter.
    	<<switch $allflowers[_i].actions[_j]>>
    
    	<<case "look">>
    		<<capture _i>>
    			[[Look][$target to $allflowers[_i]["name"]]]
    		<</capture>>
    	<</switch>>
    

    I switch the link to a Setter Link and corrected the mis-spelt $allflowers variable name.
  • Thanks a lot - that was the solution I was looking for.

    Sorry for skipping over the full version number and for accidentally posting this as discussion instead of a question - will keep these things in mind if I ever run into another problem.
Sign In or Register to comment.