Skip to main content
gamedev.net
🔒 Locked

Lua: Problems with Lua and threads

Started by Logick 0 replies 650+ views
Original Post
Logick
Logick
I am currently trying to incorporate lua into my engine. everything is working fine, except for my wait() function. My wait function takes in a float, and yields the thread using lua_yield and resumes using lua_resume. it works fine in the main lua function but if i try to run other scripts in the main function, the thread hangs. Any help is appreciated: The main script

function Main()
    wait(1)  -- waits one second, works fine
    runScript("test.lua") -- runs a script
end

test.lua

print("test1\n")
wait(2)
print("test2\n")  -- i never see this print

this is how I'm opening the file

m_thread = lua_newthread(m_luaVM);
luaL_dofile(m_thread, "main.lua");
lua_getglobal(m_thread, "Main");  //place the main function in the thread
lua_resume(m_thread, 0);		 // start the script

This is my callback for 'runScript'

void runscript()
{
     return luaL_dofile(m_thread, m_FileName)
}

My callback for 'wait'

int waitCB (lua_State *VM)
{
     double floatValue = (double )lua_tonumber(VM, -1);
     lua_pushnumber( VM, 0 );
     //....
     return lua_yield(m_thread, 0);
}

How i resume

lua_resume(m_thread, 0);

Kylotan
Kylotan
You're mixing the terms thread and coroutine. Coroutines are not threads though - in fact you usually run all your coroutines in one thread.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.