Hi, I’m looking at the button code in the Sounds Plus example in the Codea app.
I am trying to understand what this means:
if self.action then
What is this part doing? I see self.action is set to nil in the init part…so that is nil…but what is self.action()? what do the brackets do after it? anything close to an explanation would help!!!
The action which the button performs. The self.action() means the action is changed from nil elsewhere
I still don’t understand, sorry. How can a variable (self.action) also be the name of a function (self.action())? is this allowed?
Because self.action ( a variable ) is a pointer to a function. It’s originally set to nil in Button:init but later set to point to a function in the other classes. So if it’s not nil, then it’s pointing to a function.
Ok, so, self.action starts as nil, then when the button is pressed it changes and runs the function from Main.lua…
Question: If self.action is set to nil at init, why does it still activate the if function? isn’t nil like FALSE?
Or in simpler terms
If (the function self.action does not exqual false i.e. exists) then
Maybe this will help explain what we’re talking about.
func="this is a string"
And nil is blank or empty, not false
Thanks guys! I think I understand now. LOVE CODEA!
do you have Skype?
Just to correct one thing.
if var then
The condition is false if
nil and true otherwise.