Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2.3.5
Story Format: Sugarcube
First question is whether <<set $products to $products.sort()>> can sort an object alphanumerically by their name: value.
The next part of the code works, it just lists out each item and its cost.
The problem is when I try to include a “Buy” link after each product. The for loop sets the item’s attributes to the last item on the list – meaning when Player clicks “Buy” for a $2.00 item, they get charged for a $.50 item because it was the last in the for loop.
This story was converted from Harlowe, and I think I solved this issue with link-repeat, but Sugarcube doesn’t have any link-repeat as far as I can tell.
<<set $products to $products.sort()>>
<<for _item range $products>>
<<if _item.name != "food" && _item.name != "wood">>
<<-ucfirst( _item.name)>>s: $<<-_item.cost>>
<<else>>
<<-ucfirst( _item.name)>>: $<<-_item.cost>>
<</if>>
<<link "Buy">>
<<if $dollars >= _item.cost>>
<<set _item.number += 1>>
<<set $dollars -= _item.cost>>
<<replace "#buy">><<include "Buy List">><</replace>>
<</if>>
<</link>>