User:TMC/Scripts:Inventory sort
< User:TMC
Jump to navigation
Jump to search
This is how you might alphabetically sort all the items in the inventory using HamsterSpeak 4. This is just an exercise for me to try out the proposed features and syntax, based on Giz's script at http://www.slimesalad.com/forum/viewtopic.php?p=125061
Plotscript, AlphabeticalInventory, begin # Create a list (array) variable(<List> itemlist) itemlist := [] # Iterate over every slot in the inventory (InventorySlots returns the list of party inventory slots). # slot is a new variable of type InvSlot. foreach (<InvSlot> slot, InventorySlots) do ( # Skip if empty if (not(slot.used)) then (continue) # var as shorthand for variable var(<Item> WhatItem, HowMany, <String> name) WhatItem := slot.item HowMany := slot.count Name := slot.item.name # Optionally make the sort case-insensitive by converting to lowercase: # Name := Name.lowercase() # Append a name,item,count triple itemlist.append( [Name, WhatItem, HowMany] ) # And delete the original copy of the item slot.count := 0 ) # By this point the script, we've gone through every item in the inventory and the player now has nothing. # This sorts by item name, since lists are stored by the first thing in them (and then by the 2nd thing # in case of ties, etc) itemlist.sort() # Iterate over the sorted list and add variable(SlotNum) SlotNum := 0 foreach (<List> triple, itemlist) do ( WhatItem := triple[1] HowMany := triple[2] # You can still use existing inventory commands too; even though WhatItem is an Item # not an ID, you can use it where an item ID is required. SetItemInSlot (SlotNum, WhatItem) SetItemCountInSlot (SlotNum, HowMany) SlotNum += 1 ) # No need to delete the list, it happens automatically. end