lua-users home
lua-l archive

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


Hi Derek,
   I'd just add my agreement that the comments already about "event-driven"
and "state machine" will probably be easier to implement for you the
programmer, but perhaps at a bit of expense for your script coders.  The
trade-off is yours to decide, and the severity of it will likely depend on
exactly what type of actions you're attempting to "script".

   For instance, I'll make up a script that might work if you really did
have a multitasking kernel (for ideas only, please don't try checking this
for syntax, this is free-form coding at it's worst!):

  acritter:DoGreeting()

  function Critter:DoGreeting()
    RunAnimation(waveHands)
    WaitForAnimationToComplete()
    RunAnimation(speakHello)
    PlaySound("hello")
    WaitForSoundToComplete()
    WaitForAnimationToComplete()
    RunAnimation(jumpAround)
    WaitForAnimationToComplete()
  end

  Anything with "Wait" in the function name implies a "sleep" of this
process until some condition occurs at which point the script resumes on
the following line.  To do that without changing the kernel, you'd maybe
set it up something like (again, I haven't actually tried this code, so
beware):

  acritter.callbackfunc = DoGreeting

  function Critter:DoState(thisstate, nextstate, func, parm)
    if (self.state = thisstate) then
      if (func(parm)) then
        self.state = nextstate
      end
    end
    if (not self.state) then
      self.callbackfunc = nil
    end
  end

  function Critter:DoGreeting()
    DoState(1, 2, RunAnimation, waveHands)
    DoState(2, 3, WaitForAnimationToComplete, nil)
    DoState(3, 4, RunAnimation, speakHello)
    DoState(4, 5, PlaySound, "hello")
    DoState(5, 6, WaitForSoundToComplete, nil)
    DoState(7, 7, WaitForAnimationToComplete, nil)
    DoState(7, 8, RunAnimation, jumpAround)
    DoState(8, nil, WaitForAnimationToComplete, nil)
  end

  That's not TOO bad, and there are probably more elequent ways of coding
it (check the list archive for "switch" statements perhaps, or track your
state directly with a function reference, etc), but it IS less clear than
the first method.  Upside is that it would work right now, without changing
Lua.  Hope that helps.

   Cheers,

   Dave