From 1f2b25eb76da0f74dd3c7cf12ddc3eafe7d0cdad Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Tue, 8 Oct 2024 13:18:44 +0200 Subject: [PATCH] Call timers on separate coroutine threads --- src/controllers/plugins/LuaAPI.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controllers/plugins/LuaAPI.cpp b/src/controllers/plugins/LuaAPI.cpp index d2603750b..363481319 100644 --- a/src/controllers/plugins/LuaAPI.cpp +++ b/src/controllers/plugins/LuaAPI.cpp @@ -122,17 +122,19 @@ int c2_later(sol::this_state L, sol::protected_function callback, int time) QObject::connect(timer, &QTimer::timeout, [pl, name, timer, callback]() { timer->deleteLater(); pl->removeTimeout(timer); - sol::state_view lua(callback.lua_state()); - sol::protected_function_result res = callback(); + sol::state_view main(callback.lua_state()); + sol::thread thread = sol::thread::create(main); + sol::protected_function cb(thread.state(), callback); + sol::protected_function_result res = cb(); if (res.return_count() != 0) { - stackDump(lua.lua_state(), + stackDump(thread.lua_state(), pl->id + ": timer returned a value, this shouldn't happen " "and is probably a plugin bug"); } - lua.registry()[name.toStdString()] = sol::nil; + main.registry()[name.toStdString()] = sol::nil; }); timer->start();