lua-users home
lua-l archive

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


On Saturday, March 16, 2002, at 03:53  PM, Gabriel Peyré wrote:
Thanks a lot for all these answers !
I've just read the documentation of lua-w4, and I found no explaination
about the couroutines functions, wich seems to be:
--
LUA_API void lua_cobegin (lua_State *L, int nargs);
LUA_API int lua_yield (lua_State *L, int nresults);
LUA_API int lua_resume (lua_State *L, lua_State *co);
--

I've been playing around with this and the following approach seems to work:

lua_getglobal( L, "main" );
lua_cobegin( L, 0 );
lua_resume( L, 0, 0 );
... lua code runs until a "yield" is issued.

at this point you continue with the rest of your program. The next time you'd like the Lua code to execute just call lua_resume again. I'm not clear on the argument handling stuff that's why it's 0 for now. I also don't need it for my purposes. Another problem I face is that lua_resume will throw a fit is the Lua script has finished ( or wasn't actually yielded ). There seems to be no public function in the API to determine if a script has yielded or stopped naturally. I ended up putting detection code in lua_resume and returning a status code of my own to know when my scripts had terminated. I'm pretty sure there's a much better way.

Hope that helps a little,
Guy