How to sleep time in Codea (Lua)

How do you wait or sleep time in Lua???

1 Like

There isn’t a wait or sleep the way I think you mean. Depending on what your program is doing, you can code something that would look like wait or sleep, but Codea is constantly running code.

The user and all related content has been deleted.

Ah… @NatTheCoder I don’t think you know what I’m talking about. I’m a python/java geek, And both of these languages have a statement that looks like this:

Python:
import time
time.sleep(1)
--waits one second
Java: 
thread.sleep(1000)
--waits one second

And by wait or sleep I mean to wait a period of time, and then continue thread.

I thought C++ had sleep, and obj-C can use C++ code so why not have sleep in Codea?

I know right! Question to Codea: can you add a sleep function to type thread? Like ~~~
thread.sleep(0.03)~~~

thread.sleep(0.03)

@Luatee, I think because it would not not really mix well with the constantly updating draw function? Unless you used it like in a function not called from draw and it paused the refresh of draw for that long?

Yes!!! That is perfect!

@aurumcoder2624, I’m sure I could work something out to implement something like that - I’ll try and get back to you with some code :slight_smile:

look at tween.delay() in the doc.

or this discussion:
http://codea.io/talk/discussion/comment/18197

@JakAttak I was thinking of this go in to a codea project and run it, then scroll the output window. That sleeps the whole code except for box2d as thats separate and you can end up with a DeltaTime of 2 seconds and more.

@aurumcoder2624, i have written the code for a sleep function, and a main tab with an example of use. here:


--# Main
-- Sleep

displayMode(OVERLAY)
function setup()
    e = { x = WIDTH / 2, y = HEIGHT / 2, size = 20 }
    
    tween(2, e, { x = WIDTH }, tween.easing.linear, function()
        tween(2, e, { x = 0 }, { easing = tween.easing.linear, loop = tween.loop.pingpong })
    end)
end

function draw()
    background(40, 40, 50)

    -- increment size
    e.size = e.size + 1
    if e.size > WIDTH then e.size = 0 end
    
    fill(255)
    ellipse(e.x, e.y, e.size)
    
    text("Tap to sleep", WIDTH / 2, HEIGHT / 20)
end

function touched(touch)
    if touch.state == ENDED then
        sleep(math.random(1, 5))
    end
end

--# Sleep
sleep = { img = image(1,1), time = 0, start = 0 }

function sleep.init(time)
    -- Pause everything
    tween.pauseAll()
    physics.pause()
    -- Store what draw currently looks like in image
    sleep.img = image(WIDTH, HEIGHT) setContext(sleep.img)
    draw() setContext()
    -- Override draw function
    draw = _draw
    
    sleep.time = (time or 1)
    sleep.start = ElapsedTime
    print("Sleeping for ", sleep.time, " seconds")
end

function sleep.done()
    -- Unpause everything
    tween.resumeAll()
    physics.resume()
    -- Set draw function back
    draw = __draw
    
    print("Done Sleeping.")
end
sleep.mt = { __call = function(_, t) sleep.init(t) end }

setmetatable(sleep, sleep.mt)


_draw = function()
    sprite(sleep.img, WIDTH / 2, HEIGHT / 2)
    
    if ElapsedTime >= sleep.start + sleep.time then
        sleep.done()
    end
end

__draw = draw

_update, pnop = tween.update, function() end
tween.pauseAll = function() tween.update = pnop end
tween.resumeAll = function() tween.update = _update end

(There can be a bit of lag when you call sleep because of the SetContext bug)

THANKS SO MUCH HOW CAN I EVER STOP THANKING YOU I WILL IMPLEMENT THIS CODE RIGHT AWAY AND ASK SIMEON TO ADD THIS IF HE IS NOT BUSY THANK U THANK U THANK U! :smiley:

What can I do for u @JakAttak

ok, @aurumcoder2624, we get the message. No need to bump it.

Please don’t bother Simeon, though. As others have pointed out, there are different ways to implement sleep already.

I’m not bothering Simeon or am I? :-/

If you write to him with this, yes you will be. Save it for something important.