How to use metatables in a good way

i know how to use __index and __newindex and __call but like i want to know how i can use them in other ways rather than these metamethods

3 Likes

All of these would be great for OOP systems. Something like this is a good example of using __index:

local m={}
function m.new(name)
   local s = setmetatable({}, m)
   s.__index = s
   s.name = name
   return s
end
function m:do()
   print(`success {self.name}!`)
end
return m
local m=require(path)
local object=m.new("John")
object:do()
-> success John!

In this example it just basically just uh creates a object from the class (with no methods in it) and it has a index reference to the class m and so when we call the :do method it just looks in m and finds it… and calls it

and you can see in the :do method, I use self.name which we assigned in the .new function earlier. Basically self is whatever is calling the function, in this case it’s the new object so it prints its name John

why is this useful? Cause you won’t have to rewrite functions for every new object and it’s just better for aesthetics

3 Likes

Like @Doomcolp said, metatables and metamethods are really helpful for OOP systems. Metatables mean the given table will inherit all that is in the set table, including metamethods. They are very helpful for cross-table type things, for instance setting an object’s __index to the given table.

4 Likes

__newindex would be really useful for proxy tables

so if you needed to track if a change happens in a table, then newindex would be useful

For __call (which I doubt still works but I haven’t checked yet) you would kinda use it for a similar thing, but instead you’re just gonna use it to check if something is fired. I’m not gonna give any examples since I can’t think of any at the moment lol

1 Like

why did u do that- like what is the purpose what does it do?

Oh my bad I meant to put it as s.__index = m

just a typo

I thought you said you knew how they worked?

I’ll link one of my responses just gimme a second

1 Like

i know what why do u set it like that i dont get the purpose or how it works

__call is when you call a metatable like a function

1 Like

oh yeah you’re right mb

But here’s that post

1 Like

i know that __index just calls when u type something nil in a metatable
(im pretty new to metatable so sorry)

1 Like

All it’s saying is that the new object (table) s is going to look in the table m, AKA the class whenever it trys to index a nil value

so it just copies the old table aka the normal properties?? i dont get it what happens if i dont set it like WHAAAAA ITS SO CONFUSING. ALSO I THOUGHT SETTING THE METATABLE DID THE __INDEX THING ALREADY :sob:

okay okay I’ll write out what I mean:

unset

local tabOne={
["Invisible"] = "this is not invisible"
}
local tabTwo=setmetatable({}, tabOne)
print(tabTwo.Invisible)
-> nil

set

local tabOne={
["Invisible"] = "this is not invisible"
}
local tabTwo=setmetatable({}, tabOne)
tabTwo.__index = tabOne
print(tabTwo.Invisible)
-> this is not invisible
1 Like

nope it doesn’t really do that

1 Like

When you set __index to another table instead of a function, it just looks in that table rather than run your custom function

1 Like

explain it but like summarize it please im gonna cry and the table was emtpy how it gonna call if theres nothing

please wroite me a smple script with what u are saying

oh like that so thats how u do it uhmm sorry for my drama :innocent: thank you for ur help :sparkling_heart:have fun :nail_care:t2: :stuck_out_tongue_winking_eye::stuck_out_tongue:

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