From 540ff21b054e2868e5ad5166c5b78ed31940dab2 Mon Sep 17 00:00:00 2001 From: Alice <105157805+Alice2333g@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:20:22 +0800 Subject: [PATCH] add lua api: script.execute_as_script (#2824) Co-authored-by: xiaoxiao921 --- docs/lua/tables/script.md | 13 ++++++++++++- src/lua/bindings/script.cpp | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/lua/tables/script.md b/docs/lua/tables/script.md index e43b2697..a922cc0c 100644 --- a/docs/lua/tables/script.md +++ b/docs/lua/tables/script.md @@ -2,7 +2,7 @@ Table containing helper functions related to gta scripts. -## Functions (2) +## Functions (3) ### `register_looped(name, func)` @@ -75,4 +75,15 @@ end) script.run_in_fiber(func) ``` +### `execute_as_script(script_name, func)` + +- **Parameters:** + - `script_name` (string): target script thread. + - `func` (function): function that will be executed once in the script thread. + +**Example Usage:** +```lua +script.execute_as_script(script_name, func) +``` + diff --git a/src/lua/bindings/script.cpp b/src/lua/bindings/script.cpp index ecc01e78..a47108d7 100644 --- a/src/lua/bindings/script.cpp +++ b/src/lua/bindings/script.cpp @@ -3,6 +3,7 @@ #include "lua/lua_manager.hpp" #include "script_mgr.hpp" +#include "gta_util.hpp" namespace lua::script { @@ -151,15 +152,26 @@ namespace lua::script module->m_registered_scripts.push_back(std::move(lua_script)); } + // Lua API: function + // Table: script + // Name: execute_as_script + // Param: script_name: string: target script thread. + // Param: func: function: function that will be executed once in the script thread. + static void execute_as_script(const std::string& script_name, sol::protected_function func) + { + big::gta_util::execute_as_script(rage::joaat(script_name), func); + } + void bind(sol::state& state) { auto ns = state["script"].get_or_create(); ns["register_looped"] = register_looped; ns["run_in_fiber"] = run_in_fiber; + ns["execute_as_script"] = execute_as_script; auto usertype = state.new_usertype("script_util"); usertype["yield"] = sol::yielding(&script_util::yield); usertype["sleep"] = sol::yielding(&script_util::sleep); } -} \ No newline at end of file +}