I understand the first parameter refers to the table you’ve indexed but I’ve seen it being simply put as “table” with the blue wording in one example and a random letter like “t” in another, so does it matter what you name the first parameter? How does the script know which table it’s referring to?
In alvinblox’s tutorial specifically, he had the first parameter as table. What if you had multiple tables, how would the script know which table you’re trying to call from that “table” alone in the first parameter?
If you are talking about a namecall method version of __newindex, the first parameter is normally the class (table in lua’s case) that the method resides in, then it goes the key of the member you were trying to add that didn’t exist, and then the value of that key.
local someclass = setmetatable({}, {
__newindex = function(self, key, value)
print(tostring(self), key, value);
end,
__tostring = function()
return "SomeClass"
end
})
someclass["somekey"] = "some value"; -- __newindex is invoked, and prints "SomeClass somekey some value"
Yes, it doesn’t require strict name, these symbols are just here to identify what that is meant to be, but it can be named anything, such as foo or bar
Yes, but only if it’s a namecall method (i.e. it uses the : call, unlike the dot call), it can also be a dot call method, but when calling it everytime you have to manually pass in the class
local someclass = setmetatable({}, {
__newindex = function(self, key, value)
print(tostring(self), key, value);
end,
__tostring = function()
return "SomeClass"
end
})
rawset(someclass, "SomeMethod", function(self, ...)
if (self ~= someclass) return error("Call with : instead of .") end;
end) -- using rawset so it doesn't invoke __newindex
someclass:SomeMethod(); -- No error as we used `:`, someclass is automatically passed in as the first parameter,
someclass.SomeMethod(); -- Error, as we didn't pass someclass in.
someclass.SomeMethod(someclass); -- No error, as we passed someclass in.