Skip to content

Ped

import { Ped } from "@sagl/server";

A ped (NPC), from server.createPed(), events, or server.peds (the ambient population included). The nearest player's game runs its AI, as in single-player; the server checks what it reports.


Properties

Name Type Access Description
id number read
model number read Ped model (0 for a story character).
character string | null read The story character it is ("ryder"), made with createPed("ryder", ...).
data Record<string, unknown> read Your own state.
exists boolean read False once destroyed.
position Vector3 read As its owner last reported.
heading number read Degrees.
health, armour number read
weapon number read Weapon in its hand.
dead boolean read
world number read
owner Player | null read The player whose game simulates it.
ambient boolean read Part of the ambient population.
vehicle Vehicle | null read The vehicle it's in.
seat number | null read 0 driver, 1-8 passenger.
server.on("pedDeath", (ped, killer) => {
  console.log(`ped ${ped.id} (model ${ped.model}) killed by ${killer ?? "nobody"}`);
});

Methods

putInVehicle(vehicle, seat?)

putInVehicle(vehicle: Vehicle, seat?: number): void

Puts it in a vehicle seat, for everyone. A driver whose behaviour is "wander" drives around the roads.

Parameter Type Description
vehicle Vehicle In the same world.
seat number 0 driver (default), 1-8 passenger.

Throws Error if the seat is taken or the vehicle is in another world.

const taxi = server.createVehicle(420, { x: 1540, y: -1680, z: 13.5 });
const driver = server.createPed(7, taxi.position, 0, { behaviour: "wander" });
driver.putInVehicle(taxi);

removeFromVehicle()

removeFromVehicle(): void

Takes it out of its vehicle, beside it.

release()

release(): void

Hands it back to the game, like the game's MARK_CHAR_AS_NO_LONGER_NEEDED: its order stops, everyone sees it, and the server removes it as it does the ambient population: once nobody is near, or (a body) after it has lain a minute out of everyone's view. Missions release theirs when they end.

server.on("pedDeath", (ped) => { if (ped.data.bodyguard) ped.release(); });   // leave the body

destroy()

destroy(): void

Removes it for everyone.

setTimeout(() => guard.destroy(), 5 * 60_000);

Speech

say(line)

say(line: { context?: number; subtitle?: string; dialogue?: string; table?: string; ms?: number; to?: Player[] }): Promise<void>

Says something: one of the game's voice lines in the ped's own voice, and/or a subtitle; or a line of the game's mission dialogue, its recording and its text.

Parameter Type Description
line.context number A VoiceContext: Bump, Chat, ShopBuy, GenericInsultMale... (about 350 of them). The line comes from the ped's voice, which depends on its model, and not every voice has every context: if it hasn't, nothing plays.
line.subtitle string Shown at the bottom of the screen, in the game's subtitle style, to the players who hear it, if they have Subtitles on in their pause menu. Up to 255 characters.
line.dialogue string A line of the game's mission dialogue by its GXT key, e.g. "RYD2_AA": the game's own recording, played from this ped (positioned, and moving its lips), with its text as the subtitle (in each player's language). See Mission dialogue.
line.table string With dialogue: the mission table its text is in, e.g. "RYDER2".
line.ms number How long the subtitle stays. Default: about as long as reading it takes (a recording: as long as it lasts).
line.to Player[] Who hears it. Default: everyone within 40 m who can see the ped.
line.file string A recording of your own (a client file) instead, from this ped, with subtitle timed to it (see audio.speak). line.inside: from inside that vehicle, muffled.
line.effects AudioEffects With dialogue: the recording through audio effects ("phone", "radio"...). Then it plays as the server's sounds do, and the ped's lips don't move.

Returns a promise that resolves when it's done (after ms, the recording, or the reading time), so lines can be awaited one after another.

Throws RangeError if the game has no recording of dialogue.

await vendor.say({ context: VoiceContext.ShopBuy, subtitle: "Hot dogs! Get your hot dogs!" });
await vendor.say({ subtitle: "That'll be five bucks.", to: [customer] });

Mission dialogue

The game's missions' lines are recorded in its own files, and every player's game has them. A line is its GXT key: a mission's prefix and two letters (RYD2_AA, RYD2_AB, ... for Robbing Uncle Sam, in table RYDER2). CJ's lines are said with player.say().

const R = { table: "RYDER2" };
await ryder.say({ dialogue: "RYD2_AA", ...R });     // "You think you can roll this van without killing us both?"
await player.say({ dialogue: "RYD2_AB", ...R });    // CJ: "Just shut up and get in."

About 80 missions' dialogue is known, mapped from the game's own files (scripts/gen-dialogue-data.py). A key without a known recording throws.


Who sees it, and whose game runs it

onDamage(fn), onThreatened(fn, options?)

onDamage(fn: (attacker: Player | Ped, hit: Hit) => boolean | number | void): () => void
onThreatened(fn: (player: Player, how: "aim" | "melee") => void, options?: { range?: number; melee?: boolean }): () => void

onDamage: what happens to this ped when hurt, as pedDamage for all (called first): false refuses it, a number replaces the damage. onThreatened: a player aiming a gun at it within range m (default 8), or hitting it with fists or a melee weapon (unless melee: false): for a ped that gives up in a holdup or an arrest. Each returns a function that removes it.

clerk.onThreatened((player) => { void clerk.handsUp(5000); });

visibleTo

visibleTo: readonly Player[] | "world"

"world" (the default): everyone its virtual world shows it to (its world, and worlds viewing it). A list of players: only them, while they're in its world or one viewing it. Players who can't see a ped can't hit it, and it can't hit them. Changing it takes effect at once; players who leave drop off the list.

const witness = server.createPed(150, alley, 0, { visibleTo: mission.players });
witness.visibleTo = "world";    // revealed to everyone

controller

controller: Player | null

The player whose game always runs it, at any distance, while they can see it; null (the default), the nearest player's. Pin a mission's NPC to the mission's player so a chase or a follow plays out on their screen, and a bystander can't take it over. If the controller can't have it (left, in another world, can't see it), the nearest player who can runs it meanwhile.

