Able to have latent calls?
Closed this issue · 2 comments
iUltimateLP commented
Hey, I want to use this to implement robots in my puzzle game.
An example Lua code would look like this:
robot.walkForward()
robot.turnLeft()
robot.walkForward()
As you can see, I'd need the calls to be "blocking", or non-returning until the robot fully finished moving. Is that possible? Thanks!
rdeioris commented
@iUltimateLP you can use coroutines. Start by wrapping (you have various ways) the sequence of moves in a coroutine:
local robot = {}
function robot.actions()
-- get the currently running coroutine
coro = coroutine.running()
print('actions!')
-- pass the 'coro' to the blueprint to allow resuming later
robot_up(coro)
print('first step done')
-- this yield will suspend the coroutine, it will be resumed by the previous blueprint
coroutine.yield()
-- and again...
robot_up(coro)
print('second step done')
coroutine.yield()
-- and again...
robot_up(coro)
print('end of coroutine')
end
return robot
the robot_up function is a blueprint event (note how it calls the resume over the passed coroutine):
you can directly spawn coroutine from blueprint/c++ (otherwise just use lua):
iUltimateLP commented
Great, thanks for your detailed answer!