Game
On the client side, in addition to the other functions, the so-called native functions are also available, i.e. functions that are originally used in the game scripts.
To use them, simply transform the names into the camelCase format. This involves capitalizing the first letter of each word after the initial word and removing underscores, resulting in a more readable and conventional naming style.
List of native functions: HappinessMP GitHub, GTAMods Wiki
Arguments and Results
The natives keep their original signatures, with a few conventions that are the same in every language:
- A vector argument is passed as three numbers (x, y, z).
- An argument the native only writes to (a pointer in the original signature, such as the
coordinates of
GetCharCoordinates) is not passed. It comes back as a result instead, after the native's own return value if it has one. - A native with several results returns them as multiple values in Lua and as an array in
JavaScript and Squirrel:
const [x, y, z] = Game.GetCharCoordinates(ped). - A native that takes a struct or array takes a table (Lua) or array (JavaScript, Squirrel) of integers and writes its results back into it.
- Argument count and types are checked. A wrong type or a missing argument raises a script error that names the native, so a typo does not silently call into the game with garbage.
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
Thread.Create(function()
while true do
Thread.Pause(0)
-- This code spawns a SultanRS when the player presses the F1 key.
if Game.IsGameKeyboardKeyJustPressed(59) then
local model = Game.GetHashKey("SULTANRS")
local x, y, z = Game.GetCharCoordinates(Game.GetPlayerChar(Game.GetPlayerId()))
local heading = Game.GetCharHeading(Game.GetPlayerChar(Game.GetPlayerId()))
Game.RequestModel(model)
while not Game.HasModelLoaded(model) do
Thread.Pause(0)
end
local car = Game.CreateCar(model, x, y, z, true)
Game.SetCarHeading(car, heading)
Game.SetCarOnGroundProperly(car)
Game.WarpCharIntoCar(Game.GetPlayerChar(Game.GetPlayerId()), car)
Game.MarkModelAsNoLongerNeeded(model)
Game.MarkCarAsNoLongerNeeded(car)
end
end
end)
Thread.Create(async () => {
while (true) {
await Thread.Pause(0);
// This code spawns a SultanRS when the player presses the F1 key.
if (Game.IsGameKeyboardKeyJustPressed(59)) {
const model = Game.GetHashKey("SULTANRS");
const [x, y, z] = Game.GetCharCoordinates(Game.GetPlayerChar(Game.GetPlayerId()));
const heading = Game.GetCharHeading(Game.GetPlayerChar(Game.GetPlayerId()));
Game.RequestModel(model);
while (!Game.HasModelLoaded(model)) {
await Thread.Pause(0);
}
const car = Game.CreateCar(model, x, y, z, true);
Game.SetCarHeading(car, heading);
Game.SetCarOnGroundProperly(car);
Game.WarpCharIntoCar(Game.GetPlayerChar(Game.GetPlayerId()), car);
Game.MarkModelAsNoLongerNeeded(model);
Game.MarkCarAsNoLongerNeeded(car);
}
}
});
Thread.Create(function() {
while (true) {
Thread.Pause(0);
// This code spawns a SultanRS when the player presses the F1 key.
if (Game.IsGameKeyboardKeyJustPressed(59)) {
local model = Game.GetHashKey("SULTANRS");
local pos = Game.GetCharCoordinates(Game.GetPlayerChar(Game.GetPlayerId()));
local heading = Game.GetCharHeading(Game.GetPlayerChar(Game.GetPlayerId()));
Game.RequestModel(model);
while (!Game.HasModelLoaded(model)) {
Thread.Pause(0);
}
local car = Game.CreateCar(model, pos[0], pos[1], pos[2], true);
Game.SetCarHeading(car, heading);
Game.SetCarOnGroundProperly(car);
Game.WarpCharIntoCar(Game.GetPlayerChar(Game.GetPlayerId()), car);
Game.MarkModelAsNoLongerNeeded(model);
Game.MarkCarAsNoLongerNeeded(car);
}
}
});