As of recent, I’ve been experimenting with OOP on ROBLOX, and I’ve been trying to find an OO method of writing an item database. However, I’ve come to no avail. I’ll give an example, since many of you may not know what I’m talking about.
Typical Item Database (Example):
-- Module
local ItemDatabase = {
-- Stored as keys and tables in order to be displayed by a client.
Bat = {
Name = "Bat";
Price = 0;
Image = "rbxassetid://0";
}
}
-- Returning just like a typical ModuleScript
return ItemDatabase;
My attempt at an OOP Version (Another Example):
-- Item Class
local Item = {}
Item.__index = Item
function Item.new(Name)
local self = {}
setmetatable(self, Item)
self.Name = Name
-- add to item
DatabaseModule:Set(Name, self)
return self;
end
return Item;
-- subclass
local Bat = {}
Bat.__index = Bat
setmetatable(Bat, Item)
function Bat.new()
local self = Item.new("Bat")
setmetatable(self, Bat)
return self
end
-- Database Module
local Module = {}
local Database = {}
function Module:Set(Key, Value)
Database[Key] = Value
end
function Module:Get(Key)
return Database[Key] or Database
end
return Module;
If anyone has any better solutions or ideas to add to this please tell me!