I’m trying to figure out how to handle events using coroutines (in Lua). I see that a common way of doing it seems to be creating wrapper functions that yield the current coroutine and then resume it when the thing you’re waiting for has occured. That seems like a nice solution, but what about these problems? :
-
How do you wait for multiple events at the same time, and branch depending on which one comes first? Or should the program be redesigned to avoid such situations?
-
How to cancel the waiting after a certain period? The event loop can have timeout parameters in its socket send/receive wrappers, but what about custom events?
-
How do you trigger the coroutine to change its state from outside? For example, I would want a function that when called, would cause the coroutine to jump to a different step, or start waiting for a different event.
EDIT:
Currently I have a system where I register a coroutine with an event, and the coroutine gets resumed with the event name and info as parameters every time the event occurs. With this system, 1 and 2 are not issues, and 3 can solved by having the coro expect a special event name that makes it jump to the different step, and resuming it with that name as an arg. Also custom objects can have methods to register event handlers the same way.
I just wonder if this is considered the right way to use coroutines for event handling. For example, if I have a read event and a timer event (as a timeout for the read), and the read event happens first, I have to manually cancel the timer. It just doesn’t seem to fit the sequential nature or handling events with coroutines.
If you need to use coroutines for this, rather than just a Lua function that you register (for example, if you have a function that does stuff, waits for an event, then does more stuff), then this is pretty simple.
coroutine.yieldwill return all of the values passed tocoroutine.resumewhen the coroutine is resumed.So just pass the event, and let the script decide for itself if that’s the one it’s waiting for or not. Indeed, you could build a simple function to do this:
This function will continue to yield until one of the events it is given has been fired. The loop assumes that
RegisterForAnyEventis temporary, registering the function for just one event, so you need to re-register every time an event is fired.Put a counter in the above loop, and leave after a certain period of time. I’ll leave that as an exercise for the reader; it all depends on how your application measures time.
You cannot magic a Lua function into a different “state”. You can only call functions and have them return results. So if you want to skip around within some process, you must write your Lua function system to be able to be skippable.
How you do that is up to you. You could have each set of non-waiting commands be a separate Lua function. Or you could just design your wait states to be able to skip ahead. Or whatever.