Esc to clear, Enter to open the best match, click an export to open it in its page
Vehicles

Vehicle data & handling API

Attach your own data to a car, react to it on every client, and change how the car drives and how its dial reads. For when to use data, see Vehicle data.

Vehicle data

SetVehicleData / GetVehicleData / ClearVehicleDataSERVERCLIENTWAITS#

Attach data to a vehicle by name: saved in the car's row and replicated to every client with the car in scope. Set and Clear are server only; Get works on both sides, and with no key returns a copy of everything the car carries.

ParamTypeDescription
vehicleIdnumber|tableA vehicle reference ({ entity = veh } for a handle)
keystring1–40 characters of [A-Za-z0-9_]
valueanyAnything JSON-encodable, false included; nil removes it
Returns: writes boolean, string|nil; reads the value (a stored false reads as false) or a copy of the block
A key belongs to the resource that wrote it first. Writing or clearing another resource's key is refused, naming the owner. Each resource gets 32 keys per car.
SetVehicleData(vehicleId, values)SERVERWAITS#

A table in place of the key sets several keys in one replication and one database write, for installing a kit. Each key still fires its own whip_core:vehicle:dataChanged after the commit.

ParamTypeDescription
vehicleIdnumber|tableA vehicle reference
valuestable{ [key] = value }, keys as for one
Returns: number written, or false, reason when nothing was written
A batch can't remove keys (use ClearVehicleData). Keys past your 32 are skipped and logged; a batch that would take the block past 8 KB, or touches another resource's key, is refused whole.
ClearAllVehicleData(vehicleId)SERVERWAITS#

Remove every key your resource put on a car: uninstalling a kit. Other resources' keys stay. Fires one dataChanged per key removed. Stats are untouched (see ClearVehicleStat).

ParamTypeDescription
vehicleIdnumber|tableA vehicle reference
Returns: boolean, string|nil: why not is no such vehicle, or the write failed
OnVehicleDataChanged(key, callback, id, opts)SERVER#

Be called when one data key changes, on any vehicle: the server half of the client export, and cheaper than handling every dataChanged broadcast. Dropped when your resource stops.

ParamTypeDescription
keystringThe data key to watch
callbackfunction(vehicleId, value, cleared, old), or one table with table = true
idstring?Only for two listeners on one key; the same id replaces
optstable?{ table = true, now = true }; may go third, in place of id
Returns: boolean registered
Whip.register(function()
    Whip.core:OnVehicleDataChanged('kit', function(change)
        if change.cleared then --[[ the kit came off; change.old is what it was ]] end
    end, { table = true, now = true })
end)
Use table = true: { vehicleId, key, value, old, cleared, resource } (plus entity on the client). Positional order differs: the client's callback gets (vehicleId, value, old, entity), and OnVehicleStatChanged's (vehicleId, value, previous, cleared, source). now = true also calls it at once for every car with the key.
WatchVehicleData(key, data)CLIENT#

Run code while a car in scope carries a data key: the data twin of WatchVehicleStat. For what each client must do to a car itself because GTA doesn't replicate it: an engine sound, wheel stance, handling.

FieldTypeDescription
onGainedfunction?(entity, value, vehicleId): a car in scope has the key (cars already in scope are announced on registration)
onChangedfunction?(entity, value, old, vehicleId): a new value; without it, onLost(old) then onGained(new)
onLostfunction?(entity, value, vehicleId): the key went or the car left scope; value is what it was
onStartfunction?(entity, value, vehicleId): starts for the car (with driverOnly: you now drive it)
onStopfunction?(entity, value, vehicleId): you got out, the key or car went. A new value while started fires onStop(old), onStart(new)
driverOnlybool?Narrow onStart/onStop to the car you drive, for handling. Default false
idstring?Only for two watchers on one key; the same id replaces (with onStop)
Returns: string|false, string|nil: watcher id, or false and why
exports.whip_core:WatchVehicleData('engine', {
    onGained = function(entity, value) ForceUseAudioGameObject(entity, value.audio) end,   -- every car in scope
    onStart  = function(entity, value) --[[ apply handling ]] end,                        -- you drive it
    onStop   = function(entity, value) --[[ restore handling ]] end,
    driverOnly = true,
})
Your resource stopping never reaches onStop. Put things back in your own onResourceStop too; a resource without dependency 'whip_core' also does it in Whip.onCoreLost. Values are compared by content, so another key changing doesn't re-fire you.
UnwatchVehicleData(watcherId)CLIENT#

Stop one of your data watchers. Fires onStop for what it started, not onLost. Watchers also go when their resource stops.

ParamTypeDescription
watcherIdstringThe id WatchVehicleData returned
Returns: boolean removed

Handling and power

