Hello, I am making a shop system that involves dictionaries. For the shop system I need the different items to have an order. I know dictionaries don’t have an order, so I don’t know how I should sort each item. If anyone has a better way that wont have too many compromises then that is greatly appreciated.
Thanks!
You can initialize a lookup table when the game starts and use table.sort to put swords that do the least damage at lower indexes and those that do more damage at higher indexes, then if you want to show the items you can just loop over them and create gui instances and the worst swords should be first. You could also just use table.sort and check the amount they cost if you wish to do that instead.
Here is some “example code” which may or may not work (I didn’t test it…)
local lookup = {}
local items = ... --// your list of items
for zoneName, zone in pairs(items) do
--// initalize containers weapon types for this zone in their own table
local zoneContainer = {}
--// initialize the values for the containers for each weapon type for this zone.
for weaponType, weapons in pairs(zone) do
local weaponContainer = zoneContainer[weaponType]
if not weaponContainer then
--// create the container if it doesnt exist
zoneContainer[weaponType] = {}
end
for weaponName, weapon in pairs(weapons) do
--// new weapon object to hold the name aswell as its other properties so that it can be used in an array for sorting
local weaponObject = {
Name = weaponName
}
for statName, statValue in pairs(weapon) do
weaponObject[statName] = statValue
end
table.insert(zoneContainer[weaponType], weaponObject)
end
--// sort each weapon by its damage
table.sort(zoneContainer[weaponType], function(weaponA, weaponB)
return weaponA.Damage < weaponB.Damage
end)
end
lookup[zoneName] = zoneContainer
end
and so the weakest sword in zone1 would be accessed by doing lookup["Zone1"]["Swords"][1]