lua-users home
lua-l archive

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


On 19 July 2010 16:47, Nathaniel Lewis <linux.robotdude@gmail.com> wrote:
>
> I am currently writing a game engine for Linux that I intend to open
> source.  I realize that I need some sort of script to go along with a map in
> it to describe what will happen when.  I decided to turn to Lua as a script
> because its syntax is simple and not too difficult to learn.  But I need the
> interpreter to run as a separate thread than the main thread for reasons
> like this:
>
> while encounter_Living("DemoEncounter") > 0 do
>  -- Do something while the encounter is alive --
> end
> ui_Print("Encounter Killed");
>
> Currently all I have been able to do is have an update function called after
> every frame is rendered but and waiting would cause the game to lock up.  Is
> there any way to run the Lua interpreter as a separate thread from the main
> thread for Windows and Unix?
> Nathaniel Lewis

You probably want to wrap your update function in a coroutine.
Something like this: (untested)

update = coroutine.wrap(function ()
  while true do
    while encounter_Living("DemoEncounter") > 0 do
      -- Do something while the encounter is alive --
      coroutine.yield() -- this temporarily returns from the function,
resuming here when it's called again
    end)
    ui_Print("Encounter Killed");
  end
end)

Coroutines are a bit like threads, but instead of running in parallel
to the rest of your program, they manually yield to it until they are
resumed. For the details see

http://www.lua.org/manual/5.1/manual.html#2.11

    henk