I am trying to understand how OOP works in Lua, and I thought that I have an easy way to do this, but it does not work, and I just don’t see the reason. Here is what I am trying:
Person = { };
function Person:newPerson(inName)
print(inName);
p = { };
p.myName = inName;
function p:sayHello()
print ("Hello, my name is " .. self.myName);
end
return p;
end
Frank = Person.newPerson("Frank");
Frank:sayHello();
FYI, I work with the Corona SDK, although I assume it doesn't matter (except where print () comes from, I suppose). In any case, the part that kills me is that inName is nil, which is reported by print (inName) ... so myName is obviously nil, so sayHello () calls fail (although they work it is ok if I hardcode the value for myName, which makes me think that the basic structure I'm trying to sound, but I need to skip something simple). Apparently, as far as I can tell, the inName value is not set when newPerson () is called, but I can’t understand for life why; I do not understand why this is not like any other function call.
Any help would be greatly appreciated. Thank!