leet.vehicle and leet.entities

leet.vehicle

A script can send the current vehicle to a point on the client's autopilot. The player must be in the driver's seat.

The drive uses the same autopilot settings as the menu, and the menu or the bind can stop it. Only one drive runs at a time.

leet.vehicle.driveTo

TypeScript
leet.vehicle.driveTo(x: number, y: number, z: number, callback?: (result: string) => void): undefined

The client's autopilot drives the current vehicle to the point (x, y, z). The player must be the driver. Returns nothing (undefined); the outcome of the drive arrives in callback.

JavaScript
leet.vehicle.driveTo(25.7, -1347.3, 29.5, (result) => {
    leet.ui.notify(`Drive ended: ${result}`);
});

Parameters

  • x, y (number): destination, finite, within ±100000.
  • z (number): height of the destination, finite, within ±100000. Use a value near the ground.
  • callback (function, optional): receives the result, see the table below. May be omitted or undefined.
resultMeaning
"arrived"The vehicle reached the point.
"cancelled"The autopilot was switched off, or the drive was replaced by another driveTo.
"no_vehicle"The player is not the driver of a vehicle.
"left_vehicle"The player left the vehicle during the drive.
"not_in_game"The player is not in the game (for example, loading or disconnected).

Keep in mind

  • The drive uses the autopilot settings from the menu: speed and stop distance. Scripts can read and change them through leet.features as vehicle.autopilot.speed and vehicle.autopilot.stop_range.
  • While the car drives, the vehicle.autopilot feature is on. Switching it off (in the menu, with its bind, or with leet.features.set('vehicle.autopilot', false)) stops the drive.
  • One drive at a time. A new driveTo cancels the drive in progress; its callback receives "cancelled".
  • The callback fires exactly once, when the drive ends, with one string argument, result. It never fires during the driveTo call itself. An exception thrown by it is swallowed.
  • On reconnect, the callback of a drive still in progress is dropped and never fires.

Careful. The vehicle has arrived when it is within the stop distance of the point, height included. Give a z near the ground at the destination, or the vehicle may never count as arrived.

Throws: leet: invalid coordinates (x, y or z is not a number, is NaN / Infinity, or is outside ±100000), leet: callback must be a function (callback is given and is not a function).

Recipes

Drive to a saved point

JavaScript
const TARGET = 'Work';
const point = leet.world.points().find((p) => p.name === TARGET);
const prevSpeed = leet.features.get('vehicle.autopilot.speed');

if (!point) {
    leet.ui.notify(`Save a point named "${TARGET}" in World > Coordinates`, 'warning');
} else {
    leet.features.set('vehicle.autopilot.speed', 25);
    leet.vehicle.driveTo(point.x, point.y, point.z, (result) => {
        if (result === 'arrived') leet.ui.notify(`Arrived at ${TARGET}`, 'success');
        else if (result === 'no_vehicle') leet.ui.notify('Get in a vehicle as the driver first', 'warning');
        else leet.ui.notify(`Drive ended: ${result}`, 'error');
    });
}

leet.events.onUnload(() => {
    leet.features.set('vehicle.autopilot', false); // stops the drive; its callback gets "cancelled"
    if (prevSpeed !== undefined) leet.features.set('vehicle.autopilot.speed', prevSpeed);
});

The script drives to the saved point Work (from World > Coordinates) at 25 m/s. The menu speed is shared, so the script remembers the old one: pressing Stop ends the drive and restores it.

Deliveries one after another

JavaScript
const stops = [
    { name: 'Warehouse', x: 1197.3, y: -3253.6, z: 7.1 },
    { name: 'Shop', x: 25.7, y: -1347.3, z: 29.5 },
    { name: 'Garage', x: -356.1, y: -134.6, z: 38.9 },
];
let stopped = false;

function next(i) {
    if (stopped || i >= stops.length) return;
    const s = stops[i];
    leet.vehicle.driveTo(s.x, s.y, s.z, (result) => {
        if (stopped) return;
        if (result !== 'arrived') {
            leet.ui.notify(`Route stopped before ${s.name}: ${result}`, 'error');
            return;
        }
        leet.ui.notify(`${s.name} reached (${i + 1}/${stops.length})`, 'success');
        next(i + 1);
    });
}

