Przejdź do treści
Natives: talking to the game

Natives: talking to the game

What FiveM natives are, how to find them on docs.fivem.net, read their signatures, use hashes and joaat, handle entities, and a cheat sheet of the natives you use most.

Ta dokumentacja jest na razie po angielsku.

Last updated

Natives are the functions the game engine exposes: GetEntityCoords, SetPedIntoVehicle, DrawMarker and thousands more. FiveM adds its own CFX natives on top (GetPlayerIdentifierByType, SetResourceKvp, state bags…). Almost everything your scripts do in the game world goes through a native.

Plays from youtube-nocookie.com after you click.

Finding natives

The reference is at docs.fivem.net/natives. It lists every known native, grouped by namespace (PLAYER, PED, VEHICLE, ENTITY, OBJECT, HUD, GRAPHICS, STREAMING, TASK, CFX…), with parameters, return values and often community notes. The data comes from the open citizenfx/natives repository.

Tips for searching:

  • Guess the verb and the thing: SetVehicle..., GetPed..., IsEntity..., Create..., Delete....
  • Filter by client or server on the page. Some natives exist only on the server (GetPlayerIdentifierByType, DropPlayer), most game natives only on the client.
  • Natives without a proper name show as _0x1234ABCD or N_0x.... They work, they just haven’t been named yet.
  • Read the examples and notes at the bottom of a native’s page. That’s where people document the quirks.

With the VS Code setup (Lua Language Server plus the FiveM native declarations), you get autocomplete and parameter hints for all of them.

Reading a signature

The docs show natives in C style:

c
Vehicle CREATE_VEHICLE(Hash modelHash, float x, float y, float z, float heading, BOOL isNetwork, BOOL netMissionEntity);

In Lua you call the PascalCase name:

Lua
local veh = CreateVehicle(modelHash, x, y, z, heading, true, false)
  • Hash is a number (see below), but most natives also accept a model name string and hash it.
  • Vehicle, Ped, Object, Entity are entity handles: numbers valid on this client only.
  • BOOL is true / false in Lua.
  • Parameters marked with * are pointers (outputs). In Lua they come back as extra return values: local found, groundZ = GetGroundZFor_3dCoord(x, y, z, false).
  • Vector3 returns are vector3 values in Lua: pos.x, pos.y, pos.z, and you can subtract them and use #(a - b) for distance.

Hashes and joaat

GTA identifies models, weapons, animations sets and much more by a 32 bit hash of the name, computed with Jenkins one-at-a-time (joaat).

Lua
local h1 = GetHashKey('adder')   -- native, works everywhere
local h2 = joaat('adder')        -- CfxLua helper, same result
local h3 = `adder`               -- CfxLua backtick syntax, hashed at compile time
print(h1 == h2, h2 == h3)        -- true true

Hashes can be shown as signed or unsigned numbers, or as hex. adder is the same model whether you see it as a negative or positive number. Use the joaat Hash Calculator to convert a name to all three forms, and the Model & Hash Browser to look up vehicles, peds and weapons with pictures.

Tip

Hashes are case insensitive: joaat('ADDER') == joaat('adder'). That’s why spawn names work in any case.

Entities, handles and network IDs

  • An entity handle is a local number. Handle 123 on your client is not the same entity on another client or on the server.
  • To tell another machine about an entity, send its network ID: NetworkGetNetworkIdFromEntity(entity) on one side, NetworkGetEntityFromNetworkId(netId) on the other (use NetToVeh / NetToPed on the client if you like).
  • Check it still exists before using it: DoesEntityExist(entity).
  • With OneSync, the server can read and create entities too: GetEntityCoords(GetPlayerPed(src)), CreateVehicleServerSetter(...). See State bags and OneSync.

Loading things before using them

Models, animation dictionaries, particle assets, texture dictionaries and scaleforms must be requested and loaded before use:

Lua
local function loadModel(model)
    local hash = type(model) == 'number' and model or joaat(model)
    if not IsModelInCdimage(hash) then return nil end
    RequestModel(hash)
    local timeout = GetGameTimer() + 5000
    while not HasModelLoaded(hash) do
        if GetGameTimer() > timeout then return nil end
        Wait(0)
    end
    return hash
