Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
Passing parameters to a coroutine in Lua

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
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 ...)
(written 2009-08-01)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U114 - Lua - Threading and Coroutines.
  [4270] Embedding Lua to perform tailored code at an interval - (2014-05-03)
  [3395] Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [1699] If you are learning Lua, here are some more examples - (2008-07-06)
  [1691] Co-routines in Lua - co-operative processing - (2008-06-29)

Back to
Seeing Guadalajara - a first glimpse downtown
Previous and next
Horse's mouth home
Forward to
New Lua Examples - for last weeks delegates
Some other Articles
For Lua Programmers AND for Town Planners
How to make a Risotto (PHP build style)
Guadalajara - a special tour of a lovely city
New Lua Examples - for last weeks delegates
Passing parameters to a coroutine in Lua
Seeing Guadalajara - a first glimpse downtown
What are closures in Lua?
Floor to ceiling
Learning to write high quality code in Lua
Lua training class in Spanish
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2314_Pas ... n-Lua.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat