Using lua 5, andI just can''t figure this out. My goal is to eventaully havea scripting system where each script has a few functions and all scripts are being proccess simultaneously (well close to
).
Anyway while trying to start I wrote the following sample program and script, however it doesn''t work as I expected. Anyone familar with lua please correct me on both my program and comments if they are wrong (they must be)
lua_State *Thread;
int luaWait(lua_State *L)
{
// yield to test threads
lua_yield(Thread, 0);
return 1;
}
void main()
{
// open lua and std libs
lua_State *Lua = lua_open();
lua_baselibopen(Lua);
lua_iolibopen(Lua);
lua_strlibopen(Lua);
lua_mathlibopen(Lua);
// register dumb function
lua_register(Lua, "Wait", luaWait);
// create a thread
Thread = lua_newthread(Lua);
// parse file in thread
lua_dofile(Thread, "Test.txt");
// call Main() function
lua_getglobal(Thread, "Main");
lua_call(Thread, 0,0);
lua_pop(Thread, 1);
// resume yielded thread
lua_resume(Thread, 0); // should resume where yield left off?
}
Script looks like this
function Main()
print("In Main")
Wait()
print("Out of Wait")
end
I would expect it to output InMain, then Out of dumb. However the program prints InMain and then terminates. It seems my resume does nothing?
I''ve looked at the lua5 documentation and it is next to worthless for making any sense out of this. I just wanna know the right way to run/stop/run script functions from C.
I''d greatly apprieciate any help at all.
Thanks