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 will need to use the <<capture>> macro to associate the value of _i with the links setter.
I switch the link to a Setter Link and corrected the mis-spelt $allflowers variable name.
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.