It looks like you're new here. If you want to get involved, click one of these buttons!
<<set $huntLoop to 1>>\
<<set $availPop to 10>>\
<<huntLinks>>
Then, in my "huntLinks" passage, I have:
<<if $huntLoop <= $availPop >>\
[[<<$huntLoop>>|hunt][$hunters = $huntLoop; $availPop -= $huntLoop]]
<<set $huntLoop += 1>>\
<<huntLinks>>
<<endif>>\
It prints a list of links that are numbers 1 to 10 as I intended. However, it seems Twine throws whatever the last value of $huntLoop was into the expressions for EVERY link. ALL of the links set $hunters to 11, and $availPop to -1...
Comments
SugarCube doesn't even show the links; it seems to perform the recursion twice, the first time without generating output. Therefore the second time the iteration variable is already past the end and the recursion ends immediately.
Correct. The set component of the link syntax happens on click, not at the moment of creation.
The links show fine for me. I assume you probably had a syntax problem.
Do you know an easy way to capture the iterator's value, to create a kind of closure in Twine? Or a simple way to create links in JavaScript, which does support closures?
Indeed: I copy-pasted Strangelander's code and the <<$huntLoop>> syntax works in SugarCane but doesn't work in SugarCube. However, I didn't get any feedback on this error: no red text, no alert box, nothing on the JavaScript console log.
The only easy way to capture the value of $huntLoop at the specific moment when the link is created, while still only assigning it to $hunters and $availPop upon click, is to evaluate it when you create the link.
So, while not pretty, this should work: (assuming I'm understanding what he's trying to do here)
Hmm. The links are created just fine, there's simply no link text for them. Odd. I would have expected to see something like a literal "<<$huntLoop>>". I'll have to look into it, but off the top of my head it's probably seeing the variable sigil and trying to get the value based on "<<$huntLoop>>", rather than simply displaying it since it's not a $variable.
<<print "[[<<" + $huntLoop + ">>|hunt][$hunters = " + $huntLoop + "; $availPop -= " + $huntLoop + "]]">>
Any way to escape those >> characters or something? Backslash doesn't work, apparently.
EDIT: the solution is to just get rid of the inner angle brackets altogether:
<<print "[[" + $huntLoop + "|hunt][$hunters = " + $huntLoop + "; $availPop -= " + $huntLoop + "]]">>
Thank you, that helped a lot, and knowing you can use quotes and pluses to concat stuff is great.
(I was looking for a similar mechanism for my "23" game.)