const target = server.createPed(29, start, 0, { visibleTo: [player], controller: player, mission: true });

Mission settings

How a ped behaves beyond its orders. A ped made with createPed(..., { mission: true }) starts from { reactions: "self-defence", keepTask: true }: it ignores gunfire and sticks to its orders, but fights back when hit. Without mission it behaves like the game's own peds of its kind (a civilian flees gunfire).

set(settings)

set(settings: PedSettings): this

Changes its settings, for everyone; fields left out keep their value.

Field Type Description
reactions "normal" | "none" | "self-defence" normal: like the game's peds of its kind. none: ignores everything, even being hit. self-defence: ignores everything but fights whoever hits it.
keepTask boolean Being hit doesn't stop its order. Without it, a self-defending ped drops its order (failed, "attacked") to fight back.
invincible boolean Takes no damage.
proofs { bullet?, fire?, explosion?, collision?, melee? } Takes no damage of these kinds. The server enforces it, not just the game.
draggable boolean false: players can't drag it out of a vehicle.
accuracy number Aim, 1-100.
hostileTo Player[] Attacks these players on sight, when not busy with an order. Players who leave drop off the list.
hostileRange number How near "on sight" is, in metres. Default 40.

Returns the ped.

const clerk = server.createPed(Skin.Clerk, counter, 180, { mission: { reactions: "none", invincible: true } });

const dealer = server.createPed(29, alley, 90, { mission: true, weapon: Weapon.Pistol });
dealer.set({ hostileTo: [player], accuracy: 60 });   // the shootout starts

settings

readonly settings: Readonly<PedSettings>

Its settings as last set.


Orders

Tell a ped what to do. Each order is one of the game's own tasks, run by its owner's game with the ped's own AI (it steers round things, opens doors, gets up when knocked over and carries on). If another player's game takes the ped over, it carries on from the step it was at.

Every order returns a promise of how it ended, an OrderResult. It never rejects: a failed or cancelled order resolves too. A new order cancels the one before ("superseded").

const result = await clerk.walkTo(counter);
if (result.status !== "done") console.log("didn't make it:", result.reason);

walkTo(position, options?)

walkTo(position: Vector3, options?: { pace?: Pace; straight?: boolean; radius?: number }): Promise<OrderResult>

Walks (or runs, sprints) to position.

Parameter Type Description
position Vector3 Where to.
options.pace "walk" | "run" | "sprint" Default "walk".
options.straight boolean A straight line instead of along the paths (short distances, open ground).
options.radius number Done within this many metres. Default 1.

Fails with "unreachable" if it makes no progress for 20 seconds.

await ped.walkTo({ x: 2495, y: -1660, z: 13.5 }, { pace: "run" });

follow(target, options?)

follow(target: Player | Ped, options?: { distance?: number; ms?: number }): Promise<OrderResult>

