Following Nick's examples of using coroutines to pause a script until input is received from the MUD, here's a simpler but more flexible method:
This creates "deferreds" - functions that start an asynchronous action, which can be signaled completed at a later time. A deferred is created by passing it a function to be called asynchronously (func) and a callback function, to be invoked once the action initiated by func is completed.
To use it:
This method is mainly useful when the "MUD-bound" action involves triggering or otherwise processing lots of MUD output, not just one line. In such cases you can't really predict what exactly you'll need to do, which functions exactly will be called by triggers, timers, aliases, and how many times. So you simply start the action and resume processing in a different (callback) function, usually passing it any results you gathered from the MUD>
function defer(func, callback)
local func2 = coroutine.create(function(...)
func(arg)
local args = {coroutine.yield()}
callback(args)
end)
return function(...) coroutine.resume(func2,arg) end
end
This creates "deferreds" - functions that start an asynchronous action, which can be signaled completed at a later time. A deferred is created by passing it a function to be called asynchronously (func) and a callback function, to be invoked once the action initiated by func is completed.
To use it:
-- create a deferred
deferred = defer(function(...) print("start some MUD input bound action") end,
function(...) print("this is called when the action completes") end)
-- start the action by calling the deferred
-- whatever args you pass to it, will be relayed to a
-- corresponding function
deferred("foo", "bar")
-- once you've finished processing MUD output, call the
-- deferred again to initiate the callback, which should
-- resume paused processing
deferred("MUD result1", "MUD result2")
This method is mainly useful when the "MUD-bound" action involves triggering or otherwise processing lots of MUD output, not just one line. In such cases you can't really predict what exactly you'll need to do, which functions exactly will be called by triggers, timers, aliases, and how many times. So you simply start the action and resume processing in a different (callback) function, usually passing it any results you gathered from the MUD>