Any other way to organise large data trees?

Currently I’m just using a table inside a modulescript, which basically contains all the info for each of my classes, like below (kept only 1 class to keep it short)

classItems.Weapons = {
	Knight = {
		['Sword'] = {
			Cost = 0,
			Currency = '',
			Rarity = 'Common'
		},
		['Claymore'] = {
			Cost = 100,
			Currency = 'Gold',
			Rarity = 'Common'
		},
		['Highlander'] = {
			Cost = 200,
			Currency = 'Gold',
			Rarity = 'Common'
		},
		['Light'] = {
			Cost = 250,
			Currency = 'Gold',
			Rarity = 'Rare'
		},
		['Long Sword'] = {
			Cost = 500,
			Currency = 'Gold',
			Rarity = 'Rare'
		},
		['Trusty'] = {
			Cost = 50,
			Currency = 'Gems',
			Rarity = 'Epic'
		},
		['Riptide'] = {
			Cost = 100,
			Currency = 'Gems',
			Rarity = 'Epic'
		},
		['Shai'] = {
			Cost = 200,
			Currency = 'Gems',
			Rarity = 'Legendary'
		},
		['Judgement'] = {
			Cost = 250,
			Currency = 'Gems',
			Rarity = 'Legendary'
		},
	},
}

classItems.Armours = {
	Knight = {
		['Griswold'] = {
			Cost = 0,
			Currency = '',
			Rarity = 'Common'
		},
		['Redcliff'] = {
			Cost = 100,
			Currency = 'Gold',
			Rarity = 'Common'
		},
		['Jahrfyre'] = {
			Cost = 150,
			Currency = 'Gold',
			Rarity = 'Rare'
		},
		['Enchanted'] = {
			Cost = 75,
			Currency = 'Gems',
			Rarity = 'Rare'
		},
		['Empyrean'] = {
			Cost = 150,
			Currency = 'Gems',
			Rarity = 'Epic'
		},
	},
}

So my question is, is there any other way I can store this data? Using 3rd pata sources like google spread sheets or something? As atm I have only 4 classes, but just those 4 classes take up about 400 lines of code and I’m worried in the future when I have more it may become difficult to easily navigate around and find what I need to find. Does anyone have suggestions on how can I tidy this up? if I could seperate it into 2 modules? Or a module for each classes?

1 Like

you could create a module for each type of weapon/armour/etc., then just reference them within each class. that way you can reuse the items for several classes, if more than one class has access to the same item(s).

e.g.
Knight Module: return {"Long Sword", "Light", "Highlander"}
Weapons: return {["Long Sword"] = { ... }}

You can always just store all the individual weapon/armor data in a single line:

Knight = {
	['Sword']      = { Cost = 0,   Currency = '',     Rarity = 'Common' },
	['Claymore']   = { Cost = 100, Currency = 'Gold', Rarity = 'Common' },
	['Highlander'] = { Cost = 200, Currency = 'Gold', Rarity = 'Common' },
	['Light']      = { Cost = 250, Currency = 'Gold', Rarity = 'Rare'   },
	['Long Sword'] = { Cost = 500, Currency = 'Gold', Rarity = 'Rare'   },
	--...

That would at least look neat in the script.
You can also use an array and map those to the Cost/Currency/Rarity indexes if you want to make the data fit on 1 line better:

Knight = { --          Cost | Currency | Rarity
	['Sword']      = { 0,    '',        'Common' },
	['Claymore']   = { 100,  'Gold',    'Common' },
	['Highlander'] = { 200,  'Gold',    'Common' },
	['Light']      = { 250,  'Gold',    'Rare'   },
	['Long Sword'] = { 500,  'Gold',    'Rare'   },
	--...
-- in your script,

local AttrMap = { -- attribute map obvs
	Cost = 1,
	Currency = 2,
	Rarity = 3
}

local SwordCost = classItems.Weapons.Knight.Sword[AttrMap.Cost]
-- something like that

You could probably even go as far as creating a new dictionary for each array, putting this at the bottom of your module script:

local _AttrMap = { -- reverse attribute map obvs
	[1] = 'Cost',
	[2] = 'Currency',
	[3] = 'Rarity'
}

-- classItems data here

-- assuming there are more item types that don't use this conversion
for _, itemName in ipairs{'Weapons', 'Armours'} do 
	for _, classData in pairs(classItems[itemName]) do
		for name, data in pairs(classData) do
			local newDictionary = {}
			for i, v in ipairs(data) do
				newDictionary[_AttrMap[i]] = v
			end
			classData[name] = newDictionary
		end
	end
end
-- in your script,
local SwordCost = classItems.Weapons.Knight.Sword.Cost
1 Like