To clarify (mainly my second post) somewhat... Consider a task of getting data from the MUD using an alias. The process of doing so normally involves entering the alias, which executes a certain script and sends a command to the MUD. Some time later, the MUD output, produced by your command, returns to the client, a trigger fires and invokes another script, which processes the output and probably invokes yet another script to record the data or process it in some way, or to send another command to the MUD.
All of that results in many script functions (or executable script strings), spread all over a plugin or a world file, and tied together only through your creativity in naming them, and commenting the code. Essentially, what is a single procedure: get inventory, figure out if it contains an item, if yes - use that item, if not - search around for that item, etc.; falls apart into a collection of functions, which aren't even all that functional - they are procedures, chained together through some obscure sequence of calls to the MUD.
Using coroutines, on the other hand, you can define a procedure that encapsulates the entire process that you are trying to carry out. A coroutine has the following general form:
function co()
someActions()
coroutine.yield()
otherActions()
coroutine.yield()
moreActions()
coroutine.yield()
finally()
end
Here, each coroutine.yield() is a separate point of exit/re-entry into the procedure, which (like a normal point of exit/entry can return/take arguments). What this means is that with a coroutine, you can actually have the MUD return data to the procedure, like an ordinary function would.
You still need to define extra functions for communicating with Mushclient triggers/timers/aliases, but those functions become "pluggable" - they don't contain any logic of the overall process - they just talk to the MUD through Mushclient, get the data, and return that data back to the function that carries out your process. You can easily add new blocks that return new data, without needing to sift through all the pieces of your script, trying to figure out how all of it is tied together through triggers/aliases |