What are the advantages of OOP and 'Topic' system?

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.

Personally, I use OOP all the time. The game data model, services, the whole lua VM, everything is built in the OOP-style. After all, the underlaying engine language is very object oriented. I find it very convinient to wrap functionality in “classes” and require, modify, use, later possibly dispose. Like a packet. A tycoon class, a vehicle class, a camera class, a sword class, you name it.

It’s absolutely no necessity, however. Anything can be done without OOP as well. It’s intention is organization, and I find it very useful.

Unfortunately I don’t know Java enough to compare, but Roblox uses luau - a derivation of lua. Considering object-oriented programming languages, lua is prototype-based (sometimes called classless programming lol). That means behavor is inherited from an existing object that servers as a prototype.

For example, all Roblox instances originate from Instance class. They inherited all its properties, while adding their own properties, states and behavior.

I highly JonByte’s article as an excellent concise reading material.

Topics? I’m not sure I’ve heard this particular term before in Roblox or luau context. I believe B Ricey linked it as an example of a similar model to RbxScriptSignals. And since the site mentions “unidirectional” communication, I’m quite certain it’s the matter of events. Think broadcast in Scratch.

In Roblox, a script signal is created, access with a couple of methods and properties returned in form of an instance, it can be fired (with arguments), and any script that connects a listener to that event can receive a signal with those args.

On the contrary, functions (not events), are similar, but return a response. The “thread” yields until the returned values are received in the script that fired.

RbxScriptSignal: RBXScriptSignal | Documentation - Roblox Creator Hub

Bindable signals (communication on the same device): Bindable Events and Functions | Documentation - Roblox Creator Hub

Remote signals (networking; communication across client-server boundary): Remote Events and Functions | Documentation - Roblox Creator Hub

Don’t worry, all this is rather simple.

1 Like

This response and linked resources is/are incredibly helpful, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.