I am currently following B Ricey’s series on creating a Tycoon as it seems like a good resource to learn OOP for Roblox.
I’m wondering if any experienced developers here can tell me if they follow OOP principles and if they recommend doing this for Roblox?
As an example this is some code from the series:
local CollectionService = game:GetService("CollectionService")
local template = game:GetService("ServerStorage").Template
local componentsFolder = script.Parent.Components
local function NewModel(model, cframe)
local newModel = model:Clone()
newModel:SetPrimaryPartCFrame(cframe)
newModel.Parent = workspace
return newModel
end
local Tycoon = {}
Tycoon.__index = Tycoon
function Tycoon.new(player)
local self = setmetatable({}, Tycoon)
self.Owner = player
self._topicEvent = Instance.new("BindableEvent")
return self
end
function Tycoon:Init()
self.Model = NewModel(template, CFrame.new(0, 1, 0))
self:AddComponents(self.Model.Part)
end
function Tycoon:AddComponents(instance)
for _, tag in ipairs(CollectionService:GetTags(instance)) do
local component = componentsFolder:FindFirstChild(tag)
if component then
self:CreateComponent(instance, component)
end
end
end
function Tycoon:CreateComponent(instance, componentScript)
local compModule = require(componentScript)
local newComp = compModule.new(self, instance)
newComp:Init()
end
function Tycoon:PublishTopic(topicName, ...)
self._topicEvent:Fire(topicName, ...)
end
function Tycoon:SubscribeTopic(topicName, callback)
local connection = self._topicEvent.Event:Connect(function(name, ...)
if name == topicName then
callback(...)
end
end)
return connection
end
function Tycoon:Destroy()
self.Model:Destroy()
self._topicEvent:Destroy()
end
return Tycoon
I can understand when he explains what he is doing, although it is still a bit confusing (and he linked some robotics operating system documentation to help explain this ‘topics’ system, which I struggled to translate into Layman’s terms and for use with Roblox).
This topics system seems a bit overly complex with the extra boiler plate code to just run a remote event, but I’m guessing it becomes much more useful as a project gets larger?
Also I would appreciate any other advice or resources for learning OOP with Roblox. I completed a OOP course with Java from Helsinki University, but using OOP on Roblox (syntactically at least) seems quite different. Just want to make sure I’m doing the right thing by learning this early in my scripting journey, to be honest.