Main Content

Embedding Lua to perform tailored code at an interval

Archive - Originally posted on "The Horse's Mouth" - 2014-05-03 17:38:09 - Graham Ellis

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.