Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Add createThread and wait function
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeee committed Feb 1, 2021
1 parent deb2de0 commit 7ab5ac8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/CLuaDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ void lua_pushmvalue(lua_State* L, const alt::MValueConst &mValue)
case alt::IMValue::Type::BASE_OBJECT:
lua_pushbaseobject(L, mValue.As<alt::IMValueBaseObject>()->Value().Get());
break;
case alt::IMValue::Type::VECTOR2:
lua_pushvector2(L, mValue.As<alt::IMValueVector2>()->Value());
break;
case alt::IMValue::Type::VECTOR3:
lua_pushvector3(L, mValue.As<alt::IMValueVector3>()->Value());
break;
case alt::IMValue::Type::LIST:
{
auto list = mValue.As<alt::IMValueList>();
Expand Down
2 changes: 1 addition & 1 deletion src/CLuaScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CLuaScriptRuntime : public alt::IScriptRuntime
~CLuaScriptRuntime() { };

private:
const semver::version version{ 1, 1, 1, alt::ICore::SDK_VERSION, semver::branch::dev };
const semver::version version{ 1, 1, 2, alt::ICore::SDK_VERSION, semver::branch::dev };
#ifdef ALT_SERVER_API
alt::config::Node::Dict serverConfigDict;
#endif
Expand Down
59 changes: 59 additions & 0 deletions src/Defs/CLuaMiscScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,68 @@

void CLuaMiscScripts::Init(lua_State* L)
{
thread(L);
inspect(L);
}

void CLuaMiscScripts::thread(lua_State* L)
{
const char* const thread = R"~LUA~(
local threads = {}
function createThread(callback)
assert(type(callback) == "function", "callback argument is not a function")
threads[coroutine.create(callback)] = {
lastTime = alt.getNetTime(),
waitFor = 0,
args = {}
}
end
function wait(arg, ...)
assert((type(arg) == "number" or type(arg) == "function"), "only number and function allowed")
coroutine.yield(arg, ...)
end
alt.on("tick", function()
for callback, data in pairs(threads) do
local condition = nil
if type(data.waitFor) == "number" then
condition = (alt.getNetTime() > (data.lastTime + data.waitFor))
elseif type(data.waitFor) == "function" then
condition = data.waitFor(unpack(data.args))
end
if condition then
if coroutine.status(callback) == "suspended" then
local args = {coroutine.resume(callback)}
if args[1] then
data.lastTime = alt.getNetTime()
data.waitFor = args[2]
data.args = {unpack(args, 3)}
else
error(args[2], 2)
end
end
end
if coroutine.status(callback) == "dead" then
threads[callback] = nil
end
end
end)
)~LUA~";

luaL_loadbuffer(L, thread, strlen(thread), NULL);
lua_call(L, 0, LUA_MULTRET);
//lua_setglobal(L, "inspect");
}

void CLuaMiscScripts::inspect(lua_State* L)
{
const char* const inspect = R"~LUA~(
Expand Down
1 change: 1 addition & 0 deletions src/Defs/CLuaMiscScripts.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class CLuaMiscScripts
static void Init(lua_State* L);

private:
static void thread(lua_State* L);
static void inspect(lua_State* L);
};

0 comments on commit 7ab5ac8

Please sign in to comment.