: just carries self when you’re using tables. . doesn’t carry self when using tables.
--if you want to access something in the main table, use :
function Main:DoThis()
local a = self.a --you get self
end
--If you're making a function and you don't want self just use .
function Main.DoThing()
--etc
end
--If you DO want self, you would have to place it as one of the arguments
function Main.DoThing(self)
--etc
end
Others have already covered it, but maybe this helps too.
It’s basically just short-hand code. Some call it “syntactic sugar.”
When you call a table function with :, it injects the table itself as the first argument:
myTable.Hello(myTable, "Test")
SAME AS:
myTable:Hello("Test")
When you define a table function with :, it creates a hidden self parameter as the first parameter.
function myTable.Hello(self, msg) print(self == myTable) end
SAME AS:
function myTable:Hello(msg) print(self == myTable) end
So ideally, you match the caller and the definition. If it’s defined using :, then it makes sense to call it using :. If it’s defined using ., then call it with . too.