rdeioris / LuaMachine

Unreal Engine Plugin for Lua APIs implementation
MIT License
581 stars 120 forks source link

Able to have latent calls? #18

Closed iUltimateLP closed 3 years ago

iUltimateLP commented 3 years ago

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 3 years ago

@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):

immagine

you can directly spawn coroutine from blueprint/c++ (otherwise just use lua):

immagine

iUltimateLP commented 3 years ago

Great, thanks for your detailed answer!