lua-users home
lua-l archive

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


On Mon, Jul 19, 2010 at 8:33 PM, Nathaniel Lewis
<linux.robotdude@gmail.com> wrote:
> So I understand (I'm fairly new to lua), when you yield a coroutine, it
> will actually return from the lua_pcall inside C++?  Do I resume it from
> C++ with lua_pcall again or is there a special resume command I missed
> in the manual?
>                                Nathaniel
>
> On Mon, 2010-07-19 at 17:21 -0400, Henk Boom wrote:
>> 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
>
>
>

coroutine.resume may be called from Lua if you created it the
coroutine with coroutine.create. With coroutine.wrap you just call the
returned function to resume the coroutine. (update in Henk's example)

See http://www.lua.org/pil/index.html#9

Nick