Hi developers,
I don’t know much about OOP, but I really want to know more about it. But my question is
Why is OOP better in some cases?
Couldn’t you just use a regular script to make what you want? If yes, then why do people use OOP?
Hi developers,
I don’t know much about OOP, but I really want to know more about it. But my question is
Why is OOP better in some cases?
Couldn’t you just use a regular script to make what you want? If yes, then why do people use OOP?
Object Oriented Programming (OOP) is usually used for abstraction. It’s commonly found in modules and libraries where you want to simplify the process of accomplishing certain things.
It’s a concept used in almost every large-scale project, since it can result in significantly easier maintenance and usage.
People use it because it makes everything cleaner and easier.
Yes it does, but it is kind of hard, isnt it? And regular scripting is easier than oop (at least in my opinion) so should I start switching my scripts to OOP?
Here’s a simple comparison:
-- Procedural
local car1Speed = 0
local car2Speed = 0
local function accelerateCar1(amount)
car1Speed += amount
end
local function accelerateCar2(amount)
car2Speed += amount
end
accelerateCar1(50)
accelerateCar2(30)
accelerateCar1(20)
-- OOP
local Car = {}
Car.__index = Car
Car.new = function()
return setmetatable({
-- Here goes all car data.
Speed = 0
}, Car)
end
Car.Accelerate = function(car, amount)
car.Speed += amount
end
local car1 = Car.new()
local car2 = Car.new()
car1:Accelerate(50)
car2:Accelerate(30)
car1:Accelerate(20)
I personally prefer to omit .new and instead have a standalone constructor, although .new is most common practice.
I can use this for modules, right?
Absolutely! That’s the most common use for it by far!
I knew it! I see so much big games using modules with OOP and i really wanted to try it myself, Because i don’t know much about that, I unfortunately used AI.. But i hope i can learn it sometime!
You can always look at other people’s implementations to try to learn from them. I have a few open-source libraries that rely on OOP. ![]()
You do? That’s cool, can you share it for me if you don’t mind?
You shouldn’t need to switch your scripts to OOP if you don’t need it. OOP is essentially a programming paradigm where you define a class with properties (fields) and functions (methods). Imagine this as a blueprint for a house. Using this blueprint, you can create objects (houses).
For example, imagine a zombie game, you could create two classes for a fast zombie and an exploding zombie which both inherit from a base zombie class. In this example, OOP would be really handy.
Regular scripts would make it hard to have the same level of organisation that OOP grants you.
Luau isn’t a language designed for OOP and so we create faux OOP using metatables and since metatables aren’t the easiest thing to work with, it can get a bit annoying.
So to answer your question, OOP isn’t inherently better and it depends on what your making. In my previous example, OOP would be a suitable choice. However, with a clicker game, what would the classes be? OOP isn’t necessary and overcomplicates things for no reason when you can simply use normal scripts.
Thanks for the long explanation, I can really understand it now!
No problem, if it gave you the answer you were looking for, would you be able to mark it as a solution? Thanks!
They’re open-source, meaning they’re available to anyone for free!
You can find most of them here.
I’ve brought you a little more OOP sir:
--!strict
type car = {
speed : number;
accelerate : (self : car) -> ();
}
local function create_car(acceleration : number) : car
local car : car = {
speed = 0;
accelerate = function(self : car)
self.speed += acceleration
end,
}
return car
end
local car_manager = {
cars = {} :: { car };
}
function car_manager.accelerate_all(self : typeof(car_manager))
for _, car in self.cars do
car:accelerate()
end
end
function car_manager.add(self : typeof(car_manager), car : car)
table.insert(self.cars, car)
end
car_manager:add(create_car(1))
car_manager:add(create_car(2))
car_manager:accelerate_all()
print(car_manager)
Nice. You’ve primarily just made some personal preference changes, albeit both are equally OOP.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.