lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 21 November 2005 7:50 pm, Beric Holt wrote:
> Unfortunately this option works in the opposite direction to the way
> that my game platform is built.  My game platform run cycle is driven
> from the C++ end, not lua.

i guess that's the usual case, and the easiest to write.  but it's not 
difficult to turn it inside-out using coroutines; it's called "inversion of 
control"

this is exactly what Thomas suggested; you can think of a coroutine as a 
easier way to write a state machine, where the main 'state' is precisely the 
coroutine state

instead of doing something like:

---------------- start example 1
function stateMachine(input)

  if state == 1 then
    -- something
  elseif state == 2 then
    -- something else
  elseif state == 3 then
.
.
.
.
  else 
    -- error
  end
  -- final processes
  return output
end
----------------- end example 1

you write things like this:

----------------- start example 2
function invertedStateMachine(initinput)
  -- initialization code
  while not finish do
    input = coroutine.yield (output)
    -- do things
  end
  return finalizingoutput
end

stateMachine = coroutine.wrap (invertedStateMachine)
------------------ end example 2

on both cases, your C code calls stateMachine() for each iteration, with some 
parameters as input, and gets some parameters as output; in fact, from the C 
code it's exactly the same, even if example 2 is written as an event loop 
'driven' from Lua, while example 1 is a classic state machine 'driven' from C


-- 
Javier

Attachment: pgpFo0l2HUyZW.pgp
Description: PGP signature