Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

LUA Sleep function
Started by scottoliver06 Mar 15 2016 09:30 AM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

scottoliver06

[GLOBAL: userInfoPane.html]
scottoliver06
  • Observer

  • 1 posts
  • Corona SDK

I am trying to make a script sleep and I am new to this language like 5 minutes new. How do you make it sleep or pause for 10 seconds?  I found this

 

function sleep(n)
os.execute("sleep " .. tonumber(n))
end

 

But how do you define (n)?



[TOPIC: post.html]
#2

torbenratzlaff

[GLOBAL: userInfoPane.html]
torbenratzlaff
  • Contributor

  • 404 posts
  • Corona SDK

Hey scottoliver,

I don't know about the code you posted, but it seems os.execute does not work reliable on all devices (depending on your input).

The important question is, what do you want to do. Do you really want to like freeze your app like your code suggests or do you just want to wait some time to execute some code?

 

If it's the second you should take a look at timers, which call a function after a given delay.

timer.performWithDelay( delay, listener )

Follow this link to the documentation for further information:

https://docs.coronalabs.com/api/library/timer/performWithDelay.html



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,534 posts
  • Enterprise

Sleeping is not how Corona works. It's an event driven system. You should learn to use timers to schedule things as @torbenzatzlaff said or use things like collisions (if you're using Physics), touch and tap events to cause actions to happen.



[TOPIC: post.html]
#4

ossi.koivula

[GLOBAL: userInfoPane.html]
ossi.koivula
  • Observer

  • 1 posts
  • Corona SDK

I downloaded Corona today and I'm still experimenting it, but to the point:

if I wanted to have a timer count from 3 to 1 yielding a second on each number I'd have to do something like

local textObj = display.newText("",160,0,"Arial",50)

local setText = function(obj,t)
obj.text = t
end

for i = 0,3 do
timer.performWithDelay(1000*i,function()
setText(textObj,3-i)
end)
end

It feels stupid to define a function for each little thing I need, when I could just do

local textObj = display.newText("",160,0,"Arial",50)

for i = 3,1,-1 do
textObj.text = i
wait(1) -- sleep(1000)
end

I've got 4 years with lua on ROBLOX so I'm probably just be used to their system. Also I've got no experience on yielding in other languages (well I just know a bit of PHP) so I don't know how they handle that. I would love to hear from someone if the first piece of code is even the correct way to do it. 



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,534 posts
  • Enterprise

You're close:

local textObj = display.newText("",160,0,"Arial",50)

local ticks = 3
local setText = function(obj,t)
    obj.text = t
end


timer.performWithDelay( 1000, function() setText(textObj, ticks); ticks = ticks - 1; end, 3)

This method tells the timer to run 3 times, with a 1 second delay.




[topic_controls]
[/topic_controls]