Can someone help me understand this? (OOP/metatables) (.__index)

How to Organize & Style your Code in Roblox Development (and when to use OOP) - YouTube I just watched this video but I dont really get it. Can someone explain the concept of OOP (object oriented programming)? I tried looking up definitions but I didn’t really understand and can someone explain the deal with metatables and “.__index” and generally everything in this video?

…I need a tutorial for a tutorial im so stupid…

Well it might be a good idea to get started with basic metatable usage and understanding how they work before you try making a class and getting into the idea of abstraction and whatnot.

If there is something specific you’re having trouble with I can try to explain.

1 Like

alr, can you try to explain it? EDIT: oops I misunderstood

can you write me sample code for a metatable and explain what each part does if you can do that? I still don’t really get it…

There are many tutorials on metatables you can look at so I’m not too sure how regurgitating random information would be helpful, but I had trouble with metatables as well so I guess I can try putting it in a way that I wish someone had explained to me.

Basically whenever you try to manipulate a table, a present metamethod may be present in the metatable. If it’s present, it uses the present metamethod, to do something, can be either to do something to the table, or not to do something to the table.

Basically, you know how when you call a function you would typically call it by doing func()? Well if you try indexing a table with a metamethod, you can call a function without doing the parentheses.

For example if you want to detect someone trying to index your table with ‘key’ and you want to call a function when the table is indexed with ‘key’, and only when the key is ‘key’, you can do that.

local t = setmetatable({}, { __index = function(self, key) -- note __index will only fire if the key is not present in the table
    if key == 'key' then
        callSomeFunction()
    end
end})
local index = t.key -- will fire the metamethod

Same premise goes for all other metamethods, if you try to add a table with something else, it will fire the __add metamethod. Try to call ‘tostring’ with a table, the __tostring metamethod will fire.

This is a great tutorial on OOP.

To create an “Object” in lua you need two things

local Object = {}
Object.__index = Object

and

local newInstance = {}
setmetatable(newInstance, Object)

Say you call a function:
newInstance:Start()

If you didn’t specifically write
function newInstance:Start()

Then the roblox will complain and say we found no such function.

BUT if you set your metatable to Object:

setmetatable(newInstance, Object)

then lua will continue it’s search in Object!

NOTE: The search isn’t straight forward though, it wont just start looking for Object:Start()
It will check if your metatable (Object) has an __index function, and if so it will use it to continue it’s search.

So if you don’t specify an index function:

Object.__index = Object

then it doesn’t matter that you set the metatable.
You need both.

Here’s the full code:

Summary
local Object = {}
Object.__index = Object

function Object.new()
    local self = setmetatable({}, Object) --returns the first parameter
    return self
end

function Object:Start()
--do stuff
end

local newInstance = Object.new()
newInstance:Start()
1 Like

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