I made an efficient sleep function

When I asked how to make the game sleep for x miliseconds I was told that there was no concept of waiting so I then set out on a journey to discover a sleep function, I came out with this.
 

function sleep(n) timer.performWithDelay(n,function() coroutine.resume(co) end) coroutine.yield() return true end function main() local i = 0 while sleep(1000) do i = i + 1 print(i) end end co = coroutine.create(main) coroutine.resume( co )

I don’t see a downside to using this. before I was using a recursive function in a perform with delay to achieve a delay effect.

This was the thread I was referring to when i said

I was told that there was no concept of waiting

https://forums.coronalabs.com/topic/50088-how-to-wait-a-certain-amount-of-time/

There is a huge downside. Corona SDK will not update the screen but once every second because you’re putting it into a busy wait loop.

Ironcally you’re using a timer to do the work. If you want something to happen every 1000 ms, just use a timer.  You’re blocking other events that could be going on. Your user interface will become non-responsive for brief periods of times.

If you need a game loop, you can use the Runtime “enterFrame” event which gets called either 30 or 60 times per second and corresponds with screen updates and test to see if your “delay” time has passed and take action. 

It’s your choice, if you want to busy-wait your app, then you can but I think you’re going to find it much harder to build your game and have it perform as best as it can.

Rob

This was the thread I was referring to when i said

I was told that there was no concept of waiting

https://forums.coronalabs.com/topic/50088-how-to-wait-a-certain-amount-of-time/

There is a huge downside. Corona SDK will not update the screen but once every second because you’re putting it into a busy wait loop.

Ironcally you’re using a timer to do the work. If you want something to happen every 1000 ms, just use a timer.  You’re blocking other events that could be going on. Your user interface will become non-responsive for brief periods of times.

If you need a game loop, you can use the Runtime “enterFrame” event which gets called either 30 or 60 times per second and corresponds with screen updates and test to see if your “delay” time has passed and take action. 

It’s your choice, if you want to busy-wait your app, then you can but I think you’re going to find it much harder to build your game and have it perform as best as it can.

Rob