end

local function loadAnimDict(dict)
    RequestAnimDict(dict)
    while not HasAnimDictLoaded(dict) do Wait(0) end
end

Release them when done (SetModelAsNoLongerNeeded, RemoveAnimDict). ox_lib has ready made helpers: lib.requestModel, lib.requestAnimDict, lib.requestNamedPtfxAsset.

A cheat sheet of common natives

Player and ped

Native Use
PlayerPedId() Your character’s ped (client). Also available as cache.ped with ox_lib.
PlayerId() Your local player index (client).
GetPlayerServerId(PlayerId()) Your server ID (client).
GetPlayerPed(serverId) A player’s ped on the server (OneSync).
GetEntityCoords(ped) / GetEntityHeading(ped) Position and heading.
SetEntityCoords(ped, x, y, z, false, false, false, false) Teleport.
GetEntityHealth(ped) / SetEntityHealth(ped, 200) Health (peds: 100 dead, 200 full for the freemode ped).
IsPedInAnyVehicle(ped, false) / GetVehiclePedIsIn(ped, false) Vehicle checks.
GiveWeaponToPed(ped, joaat('weapon_pistol'), 50, false, true) Give a weapon.
TaskPlayAnim(ped, dict, name, 8.0, -8.0, -1, flags, 0, false, false, false) Play an animation. Build the flags with the Animation Flags Calculator.

Vehicles

Native Use
CreateVehicle(model, x, y, z, heading, true, false) Spawn (client).
SetPedIntoVehicle(ped, veh, -1) Seat as driver.
SetVehicleNumberPlateText(veh, 'FIVEMAD') Plate.
SetVehicleFixed(veh) / SetVehicleEngineHealth(veh, 1000.0) Repair.
SetVehicleModKit(veh, 0) then SetVehicleMod(veh, modType, index, false) Tuning.
TaskVehicleDriveToCoord(...) AI driving. The driving style flag comes from the Driving Style Calculator.
DeleteEntity(veh) Remove.

World and UI

Native Use
AddBlipForCoord(x, y, z) + SetBlipSprite, SetBlipColour Map blips. Browse sprites and colours in the Blip, Marker & Checkpoint Browser.
DrawMarker(type, x, y, z, ...) 3D markers, must be called every frame.
IsControlJustPressed(0, 38) Key input. Control 38 is E. Find indexes in the Controls Reference.
BeginTextCommandDisplayHelp('STRING') Help text top left. Colour codes like ~g~ preview in Draw Text Colors.
PlaySoundFrontend(-1, name, set, true) UI sounds. Find names in the Sound Browser.
UseParticleFxAsset(asset) + StartParticleFxNonLoopedAtCoord(...) Particles. Names and dictionaries in the Particle Effects List and with video previews on /particles.
CreateObject(joaat(model), x, y, z, true, false, false) Props. Search models with pictures in the Objects / Props List.

CFX natives (FiveM specific)

Native Use
GetPlayerIdentifierByType(src, 'license') A player’s identifier (server).
GetPlayers() All player server IDs (server).
DropPlayer(src, reason) Kick (server).
GetConvar('name', 'default') Read a convar.
SetResourceKvp / GetResourceKvpString Small persistent storage.
GetCurrentResourceName() Your resource’s name.
PerformHttpRequest(url, cb, method, data, headers) HTTP from the server.

Coordinates

Most natives take world coordinates. Get them in game by printing GetEntityCoords(PlayerPedId()), or pick them on the Interactive Map, which shows coordinates as you move over the map.

Common native mistakes

  • Forgetting to load a model: the entity doesn’t appear or the game returns 0.
  • Calling a per frame native once: DrawMarker, DrawText, DisableControlAction must run every frame in a loop with Wait(0).
  • Using a client native on the server (or the other way round): the docs page tells you which side it’s for.
  • Passing an integer where a float is expected in C# or JS is an error, in Lua it’s usually fine, but write 1.0 for clarity.
  • Old names: natives get renamed as people learn what they do. If a tutorial’s native doesn’t exist, search the hash on the natives page.

Next: Threads and performance.