Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

timer.performWithDelay( ) how to write the listener parameter
Started by equesyova Sep 23 2018 06:02 AM

6 replies to this topic
performwithdelay

Best Answer Rob Miracle , 23 September 2018 - 05:54 PM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

equesyova

[GLOBAL: userInfoPane.html]
equesyova
  • Observer

  • 15 posts
  • Corona SDK

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

 

There is this line:

timer.performWithDelay(250function () 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 )

 



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,628 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

equesyova

[GLOBAL: userInfoPane.html]
equesyova
  • Observer

  • 15 posts
  • Corona SDK

Got it. Thank you :)



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,969 posts
  • Corona Staff

  Best Answer

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


  • dodi_games likes this

[TOPIC: post.html]
#5

equesyova

[GLOBAL: userInfoPane.html]
equesyova
  • Observer

  • 15 posts
  • Corona SDK

Thank you, Rob. Very clear answer.



[TOPIC: post.html]
#6

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 324 posts
  • Corona SDK

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....

[TOPIC: post.html]
#7

equesyova

[GLOBAL: userInfoPane.html]
equesyova
  • Observer

  • 15 posts
  • Corona SDK

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


  • dodi_games likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: performwithdelay