From 5c36f59b4f25e4d5bc8e58baa557bca0b135aec1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 22 Jan 2024 18:38:46 +0300 Subject: [PATCH] added time.uptime() and sleep(timesec) --- res/scripts/stdlib.lua | 8 ++++++++ src/logic/scripting/api_lua.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 35927573..c6e7814c 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -49,3 +49,11 @@ function load_script(path, nocache) end return result end + +function sleep(timesec) + local start = time.uptime() + while time.uptime() - start < timesec do + coroutine.yield() + end +end + diff --git a/src/logic/scripting/api_lua.cpp b/src/logic/scripting/api_lua.cpp index b05595b8..74a799c4 100644 --- a/src/logic/scripting/api_lua.cpp +++ b/src/logic/scripting/api_lua.cpp @@ -13,6 +13,7 @@ #include "../../voxels/voxel.h" #include "../../lighting/Lighting.h" #include "../../logic/BlocksController.h" +#include "../../window/Window.h" #include "../../engine.h" inline int lua_pushivec3(lua_State* L, int x, int y, int z) { @@ -28,6 +29,17 @@ inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfunc lua_setglobal(L, name); } +/* == time library == */ +static int l_time_uptime(lua_State* L) { + lua_pushnumber(L, Window::time()); + return 1; +} + +static const luaL_Reg timelib [] = { + {"uptime", l_time_uptime}, + {NULL, NULL} +}; + /* == pack library == */ static int l_pack_get_folder(lua_State* L) { std::string packName = lua_tostring(L, 1); @@ -294,6 +306,7 @@ void apilua::create_funcs(lua_State* L) { luaL_openlib(L, "pack", packlib, 0); luaL_openlib(L, "world", worldlib, 0); luaL_openlib(L, "player", playerlib, 0); + luaL_openlib(L, "time", timelib, 0); lua_addfunc(L, l_block_index, "block_index"); lua_addfunc(L, l_block_name, "block_name");