I’m working on a inventory/item system and am wondering what design would be best for performance and usability.
My current thinking is that variables that change object to object should be stored in the object and variables that don’t change should be kept in one location and have functions to request that data when its needed.
In the following two examples CustomName is a variable unique to each Item and changeable by the user at any point.
In this example the object has all the variables.
local Item = {}
function Item.New(Id, Name, Description, Icon, Weight,CustomName)
return setmetatable({Id = Id,CustomName = CustomName, Name = Name, Description = Description, Icon = Icon, Weight = Weight}, Item)
end
Item.__index = Item
function Item:GetId()
return self.Id
end
function Item:GetName()
return self.Name
end
function Item:GetDescription()
return self.Description
end
function Item:SetCustomName(NewValue)
self.CustomName = NewValue
return self.CustomName
end
function Item:GetCustomName()
return self.CustomName
end
return Item
In this example CentralizedItems stores all the variables for each item.Id in a table where the index is the item’s Id.
local Item = {}
local CentralizedItems = require(game.Workspace.CentralizedItems)
function Item.New(Id,CustomName)
return setmetatable({Id = Id, CustomName = CustomName}, Item)
end
Item.__index = Item
function Item:GetId()
return self.Id
end
function Item:GetName()
return CentralizedItems.GetItemNameForId(self.Id)
end
function Item:GetDescription()
return CentralizedItems.GetItemDescriptionForId(self.Id)
end
function Item:SetCustomName(NewValue)
self.CustomName = NewValue
return self.CustomName
end
function Item:GetCustomName()
return self.CustomName
end
return Item
If anyone has any input on which design is best in regards to programming functionality/ease of use and game performance impact I’d appreciate it, especially if you would choose one over the other depending on if you were dealing with 10’s of Item VS 1000’s.