From 3293b374a856f151634e31bd1f2012319b5ef1ae Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 13 Oct 2023 00:13:39 +0200 Subject: [PATCH] feat(lua): expose take_control_of (#2249) --- docs/lua/tables/entities.md | 18 +++++++++++++++++- src/lua/bindings/entities.cpp | 23 ++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/lua/tables/entities.md b/docs/lua/tables/entities.md index 9edb0a79..9c86c8dc 100644 --- a/docs/lua/tables/entities.md +++ b/docs/lua/tables/entities.md @@ -2,7 +2,7 @@ Table for manipulating GTA entities. -## Functions (3) +## Functions (4) ### `get_all_vehicles_as_handles()` @@ -34,4 +34,20 @@ table = entities.get_all_peds_as_handles() table = entities.get_all_objects_as_handles() ``` +### `take_control_of(entity, try_count)` + +Must be called from a script (script.run_in_fiber for example) + +- **Parameters:** + - `entity` (Entity): Script handle of the entity we are trying to take control of. + - `try_count` (integer): Optional. Number of time we'll try taking control of the entity. Default to 300. + +- **Returns:** + - `boolean`: Returns true if we successfully got control of the entity. + +**Example Usage:** +```lua +boolean = entities.take_control_of(entity, try_count) +``` + diff --git a/src/lua/bindings/entities.cpp b/src/lua/bindings/entities.cpp index 1a13ee54..9a686e4b 100644 --- a/src/lua/bindings/entities.cpp +++ b/src/lua/bindings/entities.cpp @@ -1,6 +1,7 @@ #pragma once #include "entities.hpp" +#include "util/entity.hpp" #include "util/pools.hpp" namespace lua::entities @@ -36,11 +37,31 @@ namespace lua::entities return big::pools::get_all_props_array(); } + static bool take_control_of(Entity entity) + { + return big::entity::take_control_of(entity); + } + + // Lua API: Function + // Table: entities + // Name: take_control_of + // Must be called from a script (script.run_in_fiber for example) + // Param: entity: Entity: Script handle of the entity we are trying to take control of. + // Param: try_count: integer: Optional. Number of time we'll try taking control of the entity. Default to 300. + // Returns: boolean: Returns true if we successfully got control of the entity. + static bool take_control_of_try_count(Entity entity, int try_count) + { + return big::entity::take_control_of(entity, try_count); + } + void bind(sol::state& state) { - auto ns = state["entities"].get_or_create(); + auto ns = state["entities"].get_or_create(); + ns["get_all_vehicles_as_handles"] = get_all_vehicles_as_handles; ns["get_all_peds_as_handles"] = get_all_peds_as_handles; ns["get_all_objects_as_handles"] = get_all_objects_as_handles; + + ns["take_control_of"] = sol::overload(take_control_of, take_control_of_try_count); } } \ No newline at end of file