Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
Embedding Lua to perform tailored code at an interval

Lua is a language that's commonly embedded within other products - a game, or a control system, for example - in order to provide a programmable tailoring of that other product. It's a great language for that - it's small, it's free, it has a suitable open source license, and it comes as a C library with a nice C API too.

So ... when I come to teach Lua, I find that only a small proportion of my delegates will be using it stand alone. Most will be embedding it by providing a file of functions, or simply a file (or a series of files) of code in which they examine and change variables.

On the course that I finished last week, the client's main code device and application makes a call to a piece of Lua code at intervals to perform tailored logic. Think of (as an example) a game where a character makes a decision as to whether to move each second, and the logic for that decision is the bit that you'll be coding in Lua.

Great approach ... but two questions:
1. How do I emulate / provide a test environment for the Lua logic
2. How do I set up my interval code so that it can do several things, perhaps each at a different interval?

I've put a sample [here] on our web site.

  latest = os.time()
  while true do -- outer loop - runs actions as need be
  while true do -- inner tight loop - wait for end of second
    now = os.time()
    if now - latest > 0.9 then break end -- interval of one second (os.time is to nearest second
  end
  latest = now
  
  -- This is where the code to run every second goes!
  
  end


In order to write clean code for multiple actions, I've populated an indexed table with a series of keyed tables about each action - I've chosen to have keys which contain
* The interval for this action
* What the action is
* When the action was last performed
* A values to be passed into the action
* A "static" variable to be maintained / updated by the action.
This is very much a demonstration - you can and should decide what you want in your own API for this sort of thing - many of the elements are likely to be along the lines of my examples.

  actions = {
  {["interval"] = 4, ["action"] = first , ["recent"] = latest, ["on"] = "xx", ["c"] = 0},
  {["interval"] = 7, ["action"] = another , ["recent"] = latest, ["on"] = "yy", ["c"] = 0},
  {["interval"] = 14, ["action"] = another , ["recent"] = latest, ["on"] = "zz", ["c"] = 0},
  {["interval"] = 17, ["action"] = another , ["recent"] = latest, ["on"] = "pdq", ["c"] = 0},
  {["interval"] = 5, ["action"] = second , ["recent"] = latest, ["on"] = "rtfm", ["c"] = 0}
  }


and here is the code that runs each of the necessary actions from the table

  for _,watch in pairs(actions) do
    since = latest - watch.recent
    if since >= watch.interval then
      watch.c = watch.action(watch.c, watch.on)
      watch.recent = latest
    end
  end


with each individual action code being defined as a function, for example

  function first(count)
    print ("this in number one",count)
    return count + 1
    end


Lua's coroutines are another very powerful way of interlacing (threading) code and most of the time I would recommend that you use them to suspend and continue actions - they may way lay on top of the example I've given hear, with first and its siblings being created as coroutines and then resumed to yield within each control loop call.
(written 2014-05-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U114 - Lua - Threading and Coroutines.
  [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)
  [2314] Passing parameters to a coroutine in Lua - (2009-08-01)
  [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
Survey - Journeys
Previous and next
or
Horse's mouth home
Forward to
Line, block and nested comments - Lua compared to other languages
Some other Articles
String formatting in Lua - string.format as a wrapper for sprintf
Dot or Colon separator between table name and member in Lua - what is the difference?
Lazy operators in Lua - what they mean, and examples
Line, block and nested comments - Lua compared to other languages
Embedding Lua to perform tailored code at an interval
Survey - Journeys
Metrics - historic and current TransWilts train use
How effective is our publicity?
Facebook marketing - who are we reaching?
A lovely meal in Swindon - just a short walk from the station
4755 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., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4270_Emb ... erval.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat