The colon is syntactic sugar for defining self. The alternate method is passing the object you’re calling the method on itself. This is not really ideal for OOP-in-Lua though because you want to have self in order to mutate object state rather than class state and it generally looks (and is) messier.
local foobar = {}
function foobar:frobnicate() end
-- The above can be called as either:
foobar:frobnicate()
foobar.frobnicate(foobar)
This is primarily what said syntactic sugar is referring to. If you’re using self as a parameter in a function declared with a period then you need to call the method with a colon.
local foobar = {}
function foobar.frobnicate(self, ...) end
foobar:frobnicate("hello")
Constructor functions never implement self unless using it as the variable for the created object, so your example in itself is not correct. The reason why your code underlines and is red is because you removed the parameter but did not declare a self variable in the constructor scope, so naturally if there’s no self to use then how are you going to interact with self? Removing self from the parameters isn’t sufficient enough. You need to read into errors as well as the offered advice a little more carefully. These are problems you could’ve caught earlier.
Self is not necessary in your constructor example at all, unless you’re unconventionally writing the constructor to be called with a colon and not a period, which then yes having self as a parameter would be valid but then you need to likewise call it that way as Person:New(). Since you set it up as Person.New(self, o) and called it as Person.new(), both arguments are nil. If you called it as Person:New(), self would at least be non-nil.
EDIT: If it were up to me to write this class, or provide an example of my advice in action, here’s some basic boilerplate for OOP in Lua:
local Person = {}
Person.__index = Person
--- Construct a blank person containing Gender, Age and Weight data
function Person.new()
return setmetatable({
Gender = "Unspecified",
Age = 0,
Weight = 0
}, Person)
end
function Person:SetGender(gender)
self.Gender = gender
end
function Person:SetAge(age)
self.Age = age
end
function Person:SetWeight(weight)
self.Weight = weight
end
function Person:OutputProfile()
print(self)
end
local MyPerson = Person.new()
MyPerson:OutputProfile()
MyPerson:SetGender("Female")
MyPerson:OutputProfile()
MyPerson:SetAge(44)
MyPerson:SetWeight(160)
MyPerson:OutputProfile()
Notice that nowhere do I use self except from within methods declared with colons because it’s otherwise not necessary to do so. In the instance that I instead declared those methods with dot syntax then I would need to include self as a parameter and then call it accordingly with a colon or pass the calling object.
-- Add this before constructing MyPerson
function Person.DotDeclaredOutputProfile(self)
print(self)
end
-- Add this after constructing MyPerson
MyPerson:DotDeclaredOutputProfile()
MyPerson.DotDeclaredOutputProfile(MyPerson)
Just a simple matter of fixing your constructor and reviewing errors in my opinion.