timer.performWithDelay( ) how to write the listener parameter

I am learning LUA and I am using the code of Match-Three-Space-RPG-master.

There is this line:

timer.performWithDelay(250, function () board:drop(“down”) end)

It works fine of course.

What is the purpose of “function() … end” ? Why not write it this way:

timer.performWithDelay( 250, board:drop(“down”) )

It did bug when I did that. Some pieces started missing when the drops occurred.

There is no explanation in the documentation, where it is shown this way: 

timer.performWithDelay( delay, listener [, iterations] )

 

with example:

local listener = {}

function listener:timer( event )

    print( “listener called” )

end

 

timer.performWithDelay( 1000, listener )

You can’t pass parameters to a function the second way, so the function wrapper allows you to do that.

Got it. Thank you :slight_smile:

Let me add on to this.

When you see an API call that expects a function, it’s looking for the “Address of the function”. In Lua when you put () after a function name, it runs the function and passes the results of that function to the API call, which more often than not, isn’t a function. Most functions don’t return anything. If you don’t use () you get the address of the function.  Consider this:

local function doSomething( someNumber )      local someOtherNumber = someNumber + 10 end timer.performWithDelay( 1000, doSomething( 10 ) )

What happens in this case, is the function doSomething runs immediately. someOtherNumber is calculated but the function doesn’t return anything, so the system will return “nil” for you. This means you really are saying:

timer.performWithDelay( 1000, nil )

Now timer.performWithDelay() has nothing to do. Let’s look at another example:

local function doSomething( someNumber ) local someOtherNumber = someNumber + 10 return someOtherNumber end timer.performWithDelay( 1000, doSomething( 10 ) )

In this case, doSomething is run immediately, 10 is added to 10, temporarily stored in the variableSomeOtherNumber and then that value is returned. This makes the timer really this:

timer.performWithDelay( 1000, 20 )

20 is an invalid memory address.

Now let’s say the doSomething function is at memory location 0x98761234. If you do:

timer.performWithDelay( 1000, doSomething )  -- no parameters passed

the timer call becomes:

timer.performWithDelay( 1000, 0x98761234 )

and the timer.performWithDelay() API now has a function to execute.

When you write:

timer.performWithDelay( 1000, function() doSomething(10); end )

The function() end pair creates an anonymous function, that is it’s not storing the address to the function in a variable, but it **IS** returning an address to a function to timer.performWithDelay(). Then in 1000 ms, that anonymous function is run which calls doSomething( 10 ), passing the value to the function.

Hope this helps you understand the problem and why Nick’s solution works.

Rob

Thank you, Rob. Very clear answer.

If I can not talk, it will explode. I can not believe that @Rob just gave a university class of “timer.performWithDelay” and the best answer of this topic is “Thank you, Rob, Very clear answer.” OMG.

@equesjova I think that you need to re-read the topic and and ask yourself if that is really the best answer.

Just an opinion, in a good way. LMAO…

Sorry, I am new. I had hit the wrong button. This is now corrected.

You can’t pass parameters to a function the second way, so the function wrapper allows you to do that.

Got it. Thank you :slight_smile:

Let me add on to this.

When you see an API call that expects a function, it’s looking for the “Address of the function”. In Lua when you put () after a function name, it runs the function and passes the results of that function to the API call, which more often than not, isn’t a function. Most functions don’t return anything. If you don’t use () you get the address of the function.  Consider this:

local function doSomething( someNumber )      local someOtherNumber = someNumber + 10 end timer.performWithDelay( 1000, doSomething( 10 ) )

What happens in this case, is the function doSomething runs immediately. someOtherNumber is calculated but the function doesn’t return anything, so the system will return “nil” for you. This means you really are saying:

timer.performWithDelay( 1000, nil )

Now timer.performWithDelay() has nothing to do. Let’s look at another example:

local function doSomething( someNumber ) local someOtherNumber = someNumber + 10 return someOtherNumber end timer.performWithDelay( 1000, doSomething( 10 ) )

In this case, doSomething is run immediately, 10 is added to 10, temporarily stored in the variableSomeOtherNumber and then that value is returned. This makes the timer really this:

timer.performWithDelay( 1000, 20 )

20 is an invalid memory address.

Now let’s say the doSomething function is at memory location 0x98761234. If you do:

timer.performWithDelay( 1000, doSomething )  -- no parameters passed

the timer call becomes:

timer.performWithDelay( 1000, 0x98761234 )

and the timer.performWithDelay() API now has a function to execute.

When you write:

timer.performWithDelay( 1000, function() doSomething(10); end )

The function() end pair creates an anonymous function, that is it’s not storing the address to the function in a variable, but it **IS** returning an address to a function to timer.performWithDelay(). Then in 1000 ms, that anonymous function is run which calls doSomething( 10 ), passing the value to the function.

Hope this helps you understand the problem and why Nick’s solution works.

Rob

Thank you, Rob. Very clear answer.

If I can not talk, it will explode. I can not believe that @Rob just gave a university class of “timer.performWithDelay” and the best answer of this topic is “Thank you, Rob, Very clear answer.” OMG.

@equesjova I think that you need to re-read the topic and and ask yourself if that is really the best answer.

Just an opinion, in a good way. LMAO…

Sorry, I am new. I had hit the wrong button. This is now corrected.