I have an item setup where you can interact with various items in similar ways. I've created a widget called ItemActions which generates these actions in the form of links (Examine, Grab, etc.). This widget then has a <<link>> which runs code on click, regenerating the "Inventory" passage in the sidebar and the item list in the main page. ItemActions takes an argument of "ItemCode" so it can then handle each item appropriately.
The issue is that when I iteratively create this list, the inner variables I set up in the inner <<link>> code get over-written by the next item.
For example, let's say a room has a key and a crystal. I click the key's "Grab" link, and it runs the code that adds it to your inventory and removes it from the room. But the ID it's using to do that is the ID of the crystal, because it came afterwards and reset the _itemCode variables in my widget and in the link.
The widget looks like this:
<<widget ItemActions>>
<<set _itemCode = $args[0]>>
<<set _item = $items[_itemCode]>>
<<link 'Grab'>>
<<run $player.inventory.push(_itemCode)>>
<<run $rooms[$player.locationCode].items.deleteWith(function (val) {
return val.code === _itemCode;
})>>
<<replace '#RoomItems'>>
<<include 'Room.RoomWrapper.Items'>>
<</replace>>
<<replace '#story-caption'>>
<<include 'StoryCaption'>>
<</replace>>
<</link>>
<</nobr>>
<</widget>>
This works fine to display the link, but when you click it, the temporary variables (_itemCode) have been overwritten by whichever one came last.
How can you control this?