lua-users home
lua-l archive

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


How about something like this 

-- ********** CLIP here **************
THREAD_SCRIPTS = {}

local function YIELD (co) 
   coroutine.yield()
   local ts = THREAD_SCRIPTS[co]
   local f = loadstring(ts) - this could be loadfile(ts) if 'ts' were a file name
   if (f) then f() end
end 

function co_main(co)
      local function factorial(n)
         if n <= 0 then return 1 
      else 
         YIELD(co) 
         print("current factorial : ", n)
         return n*factorial(n-1)
      end
   end

   factorial(25)
end

local co = coroutine.create(co_main)
coroutine.resume(co, co)

THREAD_SCRIPTS[co] = 
[[
   for i = 1, 3 do 
      print('side script value : '..tostring(i*i)); 
      coroutine.yield()
   end
]]

for j = 0, 50 do
   coroutine.resume(co)
end

-- ************* CLIP here ******************

-----Original Message-----
From: Matthew Harmon [mailto:matt@matthewharmon.com]
Sent: Thursday, December 04, 2003 7:02 PM
To: Lua list
Subject: RE: yielding troubles

Yes... sorry I wasn't more clear.  I have a main thread that "spawns" the
other threads, which need to yield.  I can yield fine in these threads and
everything works great until I try to load and execute another file from
within one of these threads. (Basically a lua_dofile() ).  I can't seem to
yield from one of these "subfiles".

For my application I'd prefer not to execute the "subfile" in a separate
thread, but rather execute it in the caller's thread.  Thus, the calling
script won't continue while the "subfile" is being processed.


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Bilyk, Alex
Sent: Thursday, December 04, 2003 5:17 PM
To: Lua list
Subject: RE: yielding troubles

It is not quite clear from your example what Lua threads are involved and
the relationship between them. You cannot yield from any Lua threads back to
C (for instance the main thread cannot yield). You can only yield from one
Lua thread to another Lua thread.

AB

-----Original Message-----
From: Matthew Harmon [mailto:matt@matthewharmon.com]
Sent: Thursday, December 04, 2003 12:16 PM
To: Lua
Subject: yielding troubles

Well... I'm a Lua addict.  Our game engine is more and more becoming just a
set of functions that are called from Lua!

Anyway, I want to have one Lua program call another and execute in the same
thread. For all practical purposes this is essentially like #include-ing
code from another file.  In the child script, I'd like to be able to issue a
yield, but it doesn't seem to be working.  This should give the idea in
pseudo code:

  -- MainScript.lua
  RunScriptInCurrentThread("LoadKeyConfiguration.lua");
  RunScriptInCurrentThread("DoStartupStuff.lua");
  RunScriptInCurrentThread("OpeningSlideshow.lua");
  PrintMessage("Well, that slideshow lasted roughly 4 seconds!");

  -- OpeningSlideshow.lua
  ShowSlide("xxx")
  "yield" for 2 seconds
  ShowSlide("xxx")
  "yield" for 2 more seconds

Is this because the yield is essentially nested two-deep in C calls?  Any
help, thoughts, suggestions would be most appreciated.