The code attaches a metatable of info to items in a player’s inventory so I can access a items info easily without having to datastore/store data that will always be the same
an item’s metatable is set when a player’s data is loaded, and whenever a item is added to a player’s inventory
function SharedInventoryModule:SetItemMeta(item)
local meta = ItemDatabase:GetItem(item.Id)
if meta then
setmetatable(item, meta)
end
end
function InventoryModule:AddItem(inventory, id)
local item = ItemDatabase:GetItem(id)
if item then
local newItem = {Id = id}
SharedInventoryModule:SetItemMeta(newItem)
table.insert(inventory, newItem)
end
en
the metatable is something like{name = “Name”, stats = {}} etc
while the item would be {ID = 1, Amount = 5}
so this is possible item.name etc
is this a good idea?
Another solution could be:
a GetItemInfo() function, but then I’d have to call this function quite often to access info
As far as I know setmetatable function only binds functions like __index. It shouldn’t work with custom names, so I doubt your example works. (Or I am wrong?) Might be useful solution depending on how you use it though. You would just need to modify it a little bit.
I don’t understand the use case of metatables here. Why not just use a regular table to store data? It seems quite redundant to store data inside of an __index metamethod.