Physics belongs to the driver, so set these from a driverOnly watcher's onStart (WatchVehicleStat or WatchVehicleData) and clear them in its onStop. They apply to one car at a time: asking for a different car puts the previous one back first.

SetVehicleHandlingLayer(vehicle, name, spec) · ClearVehicleHandlingLayer(name) · GetVehicleHandlingBase(vehicle, field)CLIENT#

Never write handling yourself: declare a layer. WHIP captures each field's stock value, applies every resource's layers, and restores the car on every way out (layer cleared, another car, car gone, a resource stopping). Two features writing a field directly undo each other. Drivetrain changes are committed for you.

ParamTypeDescription
vehicletable|numberA vehicle reference
namestringYour layer's name, one per feature; setting it again replaces it
spec.settable?{ field = value }: absolute, in handling.meta units; CHandlingData floats (f…) and whole numbers (n…); gears kept to 1–7
spec.addtable?{ field = amount } (−1000 to 1000), added after every layer's set; layers' adds sum
spec.scaletable?{ field = factor } (0.01–100), applied after every set and add
spec.ordernumber?When two layers set one field, the higher wins. Default 0
Returns: Set → boolean, string|nil; Clear → boolean removed; GetVehicleHandlingBase → a field's value after every set and add, before scale
exports.whip_core:SetVehicleHandlingLayer({ entity = veh }, 'engine', { set = { fInitialDriveForce = 0.34 } })
exports.whip_core:SetVehicleHandlingLayer({ entity = veh }, 'tune', { scale = { fSuspensionForce = 1.2 } })
exports.whip_core:ClearVehicleHandlingLayer('tune')
More power is SetVehiclePower, not a layer. fInitialDriveMaxFlatVel is the gearing (speed at redline in top gear), so scaling it with a turbo stretches every gear.
SetVehiclePower(vehicle, name, multiplier) · ClearVehiclePower(name)CLIENT#

Ask for more (or less) torque on the car this player drives: a turbo's boost, nitrous. Gearing is untouched, so more torque is more speed only where drag held the car back. Ask again as the amount changes; the car gets the product of every resource's ask each frame, and is normal again when nothing is asked.

ParamTypeDescription
vehicletable|numberA vehicle reference
namestringYour ask's name, one per feature; asking again replaces it
multipliernumber0 to 10: 1.25 is a quarter more torque, 1.0 the engine as it is
Returns: Set → boolean, string|nil; Clear → boolean removed
exports.whip_core:SetVehiclePower({ entity = veh }, 'turbo', 1.0 + 0.25 * boost)
Don't call SetVehicleCheatPowerIncrease yourself. The last write in a frame wins, so it undoes WHIP's.

The rev dial

SetVehicleRevRange(vehicle, engine)CLIENT#

Say which engine a vehicle's rev dial follows, on this player's game. The dial uses the engine's model in Config.VehicleHud.rpmByModel, else rpmElectric for an electric motor, else its kind in rpmByClass. After an engine swap, name the donor: a dump truck with an F1 engine then revs to 15k.

ParamTypeDescription
vehicletable|numberA vehicle reference
enginestring|table|nilA model's spawn name; { max, idle? } in thousands of rpm (max 2–20, idle 0 or more, under max); or nil for its own engine
Returns: boolean, string|nil: why not is no such vehicle, not a vehicle model, a range out of bounds, or another running resource set it
exports.whip_core:SetVehicleRevRange({ entity = veh }, 'formula')   -- or { max = 11, idle = 1.2 }
Set it on every client, like the swap: in a WatchVehicleData watcher for every car in view, cleared in onLost. The car you're in updates at once; your settings go when your resource stops.
IsVehicleElectric(vehicle)CLIENT#

Is the vehicle's engine electric? GTA's flag on its engine's model, its own or the one SetVehicleRevRange names. whip_fuel gives electric cars no fuel.

ParamTypeDescription
vehicletable|numberA vehicle reference
Returns: boolean: false for a vehicle not here, or a game build without the flag
SetVehicleDialGear(vehicle, gear)CLIENT#

Set the forward gear the dial shows when your resource drives the gears (whip_autos keeps GTA in gear 1 and changes its ratio). Use 'speed' when you move the car yourself, as a recorded drive does: the dial works the gear out from speed and ratios, and shows R going backwards. Reverse and N stay the game's. This player's dial only; cleared when your resource stops. Clear it when you hand the car back: GTA's gearbox is put in the gear the dial showed.

ParamTypeDescription
vehicletable|numberA vehicle reference
gearnumber|string|nil1 to 20; 'speed'; or nil for the game's gear again
Returns: boolean, string|nil: why not is no such vehicle, a gear out of bounds, or another running resource set it
exports.whip_core:SetVehicleDialGear({ entity = veh }, 3)