Main Content

Passing parameters to a coroutine in Lua

Archive - Originally posted on "The Horse's Mouth" - 2009-08-01 11:09:14 - Graham Ellis

Lua's coroutines, like Python's generators, provide for something which is akin to lightweight threading - rather than a strict functional calling stack, where one function must be completed before another is invoked, you can call a code block (function or method) and have it yield back to the calling routine - i.e. suspend operation - without running to completion.

In Lua ... it works like this:

• You create a coroutine with coroutine.create which you pass a function, and it returns a variable (of type thread) which you use as a parameter to ...
coroutine.resume; called the first time, this starts the coroutine running until it hits ...
coroutine.yield which suspends operation and returns control to the resume.
• You enclose your coroutine.resume in a loop, checking for a false return to indicate it has completed.

The question was asked on Thursday's course (and an excellent question) "How do I pass parameters to and from a coroutine, and looking at my stock answers, I realise that the question had hit a slightly embarrassing hole in my resources. Because it isn't obvious, and you WILL want to pass values in and out of the coroutine, considering how closely the co-existent routines are, logically, to each other.

The answer is as follows

1. You CANNOT pass parameters in to the coroutine function in coroutine.create. Thinking about it, it would be illogical to do so at the early stage of defining the routine; you're defining and nit running it after all, so you won't necessarily have your inputs ready anyway!

2. You can pass the parameters to start the co-routine in as the second (and subsequent) parameters to your first coroutine.resume

3. You can pass further data into the coroutine as the second and subsequent parameters to your subsequent coroutine.resume calls, and they will be returned by the coroutine.yield within the coroutine function

4. Data is passed back from the co-routine at each coroutine.yield ... parameters to the yield become the second and subsequent return values from coroutine.resume

Sample program ... source code here ... sample results:

[trainee@easterton toweb]$ lua inco
co 1
hello 1 gerald
dd 11 nil
co 2
hello 2 gerald
dd 11 nil
co 3
hello 3 gerald
dd 11 nil
co 4
[snip]
hello 10 gerald
dd 11 nil
co 11
hello 11 gerald
dd 11 nil
hello nil nil
[trainee@easterton toweb]$


As well as a single co-routine implementation, where it is useful as a data factory, or as a processing member (as in my example code), you can have whole tables of coroutines; they become very useful in simulations, where each coroutine call advances the simulation / makes the next game move for a different character of other game element. (This is just one of the reasons that Lua is making it so big in the games industry at the moment ... and you can simulate not only game movements, but things like town growth too ...)