next(0);

leet.events.onUnload(() => {
    stopped = true;
    leet.features.set('vehicle.autopilot', false);
});

Each drive starts from the previous drive's callback, because a new driveTo would cancel the current one. The stopped flag keeps the route from going on after Stop, when the callback receives "cancelled".


leet.entities

A script can change variables of players, vehicles, objects and peds as if the server had sent them. Only your client sees the changes.

Pressing Stop undoes nothing. Remember the old values and restore them in onUnload.

leet.entities.setVariable

TypeScript
leet.entities.setVariable(entity: PlayerMp | VehicleMp | ObjectMp | PedMp, name: string, value: boolean | number | string): undefined

Changes a variable of an entity on this client only, as if the server had set it. Returns nothing (undefined). Read the variable with the regular entity.getVariable(name).

JavaScript
const me = mp.players.local;

leet.entities.setVariable(me, 'myFlag', true);
mp.console.logInfo(`myFlag = ${me.getVariable('myFlag')}`); // myFlag = true

Parameters

  • entity (player, vehicle, object or ped object): an mp entity, for example mp.players.local or an entry from mp.players, mp.vehicles, mp.objects, mp.peds. It must exist on the server (have a remote id). Objects, peds and vehicles created locally by client scripts are not supported.
  • name (string): variable name, non-empty, up to 128 characters.
  • value (boolean | number | string): the new value. A boolean, a finite number, or a string of up to 4096 characters.

Keep in mind

  • Only on your client. Nothing is sent to the server, and other players do not see the change.
  • Client scripts see the new value immediately. entity.getVariable(name) returns it, and handlers registered with mp.events.addDataHandler(name, ...) fire, just as when the server changes the variable.
  • When the server later sends its own value for that variable, it replaces yours.
  • Whole numbers in the 32-bit integer range (-2147483648 to 2147483647) are stored as integers. Other numbers are stored with float precision, about 7 significant digits: setVariable(e, 'myFlag', 1.23456789) reads back as roughly 1.234568.
  • Removing a variable is not supported. Objects and arrays are not accepted as values. Pass a string instead (for example JSON.stringify(...)), and getVariable then returns that string.

Careful. Restoring values is up to your script. Pressing Stop does not undo the changes. Remember entity.getVariable(name) before changing it and set it back in leet.events.onUnload.

MessageWhen
leet: invalid entityentity is not a player, vehicle, object or ped object.
leet: invalid variable namename is not a non-empty string, or is longer than 128 characters.
leet: invalid variable valuevalue is not a boolean, a finite number or a string, or is a string longer than 4096 characters.
leet: unknown entityThe entity is not on the server right now, or it was created locally by a client script.
leet: variable not setThe value could not be written.

Recipes

Restore the old value on Stop

JavaScript
const veh = mp.players.local.vehicle;

if (!veh) {
    leet.ui.notify('Get in a vehicle first', 'warning');
} else {
    const old = veh.getVariable('myLevel');
    leet.entities.setVariable(veh, 'myLevel', 5);

    leet.events.onUnload(() => {
        // a variable cannot be removed: put back the old value if there was one
        if (old !== undefined && old !== null && mp.vehicles.exists(veh)) {
            leet.entities.setVariable(veh, 'myLevel', old);
        }
    });
}

A variable cannot be removed, so the script puts a value back only if there was one. The mp.vehicles.exists check covers a vehicle that is already gone by the time you press Stop.

A data handler sees the change

JavaScript
const onMyFlag = (entity, value, oldValue) => {
    mp.console.logInfo(`[myFlag] ${entity.type} #${entity.remoteId}: ${oldValue} -> ${value}`);
};

mp.events.addDataHandler('myFlag', onMyFlag);

leet.entities.setVariable(mp.players.local, 'myFlag', 'on'); // logs "... -> on"

leet.events.onUnload(() => {
    leet.entities.setVariable(mp.players.local, 'myFlag', 'off');
});

Handlers added with mp.events.addDataHandler fire for setVariable changes just as for changes from the server.

Careful. Such a handler cannot be removed. Every run of the script adds one more, so after three runs each change is logged three times until you reconnect.


Next: Notes and limits