It looks like you're new here. If you want to get involved, click one of these buttons!
<<set $itemStrawberry = { name: "Strawberries", description: "They look so yummy!", categories: ["fruit", "food"], value: 2, }>> <<set $frogLiver = { name: "Frogman Livers", description: "You don't want to remember how you got these.", categories: ["utility"], value: 500, }>> <<set $magicPole = { name: "The Utdraig", description: "A fishing rod from the days of legend, said to always catch a fish no matter where you throw it.", categories: ["fishing rod", "tool", "weapon"], value: 500, }>> <<set $donut = { name: "Doughnut", description: "You're not sure what it's filled with, but it's probably fine.", categories: ["pastry", "food", "dish"], value: 5, }>> <<set $itemlist = [$itemStrawberry, $frogLiver, $magicPole, $donut]>>
<<set $testshop = { name: "Random Tat", stock: [0, 2, 3], }>>Now to my problem. I want to be able to generate a list of links which each would perform the respective transaction, ie. I'd get a link for the strawberries, clicking on it would then add the strawberries to my inventory and remove my money. Thing is, the only way I can come up with to do so seems to be a for-loop building a click event for each item, but in doing so all the links would break since they'd all point to an empty object:
Welcome! $testshop.name currently carries: <<for _i = 0; $testshop.stock[_i] < $itemlist.length; _i++>> <<click $itemlist[$testshop.stock[_i]].name>> <<addToInv $itemlist[$testshop.stock[_i]].name>> <</click>> <<print $itemlist[$testshop.stock[_i]].description>> <</for>>Just to reiterate, I'm aware that this code doesn't work because _i is only queried after building the page, by which point it is equal to $itemlist.length.
Comments
If you're using SugarCube ≥v2.14.0 (≥v2.15.0 for full support), then you may use the <<capture>> macro to capture the value of _i. This is the recommended method of accomplishing your goal. For example:
Elsewise, if you're using SugarCube <v2.14.0, then you'll have to use the Stupid Print Trick™ to cause the inner _i to be evaluated early—i.e. when the link is created, rather than when it's activated. For example:
PS: <<click>> is deprecated, you should be using its replacement <<link>>.
Worked like a charm. Thank you.