I don’t understand self. In the slightest. Any help appreciated, coding examples preferred.
Thank you!
At its simplest: if you define a method inside a table with a colon, self refers to the table:
local tab = {
something = "Hello",
}
function tab:PrintSomething()
print(self.something)
end
tab:PrintSomething() --> Hello
Calling a method with : automatically inserts the table as the first argument. In other words, this works:
tab.PrintSomething(tab) --> Hello
You can also define the function with a dot and call with a colon, but then you’d need to include the table in your argument list:
function tab.PrintSomethingDot(originalTable)
print(originalTable.something)
end
tab.PrintSomethingDot(tab) --> Hello
tab:PrintSomethingDot() --> Hello
3 Likes