Cache

The cache is used to store frequent information that scripts will use, this reduces the load of scripts overall as the core library is the only resource gathering these variables.

Usage

The cache is simple to access, once you've followed the Getting Started steps, you will be able to access the following values:

Update Events

The cache emits update events once a cache value has been updated, this is useful to ensure your script's values are up-to-date.

The event is structured like this

dirk_lib:cache:KEY

The KEY is the cache value you'd like to watch, in the example below we've used the ped key.

local playerPedId = lib.cache.ped

lib.onCache("ped", function(newValue, oldValue)
    playerPedId = newValue
end)

-- OR

AddEventHandler("dirk_lib:cache:ped", function(newValue, oldValue)
    playerPedId = newValue
end)

Starting Point

If you'd like a quick list of cache values for your script, use the following snippet.

playerPedId, serverId, curVehicle, curVehicleDriver, curWeapon = lib.cache.ped, lib.cache.serverId, lib.cache.vehicle, lib.cache.driver, lib.cache.weapon

lib.onCache("ped", function(newValue)
    playerPedId = newValue
end)

lib.onCache("serverId", function(newValue)
    serverId = newValue
end)

lib.onCache("vehicle", function(newValue)
    curVehicle = newValue
end)

lib.onCache("driver", function(newValue)
    curVehicleDriver = newValue
end)

lib.onCache("weapon", function(newValue)
    curWeapon = newValue
end)

lib.cache.playerId

This contains the player's local id

local playerId = lib.cache.playerId

lib.cache.serverId

This contains the player's server id or "source"

local serverId = lib.cache.serverId

lib.cache.ped

This contains the player's ped ID

local ped = lib.cache.ped

lib.cache.vehicle

This contains the current vehicle the player is within, it returns false if they are not within a vehicle

local curVehicle = lib.cache.vehicle

lib.cache.seat

This contains the current seat the player is in within a vehicle, it returns false if they are not in any vehicle

local curSeat = lib.cache.vehicle
local isDriver = curSeat == -1 -- alternativly, you can use lib.cache.driver

lib.cache.driver

This contains if the player is the driver of their vehicle

local isDriver = lib.cache.driver

lib.cache.weapon

This contains the model hash of the weapon the player is using, it returns false if they are not holding a weapon

local curWeapon = lib.cache.weapon

Last updated