What would be a good way to learn about OOP?

Hi,

So i want to learn about OOP, and I am not sure about exactly how to do it.

But i have seen people use self, metatables, and in few cases i saw: fenv was included, which is something I’m interested in learning about.

But some Tutorials and Documentation on the Topic are either:

  • Vague Explanation
    As in it isnt covered very much

  • Fast
    They explain it (or cover it) very Briefly (Connected with Vague Explanation)

  • Confusing
    They would Expect you to know other Methods or would explain it as something else.

However, this just could be me not fully understanding the concept, as I have been trying to find an accurate and more detailed answer, which I cant.

If you want to, this can just be called:
What is get/setmetatable() and get/setfenv()?

From what I read, metatables make your tables more “powerful” according to Documentation

Metatables allow tables to become more powerful than before.

So I was wondering what would be a good way for me to learn about it? Or at least a more detailed explanation about it?

Thanks.

metatables store the methods used to interact with the table, which can let you control how items are read and written from the table. You can also override some operators used for comparison. However, metatables and functions in general do not get sent through remotes which severely limits their usefulness in my opinion. I’m not the biggest fan of Lua’s oop because it is very much unlike the OOP you will find in other languages, and it doesn’t have strong guarantees about type or interfaces, which is usually the biggest feature of OOP in other languages like java and C#.

2 Likes

Watching tutorials like these can help you, he goes over self and metatables in the playlist

Yes, but this doesn’t really help with what I’m asking about, and again:

1 Like

If you think it is confusing or a bit fast, you should get more experience before you try something this advance. It’s like starting a huge project as a beginner.

what do you mean by “more experience?”
I’m just saying its hard to follow on tutorials as they explain stuff very fast

I mean like fully understanding and grasping all the basic scripting such as modules.

modules are a part of OOP.
And yes, I’m pretty sure I understand the Basic parts of Modules

Ok, then if you aren’t understanding it you should try researching on google such as “what is oop” and maybe it can be less confusing for you.

Me personally I watched tutorials and I didn’t find it confusing.

Yes, I have, I get the Concept but I don’t understand its uses, even as the other person stated , there are different versions of OOP.

That’s just you, the tutorials I watched, and the Docs i looked at just seem to vaguely explain certain parts and then just except you to know it, it did a bit of research on it, and what the functions do, but it still doesn’t clear up any confusion I had.

1 Like

Here’s the basic rundown:

Tables can have functions with an implied self argument that is automatically filled when using colon syntax.

local Car = {}
Car.Speed = 5

function Car:Boost() -- 'self' is implied and is equivalent to 'Car'
	self.Speed += 5
end

Car:Boost()
Car.Boost(Car) -- We can also use dot syntax and pass 'self' directly

This allows for tables to act as “Objects” in the sense they can both carry independent “Properties” and shared “Methods”.

function CreateCar() -- Our 'Constructor' function
	local Car = {}
	Car.Speed = 5

	function Car:Boost()
		self.Speed += 5
	end

	return Car
end

local Car1 = CreateCar()
local Car2 = CreateCar()

Car1:Boost() -- Affects only Car1's 'Speed'
Car2:Boost()

However, these methods will take up extra unneeded memory when they are duplicated for each table, so we usually use __index to shortcut them in.

local Methods = {} -- Our constant table for methods
function Methods:Boost()
	self.Speed += 5
end

function CreateCar()
	local Car = {}
	setmetatable(Car, {__index = Methods})  -- We set a metatable with the __index metamethod
											-- So that Lua looks through the 'Methods' table when we call something nonexistent in 'Car'
	Car.Speed = 5
	
	return Car
end

Understandably, this is bulky, so for convenience we can mix our Methods and Metatable tables together.

local Methods = {}
Methods.__index = Methods -- 'If we try to index a nonexistent value (or method), look through Methods'

function Methods:Boost()
	self.Speed += 5
end

function CreateCar()
	local Car = {}
	setmetatable(Car, Methods)
	
	Car.Speed = 5
	
	return Car
end

And finally for the cherry on top, we can merge the constructor (CreateCar) with the Method table to make one big Car class.

local Car = {}
Car.__index = Car

function Car.new()
	local newCar = setmetatable({}, Car) -- setmetatable returns the table constructed
										 -- You can also name 'newCar' as 'self' to be consistent
	newCar.Speed = 5

	return newCar
end

function Car:Boost() -- 'Boost' is apart of 'Car', ergo newCar can use it
	self.Speed += 5
end

There are also other things such as Inheritance and Type-Checking, including some performance opportunities such as declaring self as a table literal.

1 Like

Hi, Sorry for late Response

Thanks but do you mind explaining the code with --comments rather than just giving it out? I’m still a bit confused on some parts, i was wondering if you could clear it up?

Metatables have these things called metamethods, which do allow tables to become more powerful than before. Here is an example of a metamethod, then you can go off the docs for more info.

local myTable = {5 ,10, 15, 20} -- declare our regular array

setmetatable(myTable, { -- set the metatable
    __call = function(table, parameter) -- this metamethod fires when the table is called as a function and returns the table and the parameter it was called with.
        return myTable[math.random(1,#myTable)] * parameter -- multiply our numbers and return them.
    end
})

myTable(5) -- multiplies a random number in that table by it's param, 5
1 Like

These will set/get the function environment, these are pretty much useless and could break your code so don’t use em.

Thanks, but it still doesnt exact answer my question on what metatables (along with get/setmetatable) do, which is the part on confused on, I have tried to look into it with Docs and vids but I cant seem to understand it. (unless im missing something very obvious)

noted.


The code given also returns attempt to call a table value


@2jammers so after testing a bit with other methods, seems to work, but what can these (or most of them) be used for?

They are mainly used for if you want unique behavior, so for instance I could use the __tostring method in the table to the return a concatenated version of it.

Or, you could use __newindex for custom behavior when someone wants to create a new index in the table.

Oh, thats Interesting, so what would happen if someone did:

setmetatable({}, Table)

Instead of:

setmetatable(Table, {
    -- code
})

would it function the same?

Table would need to contain metamethods and the other contain info. Nothing different going on here.

1 Like

Oh, that’s cool, Thanks!

Also yeah, I was missing something blatantly Obvious:

They are attached to data and contain values called metamethods. Metamethods are fired when a certain action is used with the datum that it is attached to.

1 Like

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