Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

self.action button example help please!

edited March 2013 in General Posts: 43

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 that is nil...but what is self.action()? what do the brackets do after it? anything close to an explanation would help!!!



  • Posts: 835

    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?

  • dave1707dave1707 Mod
    edited March 2013 Posts: 8,462

    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?

  • Posts: 182

    Or in simpler terms

    If (the function self.action does not exqual false i.e. exists) then
        run self.action
  • dave1707dave1707 Mod
    Posts: 8,462

    Maybe this will help explain what we're talking about.

    function setup()     func="this is a string"     print(func)     print()          func=123456     print(func)     print()          func=func1     print(func)     func()     print()          func=func2     print(func)     func()     end function func1()     print("executed func1") end function func2()     print("executed func2") end
  • Posts: 835

    And nil is blank or empty, not false

  • Thanks guys! I think I understand now. LOVE CODEA!

  • Posts: 835

    do you have Skype?

  • Posts: 2,161

    Just to correct one thing.

    if var then

    The condition is false if var is false or nil and true otherwise.

Sign In or Register to comment.