I couldn't understand what OOP does

I’ve been scripting In roblox for Over 3 months i’ve learning soo much stuff, that are hard to make, in such a short time, and i’ve been struggling for the past month to understand OOP!

Is OOP like a ModuleScript?

Please could anyone explain to me! Thanks!

1 Like

OOP stands for Object Oriented Programming, there’s a great resource explaining what the term means here: All about Object Oriented Programming

2 Likes

Object oriented-ness in Lua is very different from how it works in other languages. In Lua, a Table is the only way of combining multiple values into a group. There is also Userdata which works the same way but is not accessible to you (it is written into the Roblox engine).
Any table or userdata can have a metatable set. A metatable is like a label which says “This table/userdata has these functions associated with it”, what would be called a Class in other languages. Metatables should not contain anything specific to one table/userdata, since they are shared. Historically, this is actually very C++ like, but its not obvious to Lua users in general.

2 Likes

Metatables hold both normal and special values, the most important of which is __index. If you try to access a member of a table which is missing, the __index is used to try and locate it. If __index is another ordinary table, it is searched the same way. If __index is a function, that function is called with the original table and the requested key as arguments.
Note that the __index has to be in a metatable, it does not do anything special if placed in an ordinary table.

2 Likes

OOP extends past the scopes of Roblox and is one of the most common paradigms (way of programming) in software worldwide. Standing for Object Oriented Programming, OOP breaks down the main control script of a program into the highest level of abstraction possible through classes and objects, meaning there is very little code and more so functions (or methods) which handle the execution and function of certain modules. As you delve into these modules, you go through lower levels of abstraction, until you get to the lowest level of abstraction, which holds all the code.

This is a bit confusing, and doesn’t really have much application unless you can understand what it means, so I’ll start by teaching you some concepts of Object Oriented Programming. The main idea of this paradigm stems from the idea of Classes and Objects.

Imagine you have a car factory, and you want this factory to produce some red cars. The car factory would be known as the class, and this stores all the information needed to create a car, which is the object. When you create a car in a factory, you must consider how big it’ll be, what it can do, etc. So whenever we tell our car factory to produce a car, which is also known as a “constructor” function, our car will come out identical to all other cars produced from this factory. In addition to this, it will have all of the functions (also known as methods) which we can use on our car, such as :Drive() or :Park().

Now we know about our factories (classes) and cars (objects), we can talk about inheritance. Knowing how to make a type of car is all good, but what if our car factory wanted the ability to produce a new type of car which can fly? Instead of making an entirely new factory, which does the exact same thing with this new feature, we can create something which is known as a “sub-class”. This concept uses the constructor of it’s super class (the factory above it) to produce an object, like our car, and then the sub class applies it’s own changes, as shown in the image below.

Objects within our OOP paradigm have all their data held together within them, so we do not need to store it across multiple variables. As such, when we want to access data which is “encapsulated”, we would need to access one of it’s attributes. You do this all the time without realising it! For example, when you do Part.Name, you are inadvertently fetching the name from the object identified as Part. In addition to this, our objects have functions which can act upon themselves, which are known as “methods”.

The concepts I talked about here are just a few of the many, many intricate details of OOP, however these are just the basics to get you started. All though Lua may be unconventional in the way we apply the paradigm, it very well exists in the same way and is still used by most programmers within the platform.

1 Like

Good example is the Roblox instances.
Most things like BaseParts, MeshParts, etc. inherit ChildAdded, GetChildren and other things from a parent object.

It’s made to be reusable and easy to work with for multiple programmers.

If you’re working on your own, you can most likely do great without it. You don’t have to use it, as always there’s many ways to go about things in programming. Try it out, and see what you’re most comfortable with.

1 Like

tysm for the tip i will go diffently try and understand it

so what i understand is like OOP shares like table values? but the thing i can’t understand is like metatables

metatables are the special functions, basically helper functions in the table, for example:

local MT = {
	__newindex = function(t, key, value) -- runs whenever something added a new unique element to the table

		print(`Something has writen a new {key} element which is {value} into the table!`)

	end
}

local Object = setmetatable({ }, MT)
Object.X = 2 -- "Something has writen a new X element which is 2 into the table!"
Object.Y = Object.X * 2 -- "Something has writen a new Y element which is 4 into the table!"

Additionally there are arithmetic operations and so much more

local MT = {
	__add = function(a, b) -- runs whenever something tried to add (a) with (b) OR a + b

		return a[1] .. b[1] -- Concatenate the first element of both tables

	end
}
local Object = setmetatable({ "Hello" }, MT)
local Object2 = setmetatable({ "World" }, MT)

local ConcatenatedString = Object + Object2 -- "Hello" .. "World" which is "Hello World"

print(ConcatenatedString)
2 Likes

Tysm sooo much, thats what i’ve been looking for a long time.

1 Like

Ok, so like i could use OOP but sometimes i could script without it?

1 Like

Tysm i will do what u said and practice metatables!

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