Help OOP Confusions

I’ve seen a lot of OOP videos, but I’m not quite sure why I would use it. Using format code, Object Oriented Programming keeps the scripts orderly.

After viewing the videos, it appears to me that all you’re doing is adding more scripts and creating a more complicated system. If the scripts are module scripts, then a server script call for each module script.

Here’s an illustration. (places a part in to workspace)

With traditional scripting it’s just clones the part to the workspace.

I fail to see how using this is any better or more user-friendly. I believe there must be something I’m missing because almost every video said that OOP is far superior than traditional scripting.

Don’t get bogged down trying write OOP code. My recommendation is just write the code and and if you see yourself repeating code then make a ModuleScript. Most of the time it is needless to make classes where there doesn’t need to be. Many people don’t even like OOP, especially inheritance. Inheritance is a bad word to many programmers.

2 Likes

Agreed, I don’t recommend using OOP unless you actually see a benefit from it. At the moment I don’t see any benefits but people say a good use of OOP would be for skill trees, and just assigning/acquiring properties of things that will be repetitively made.

1 Like

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects,[1] which can contain data and code: data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods). In OOP, computer programs are designed by making them out of objects that interact with one another.[2][3]
Wikipedia - Object-oriented programming

OOP, just like any other paradigm, is meant to make programming easier (source: me). You don’t need OOP to do something that is already ridiculously easy.

Where OOP shines, though, is when the project scales in size. Take the engine itself, for example.

P.S. I wouldn’t really trust youtube scripting tutorials to show the appeal of OOP.

I wouldn’t call the absolute mess of classes that is the Roblox Engine “OOP shining”. It’s more an example of why inheritance is so bad and should be favored for composition (almost) every time. My opinion.

The benefits from OOP are often used to create a repetition. Mobs, Pets, Vehicles, Maybe even a tycoon could use OOP in order to improve the amount of scripts that would be necessary in order to create it the traditional way.

Classes would work too for OOP, but it depends on if they’re a custom type of feature. Example being if the player wants to create a custom class and name it accordingly to themselves, give it it’s own properties etc. You could do it manually but why do that when it’s far more efficient to script it the way most people do? You wouldn’t be benefitting from using OOP for something that isn’t too complex.

What are the problems you’ve run into while using “the absolute mess of classes that is the Roblox Engine”?

By the way, keep in mind the drawbacks of composition, and why OOP might’ve been the best suit for a project like Roblox (especially when it is programmed in C++/C, or so I think.).

Does you cat have an Animal inside it or is it an Animal? And, it’s not like this is ever all that exposed, many people who use Roblox don’t know that their precious PointLight inherits from Light. But, inheritance is another issue, not necessarily required for OOP. In the case of just OOP, you can’t deny that the real world itself is just a bunch of objects, so the easiest way to understand a game world is to split it up into distinct objects that do specific things, but, now that I think about, composition still kind of exists with instance hierarchies, like an ECS almost.


It’s important to mention that you don’t need to use OOP for everything, only what feels like it is a separate “thing” from the rest of your code, and has “state”. For example, a Countdown object is something you could have that represents a timer that counts down to 0, then fires an event. This object has “state”, a script has to keep track of what number it’s on, so why not separate it out into an object with OOP so scripts don’t have to deal with any extra variables it has? This is a basic example, but I hope you get the idea.

It works for me usually because Roblox does a good job maintaining it.

What I am referring to, is if you look up inheritance vs composition, you’ll find most people talking about how inheritance gets hard to maintain the bigger it gets. I think the main reason is you can’t change classes easily when using inheritance. You end up with a big mess of classes and pseudo classes. You end of having to overwrite methods and you can never really go backwards. Changeability is one of the most important things in writing good code. How easily can it be changed.

Anyway I’m not an expert. That is the little bit that I know from listening to people.


Yeah objects are good. Just inheritance is bad.

1 Like

I also couldn’t understand this for a long time, because it’s easier not to use this handful of scripts. And indeed OOP is not at all necessary, it only simplifies some aspects.

But at one point, looking at OOP in practice, I realized why this was needed.

OOP is needed in order to create objects with their built-in functions. Purely for convenience.


Let’s look at the code:

local Button = {}
Button.__index = Button

function Button.new()
	local Main = {}
	
	-- create a private value
	Main._button = Instance.new("ImageButton")
	
	-- return table as response
	return setmetatable({}, Button)
end

-----------------------------------------------------------------------------------------

-- Our first function, which is responsible for pressing!
function Button:OnClick(func)
	return self._button.MouseButton1Click:Connect(func)
end

-- Button:OnClick()
-- Please note that we put a colon, not a period, so that the function knows which button (table) it works with.

-----------------------------------------------------------------------------------------

-- Our second function, which is responsible for renaming!
function Button:SetName(name)
	self._button.Name = name
end

return Button

This is our first script that contains OOP. This script is called a constructor.


local ButtonModule = require(script.Button)

-- We created a button!
local NewButton = ButtonModule.new()

-- Now let's rename our button!
NewButton:SetName("Shop")

-- Now let's create a function!
NewButton:OnClick(function()
	print("Button Pressed!")
end)

This is our second script, here we create our button and look how quickly it happens!


We can of course write functions like this:

local function CreateButton()
	return Instance.new("ImageButton")
end

local function RenameButton(Button, Name)
	Button.Name = Name
end

local function OnClick(Button, Func)
	return Button.MouseButton1Click:Connect(Func)
end

But will it really be easier?
Yes, it can, because everyone has a subjective idea of ​​simplicity.


In general, what can we say?
Using OOP, we can store functions for each button separately, that is, we will not need to constantly enter which button we have in mind, self and colon solve this issue.