Skip to content

Commit

Permalink
Added sequence.wait_until_true() and wait_until_false()
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Aug 12, 2019
1 parent ece7367 commit 7a0c58b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/sequence/sequence.script
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ end

function init(self)
sequence.run_loop(function()
print("Start of loop")

-- animate game object and wait until done
print("go.animate")
sequence.go_animate(".", "position", go.PLAYBACK_ONCE_FORWARD, random_position(), go.EASING_INOUTQUAD, 1, 0)

-- make http GET and wait for response
print("http.request")
local response = sequence.http_get("https://postman-echo.com/get?foo1=bar1&foo2=bar2")
pprint(response)

-- wait 2.5 seconds
print("delay")
sequence.delay(2.5)

-- call any function and pass a callback to be invoked when done
Expand All @@ -25,5 +30,14 @@ function init(self)
cb(1, 2, 3)
end)
print(one, two, three)

local countdown = 10
sequence.wait_until_false(function()
print(countdown)
countdown = countdown - 1
return countdown > 0
end)

print("End of loop")
end)
end
22 changes: 22 additions & 0 deletions ludobits/m/sequence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ function M.delay(seconds)
coroutine.yield()
end

function M.wait_until_false(fn, ...)
local co = coroutine.running()
timer.delay(0, true, function(self, handle, time_elapsed)
if not fn(...) then
timer.cancel(handle)
coroutine.resume(co)
end
end)
coroutine.yield()
end

function M.wait_until_true(fn, ...)
local co = coroutine.running()
timer.delay(0, true, function(self, handle, time_elapsed)
if fn(...) then
timer.cancel(handle)
coroutine.resume(co)
end
end)
coroutine.yield()
end

function M.gui_animate(node, property, to, easing, duration, delay, playback)
local co = coroutine.running()
assert(co, "You must call this from inside a sequence")
Expand Down

0 comments on commit 7a0c58b

Please sign in to comment.