Lua: refactor script api, more doc, add button for Open Lua Scripts Folder (#1588)

* lua manager: pass down the scripts folder to the instance instead of hard coding calls to the file manager everywhere
* lua: add open lua scripts folder button
* lua api: change script binding so that user cannot by mistake try to sleep or yield in a non script context
This commit is contained in:
Quentin
2023-07-03 13:01:12 +02:00
committed by GitHub
parent 71960eddbb
commit ed18f7e70d
8 changed files with 186 additions and 58 deletions

View File

@ -0,0 +1,28 @@
# Class: script_util
Class for gta script utils, the instance is usually given to you.
## Functions (2)
### `yield()`
Yield execution.
**Exemple Usage:**
```lua
script_util:yield()
```
### `sleep(ms)`
Sleep for the given amount of time, time is in milliseconds.
- **Parameters:**
- `ms` (integer): The amount of time in milliseconds that we will sleep for.
**Exemple Usage:**
```lua
script_util:sleep(ms)
```

View File

@ -2,11 +2,34 @@
Table containing helper functions related to gta scripts.
## Functions (4)
## Functions (2)
### `register_looped(name, func)`
Registers a function that will be looped as a gta script.
**Exemple 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)
```
- **Parameters:**
- `name` (string): name of your new looped script
@ -19,35 +42,37 @@ script.register_looped(name, func)
### `run_in_fiber(func)`
Executes a function inside the fiber pool, you can call natives inside it.
Executes a function once inside the fiber pool, you can call natives inside it and yield or sleep.
**Exemple 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)
```
- **Parameters:**
- `func` (function): function that will be executed once in the fiber pool, you can call natives inside it.
- `func` (function): function that will be executed once in the fiber pool.
**Exemple Usage:**
```lua
script.run_in_fiber(func)
```
### `yield()`
Yield execution.
**Exemple Usage:**
```lua
script.yield()
```
### `sleep(ms)`
Sleep for the given amount of time, time is in milliseconds.
- **Parameters:**
- `ms` (integer): The amount of time in milliseconds that we will sleep for.
**Exemple Usage:**
```lua
script.sleep(ms)
```