I am trying to use proper object oriented structure in my program, but running into an issue. I run into the issue when I try to call an object’s function from another function within the object using the “self” keyword. It is easier to give an example that explain:
--MAIN
function setup()
myRobot = Robot()
myRobot:giveInfo()
end
function draw()
background(40, 40, 50)
end
--ROBOT CLASS
Robot = class()
function Robot:init()
self.name = "Robo"
self.color = "grey"
end
function Robot:sayYourName()
print("My name is: " .. self.name)
end
function Robot:sayYourColor()
print("My color is: " .. self.color)
end
function Robot:giveInfo()
self:sayYourName() --ERROR HERE
self:sayYourColor() --AND HERE
end
I get this error: Attempt to index local “self” a nil value. Am I calling the function incorrectly? I have tried self.sayYourName() and self:sayYourName() with same results. Calling it like Robot:sayYourName() is inadequate because it does not have access to class variables.
Thanks.
Edit: I fixed a typo in the code, and replaced function call syntax with : instead of . and continue to receive same error.