Twine Version: 2.92
Sugarcube: 2.36
I’ve got help doing this with Harlowe before but I’m having trouble implementing the same thing in Sugarcube.
I’ve got the objects set up thusly in StoryInit
<<set setup.gameItems = {
"Claw Hammer" : {
"Name": "Claw Hammer",
"itemType" : "Tool",
"Price" : 2,
"Weight": 5
},
"Hand Saw": {
"Name" : "Hand Saw",
"itemType" : "Tool",
"Price" : 5,
"Weight": 3
},
"Nail": {
"Name" : "Nail",
"itemType" : "Tool",
"Price" : 1,
"Weight": 1
},
"Bucket": {
"Name" : "Bucket",
"itemType" : "Tool",
"Price" : 3,
"Weight": 4
}
}>>
<<set $inventory to []>>
<<set $inventory.push("Hand Saw", "Hammer", "Bucket", "Nail")>>
Then I’ve got this in an Inventory passage in the storyMenu which prints out the inventory items and their prices & weights
<<for $i = 0; $i < $inventory.length; $i++>>
<<set $item = setup.gameItems[$inventory[$i]]>>
<<print $item.Name>>, Price: <<print(new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP' }).format($item.Price))>>,Weight: <<print $item.Weight>><br>
<</for>>
What I’d like is for the player to be able to sort their inventory from lightest at the top to heaviest at the bottom by the weight object key. I’ve been ‘hammering’ away at the similar examples on sorting arrays on the internet but seem to can’t seem to ‘nail’ it.
Many many thanks