Keeps within distance (2 m) of target, for ms (until the next order if left out). Fails with "target lost" if the target goes.

bodyguard.follow(player, { distance: 3 });

fleeFrom(target, options?)

fleeFrom(target: Player | Ped, options?: { distance?: number; ms?: number }): Promise<OrderResult>

Runs from target until distance (50 m) away, or ms is up.

const r = await witness.fleeFrom(player, { distance: 80 });

face(target)

face(target: Player | Ped | Vector3): Promise<OrderResult>

Turns to face a player, a ped or a position.

await vendor.face(customer);

lookAt(target, ms)

lookAt(target: Player | Ped, ms: number): Promise<OrderResult>

Turns its head to target for ms.

standStill(ms?), handsUp(ms?), cower(ms?)

standStill(ms?: number): Promise<OrderResult>
handsUp(ms?: number): Promise<OrderResult>
cower(ms?: number): Promise<OrderResult>

For ms, or until the next order if left out.

await scientist.handsUp(4000);

attack(target)

attack(target: Player | Ped): Promise<OrderResult>

Attacks target with what it holds, until one of them is dead. Done when the target is down; fails with "died" if the ped dies first.

dealer.attack(player);

wander()

wander(): Promise<OrderResult>

Wanders the streets until the next order.

enterVehicle(vehicle, seat?), leaveVehicle(), driveTo(position, options?)

enterVehicle(vehicle: Vehicle, seat?: number): Promise<OrderResult>
leaveVehicle(): Promise<OrderResult>
driveTo(position: Vector3, options?: { speed?: number; hurry?: boolean; radius?: number }): Promise<OrderResult>

Walks to vehicle and gets in (seat 0, the default, drives; 1-8 a passenger's), done once it's sitting there; gets out of the one it's in; drives the vehicle it's driving to position along the roads at speed m/s (default 12), by the rules or, with hurry, weaving through traffic and lights, done within radius (8 m). standStill() in a vehicle: a driver pulls up and waits, a passenger sits.

await benny.enterVehicle(cab, 2);
void cabbie.driveTo(UNITY_STATION, { speed: 13 });

playAnim(block, name, options?)

playAnim(block: string, name: string, options?: { loop?: boolean; ms?: number }): Promise<OrderResult>

Plays one of the game's animations: name from the animation block ("DANCING", "FOOD", "DEALER", "SMOKING", ...), or from one of your own sets. Once (done when it ends), or looping for ms. Fails with "animation" if there's no such block or animation. Everyone sees it: players' copies of the ped play the same animation in step, and players arriving mid-way join it where it is.

await vendor.playAnim("FOOD", "SHP_Tray_Pose", { loop: true, ms: 5000 });

sequence(steps)

sequence(steps: PedStep[]): Promise<OrderResult>

Several steps run one after another by its owner's game, with no pause between them (up to 16). Resolves when the last is done, or one fails (result.step says which). Each finished step fires stepDone.

A PedStep is one of:

Step Same as
{ walkTo: Vector3, pace?, straight?, radius? } walkTo
{ follow: target, distance?, ms? } follow
{ fleeFrom: target, distance?, ms? } fleeFrom
{ face: target \| Vector3 } face
{ lookAt: target, ms } lookAt
{ standStill: ms \| true }, { handsUp: ... }, { cower: ... } true: until the next order
{ attack: target } attack
{ wander: true } wander
{ playAnim: [block, name], loop?, ms? } playAnim
const r = await courier.sequence([
  { walkTo: door },
  { face: player },
  { handsUp: 2000 },
  { walkTo: car, pace: "run" },
]);

stop()

stop(): void

Drops its order (resolved as "cancelled", "stopped"): back to standing, or wandering if it was created to wander.

busy

readonly busy: boolean

Whether it's carrying out an order.

on(event, listener), off(event, listener)

on(event: "stepDone", listener: (step: number, orderId: number) => void): this
on(event: "orderDone", listener: (orderId: number) => void): this
on(event: "orderFailed", listener: (reason: string, step: number, orderId: number) => void): this

This ped's order events. pedOrder has every ped's in one place.

clerk.on("orderFailed", (reason) => console.warn("clerk:", reason));

OrderResult

Field Type Description
status "done" | "failed" | "cancelled" How it ended.
reason string Failed: "died", "removed", "unreachable", "target lost", "animation", "in a vehicle", "attacked" (self-defence without keepTask), "refused". Cancelled: "superseded", "stopped".
step number The step it ended at (the last, if done).