Module m.flow
The flow module simplifies asynchronous flows of execution where your code needs to wait for one asynchronous operation to finish before starting with the next one.
Usage:
local flow = require "ludobits.m.flow" function init(self) flow.start(function() -- animate a gameobject and wait for animation to complete flow.go_animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_INCUBIC, 2) -- wait for one and a half seconds flow.delay(1.5) -- wait until a function returns true flow.until_true(is_game_over) -- animate go again flow.go_animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, 400, go.EASING_INCUBIC, 2) end) end function final(self) flow.stop() end function update(self, dt) flow.update() end function on_message(self, message_id, message, sender) flow.on_message(message_id, message, sender) end
Functions
start (fn, options, on_error) | Start a new flow. |
stop (instance) | Stop a created flow before it has completed |
delay (seconds) | Wait until a certain time has elapsed |
frames (frames) | Wait until a certain number of frames have elapsed |
until_true (fn) | Wait until a function returns true |
until_any_message () | Wait until any message is received |
until_message (message_1, message_2, message_n) | Wait until a specific message is received |
until_input_pressed (action_id) | Wait until input action with pressed state |
until_input_released (action_id) | Wait until input action with released state |
until_callback (fn, arg1, arg2, argn) | Wait until a callback function is invoked |
load (collection_url) | Load a collection and wait until it is loaded and enabled |
unload (collection_url) | Unload a collection and wait until it is unloaded |
go_animate (url, property, playback, to, easing, duration, delay) | Call go.animate and wait until it has finished |
gui_animate (node, property, playback, to, easing, duration, delay) | Call gui.animate and wait until it has finished NOTE: The argument order differs from gui.animate() (playback is shifted to the same position as for go.animate) |
play_animation (sprite_url, id) | Play a sprite animation and wait until it has finished |
ray_cast (from, to, groups) | Cast a physics ray and wait for a response for a maximum of one frame |
update (dt) | Call this as often as needed (every frame) |
on_message (message_id, message, sender) | Forward any received messages in your scripts to this function |
Functions
- start (fn, options, on_error)
-
Start a new flow. If this function is called from
within an existing flow the existing flow can either
wait for the new flow to finish or run in parallel
Parameters:
- fn The function to run within the flow
- options Key value pairs. Allowed keys: parallel = true if running flow shouldn't wait for this flow
- on_error Function to call if something goes wrong while running the flow
Returns:
-
The created flow instance
- stop (instance)
-
Stop a created flow before it has completed
Parameters:
- instance This can be either the returned value from a call to start, a coroutine or URL. Defaults to the URL of the running script
- delay (seconds)
-
Wait until a certain time has elapsed
Parameters:
- seconds
- frames (frames)
-
Wait until a certain number of frames have elapsed
Parameters:
- frames
- until_true (fn)
-
Wait until a function returns true
Parameters:
- fn
- until_any_message ()
-
Wait until any message is received
Returns:
- message_id
- message
- sender
- until_message (message_1, message_2, message_n)
-
Wait until a specific message is received
Parameters:
- message_1 Message to wait for
- message_2 Message to wait for
- message_n Message to wait for
Returns:
- message_id
- message
- sender
- until_input_pressed (action_id)
-
Wait until input action with pressed state
Parameters:
- action_id Optional action to wait for (nil for any action)
Returns:
- action_id
- action
- until_input_released (action_id)
-
Wait until input action with released state
Parameters:
- action_id Optional action to wait for (nil for any action)
Returns:
- action_id
- action
- until_callback (fn, arg1, arg2, argn)
-
Wait until a callback function is invoked
Parameters:
- fn The function to call. The function must take a callback function as its first argument
- arg1 Additional argument to pass to fn
- arg2 Additional argument to pass to fn
- argn Additional argument to pass to fn
Returns:
-
Any values passed to the callback function
- load (collection_url)
-
Load a collection and wait until it is loaded and enabled
Parameters:
- collection_url
- unload (collection_url)
-
Unload a collection and wait until it is unloaded
Parameters:
- collection_url The collection to unload
- go_animate (url, property, playback, to, easing, duration, delay)
-
Call go.animate and wait until it has finished
Parameters:
- url
- property
- playback
- to
- easing
- duration
- delay
- gui_animate (node, property, playback, to, easing, duration, delay)
-
Call gui.animate and wait until it has finished
NOTE: The argument order differs from gui.animate() (playback is shifted
to the same position as for go.animate)
Parameters:
- node
- property
- playback
- to
- easing
- duration
- delay
- play_animation (sprite_url, id)
-
Play a sprite animation and wait until it has finished
Parameters:
- sprite_url
- id
- ray_cast (from, to, groups)
-
Cast a physics ray and wait for a response for a maximum of one frame
Parameters:
- from
- to
- groups
Returns:
-
The ray cast response or nil if no hit
- update (dt)
-
Call this as often as needed (every frame)
Parameters:
- dt
- on_message (message_id, message, sender)
-
Forward any received messages in your scripts to this function
Parameters:
- message_id
- message
- sender