Function calls

I have a function in my code that I’d like to re-fire based on user input. What I’ve got now looks something like this:

[lua]local myFunction = function(event)

    local listenerFunction = function(event)

        if event.index == 1 then

            --do nothing

        else

            --call to “myFunction” goes here

            print(“You’ve reached this point!”)

       end

    --some action here

    native.showAlert(“myApp”, “Would you like to retry this action?”, {“No”, “Yes”}, listenerFunction)

end[/lua]

However, I’ve tried this approach and it does not work. I know that the listenerFunction is being called, and I know that I reach the “You’ve reached this point!” print statement, but myFunction is not being called a second time. Any suggestions? 

That’s because the function myFunction is out of scope. Try attaching it to a table like this:

local myObject = {} myObject.myFunction = function(event) local listenerFunction = function(event) if event.index == 1 then --do nothing else --call to "myFunction" goes here myObject.myFunction() print("You've reached this point!") end end --some action here native.showAlert("myApp", "Would you like to retry this action?", {"No", "Yes"}, listenerFunction) end myFunction()

Or simply making it global by removing the local keyword, although this is a bit dangerous.

Thanks, hectots. 

That’s because the function myFunction is out of scope. Try attaching it to a table like this:

local myObject = {} myObject.myFunction = function(event) local listenerFunction = function(event) if event.index == 1 then --do nothing else --call to "myFunction" goes here myObject.myFunction() print("You've reached this point!") end end --some action here native.showAlert("myApp", "Would you like to retry this action?", {"No", "Yes"}, listenerFunction) end myFunction()

Or simply making it global by removing the local keyword, although this is a bit dangerous.

Thanks, hectots.