This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/docs/lua/tables/script.md
Arthur 09b91ca6d7
Some checks are pending
Nightly Build / Build Nightly (push) Blocked by required conditions
Nightly Build / Recreate Release (push) Blocked by required conditions
Nightly Build / Check Recent Commit (push) Successful in 20s
Add Return Value Support for Script Functions (#3479)
* Add Return Value Support for Script Functions
* Removed address-of operator on instruction_pointer parameter, as sol is pass-by-value.
* Fixed reset_session_data & start_creator_script
* Added support for Vector3 and updated casting for bool type for proper conversion to Lua boolean
* Updated documentation for scr_function
* Added get_int method and updated param names for script functions
* Fix #3497 graceful landing not saved.
* Added a check in view_lsc to see if the vehicle can accept clan logos first.
* Fixed vehicle clan logo SP bypass not working properly.
* Fixed COPY VEHICLE not giving persist_car_service::spawn_vehicle_json the target's ped so it can copy their clan logo and not ours.
Fixed spawn_vehicle_json calling add_clan_logo_to_vehicle with our logo and not the ped parameter's logo.
* Added Clone Player Car.
* Fixed has_clan_logo check in view_lsc being given the wrong parameter.

---------

Co-authored-by: gir489 <100792176+gir489returns@users.noreply.github.com>
2024-08-06 14:46:48 +02:00

121 lines
3.7 KiB
Markdown

# Table: script
Table containing helper functions related to gta scripts.
## Functions (7)
### `register_looped(name, func)`
Registers a function that will be looped as a gta script.
- **Parameters:**
- `name` (string): name of your new looped script
- `func` (function): function that will be executed in a forever loop.
**Example Usage:**
```lua
script.register_looped("nameOfMyLoopedScript", function (script)
-- sleep until next game frame
script:yield()
local ModelHash = joaat("adder")
if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
script:yield()
end
local myPed = PLAYER.PLAYER_PED_ID()
local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
-- Spawns a networked vehicle on your current coords
local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
-- removes model from game memory as we no longer need it
STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
-- sleep for 2s
script:sleep(2000)
ENTITY.DELETE_ENTITY(spawnedVehicle)
end)
```
### `run_in_fiber(func)`
Executes a function once inside the fiber pool, you can call natives inside it and yield or sleep.
- **Parameters:**
- `func` (function): function that will be executed once in the fiber pool.
**Example Usage:**
```lua
script.run_in_fiber(function (script)
-- sleep until next game frame
script:yield()
local ModelHash = joaat("adder")
if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
script:yield()
end
local myPed = PLAYER.PLAYER_PED_ID()
local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
-- Spawns a networked vehicle on your current coords
local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
-- removes model from game memory as we no longer need it
STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
-- sleep for 2s
script:sleep(2000)
ENTITY.DELETE_ENTITY(spawnedVehicle)
end)
```
### `is_active(script_name)`
Returns true if the specified script is currently active/running.
- **Parameters:**
- `script_name` (string): The name of the script.
**Example Usage:**
```lua
local is_freemode_active = script.is_active("freemode")
```
### `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)
```
### `add_patch(script_name, name, pattern, offset, _patch)`
Adds a patch for the specified script.
- **Parameters:**
- `script_name` (string): The name of the script.
- `name` (string): The name of the patch.
- `pattern` (string): Pattern to scan for within the script.
- `offset` (integer): The position within the pattern.
- `_patch` (table): The bytes to be written into the script's bytecode.
**Example Usage:**
```lua
script.add_patch("fm_content_xmas_truck", "Flickering Fix", "56 ? ? 4F ? ? 40 ? 5D ? ? ? 74", 0, {0x2B, 0x00, 0x00})
```
### `start_launcher_script(script_name)`
Tries to start a launcher script. Needs to be called in the fiber pool or a loop.
- **Parameters:**
- `name` (string): The name of the script.
**Example Usage:**
```lua
script.run_in_fiber(function()
script.start_launcher_script("am_hunt_the_beast")
end)
```