Skip
netMPDeutsch

Client API (JavaScript)

Module net-client. Each resource runs in its own runtime with 64 MB of memory; a call that runs longer than 50 ms stops the script.

Modules

Client code is an ES module. Relative imports stay inside the resource.

import * as net from 'net-client'

The client API. 'net' is an alias.

import * as native from 'natives'

Game functions in camelCase, see Natives. With pointer parameters a native returns [result, value1, ...].

net.resourceName

Name of the running resource.

console.log / info / warn / error

Same output as the net.log functions.

Output

net.log(...args)

Writes to console and log file.

net.logWarning(...args)

Warning line.

net.logError(...args)

Error line.

net.notify(text)

Game notification above the minimap.

Events

Event names have 1 to 64 characters and must not start with __.

net.on(name, handler)

Subscribes to a local or game event.

net.once(name, handler)

Like on, only once.

net.off(name, handler)

Unsubscribes.

net.emit(name, ...args)

Raises a local event.

net.onServer(name, handler)

Subscribes to an event from the server.

JSexample
net.onServer('welcome', text => net.notify(text));

net.onceServer(name, handler)

Only once.

net.offServer(name, handler)

Unsubscribes.

net.emitServer(name, ...args)

Sends an event to the server. JSON, at most 32,000 characters.

JSexample
net.emitServer('buy', 'armour', 1);

RPC

A call with an answer, in both directions.

net.emitRpc(name, ...args)

Calls [ClientRpc(name)] and returns a Promise.

JSexample
const money = await net.emitRpc('balance');

net.onRpc(name, handler)

Answers player.CallRpc(name). The handler may be async.

net.offRpc(name)

Removes the handler.

Virtual entities

net.VirtualEntity, created by the server.

net.VirtualEntity.all / net.VirtualEntity.getByID(id)

Those in range, or one by id.

entity.id / pos / streamingDistance / isStreamedIn / getData(key) / hasData(key)

Read only.

virtualEntityStreamIn (entity) / virtualEntityStreamOut (entity)

It came into or left the streaming distance.

virtualEntityDataChange (entity, key, value, oldValue)

Data of a streamed entity changed.

Exports between resources

Calls are asynchronous; arguments and results are copied as JSON.

net.exportFunction(name, fn)

Offers a function to other client resources.

net.callExport(resource, name, ...args)

Calls it and returns a Promise.

JSexample
const money = await net.callExport('bank', 'balance');

Game events

Subscribe with net.on.

resourceStart ()

The resource started.

resourceStop ()

The resource is stopping. Blips and WebViews are removed automatically.

spawned ()

The local player spawned.

keydown (key)

A key went down. Virtual key code.

keyup (key)

A key was released.

enteredVehicle (vehicle, seat)

The local player sat down. The driver seat is -1.

leftVehicle (vehicle, seat)

The local player left a vehicle.

syncedMetaChange (player, key, value, oldValue)

The server changed synced data of a player or vehicle.

globalSyncedMetaChange (key, value, oldValue)

The server changed global synced data.

gameEntityCreate (entity)

A player or vehicle appeared: joined, same dimension or in range.

gameEntityDestroy (entity)

It disappeared.

streamSyncedMetaChange (target, key, value, oldValue)

Stream-synced data of a player or vehicle changed.

entityEnterColshape (shape, player)

The local player entered a client collision shape.

entityLeaveColshape (shape, player)

The local player left it.

Timers

All return an id.

net.setTimeout(handler, ms)

Runs once after a delay.

net.setInterval(handler, ms)

Runs repeatedly.

net.everyTick(handler)

Runs every frame. Use it for drawing.

net.nextTick(handler)

Runs on the next frame.

net.clearTimeout(id)

Stops a timer.

net.clearInterval(id)

Stops an interval.

net.clearEveryTick(id)

Stops an every-tick handler.

net.clearNextTick(id)

Cancels a next-tick handler.

net.clearTimer(id)

Stops any of the above.

Input and screen

Screen coordinates run from 0 to 1. Draw calls last one frame.

net.isKeyDown(key)

True while the key is held and the game window is focused.

net.KeyCode

Named virtual key codes, for example net.KeyCode.F5.

net.drawText(text, x, y, { scale, color, center })

Draws text. color is [r, g, b, a].

JSexample
net.everyTick(() => net.drawText('Hello', 0.5, 0.05, { center: true }));

net.drawRect(x, y, width, height, color)

Draws a filled rectangle centred on x and y.

net.worldToScreen(position)

Screen position of a world position, or null if off screen.

net.hash(name)

joaat hash of a name.

Player

net.Player. One object per player, so you can compare with ===.

net.Player.local

The local player.

net.Player.all

All players known to this client.

net.Player.getByID(id)

Player by server id, or null.

net.Player.refresh()

Updates and returns the list. The getters above already call it.

player.id / name / isLocal

Server id, name and whether it is you.

player.scriptID

Ped handle for natives.

player.pos / rot

Position and rotation as {x, y, z}.

player.health / armour / model

Values read from the ped.

player.vehicle / vehicleId / seat

Current vehicle and seat.

player.isDead / isAiming / isInCover / isReloading / isSwimming

State flags. isAiming only for the local player.

player.currentWeapon / heading

Weapon hash and heading in degrees.

player.isTalking

True while the player is heard in voice chat.

player.getStreamSyncedMeta(key) / hasStreamSyncedMeta(key)

Stream-synced data. Also on vehicles.

player.getSyncedMeta(key)

Data set by the server.

player.hasSyncedMeta(key)

True if the key exists.

player.setMeta / getMeta / hasMeta / deleteMeta

Client-only data on this object.

Vehicle

net.Vehicle.

net.Vehicle.all

All streamed server vehicles.

net.Vehicle.getByID(id)

Vehicle by server id, or null.

net.Vehicle.refresh()

Updates and returns the list.

vehicle.id / scriptID / model

Server id, handle and model hash.

vehicle.pos / rot / speed / engineOn

Values read from the game.

vehicle.seatCount / driver / passengers

Seats of the model and who sits in it.

vehicle.getSyncedMeta(key) / hasSyncedMeta(key)

Data set by the server.

vehicle.setMeta / getMeta / hasMeta / deleteMeta

Client-only data on this object.

Data

net.setMeta(key, value)

Client-only data of this resource.

net.getMeta(key)

Reads it.

net.hasMeta(key)

True if set.

net.deleteMeta(key)

Removes it.

net.getSyncedMeta(key)

Global data set by the server.

net.hasSyncedMeta(key)

True if set.

Blips and markers

Created by one client, visible only to that player.

new net.PointBlip(x, y, z)

Map blip. Also accepts {x, y, z}. At most 500.

JSexample
const blip = new net.PointBlip(100, 200, 30);
blip.sprite = 8;
blip.name = 'Target';

blip.sprite / color / scale / name / shortRange / pos

Read and write.

blip.valid / destroy()

Whether it exists, and removal.

new net.Marker(type, position, color)

World marker drawn automatically every frame. At most 200.

marker.type / pos / color / scale / visible

Read and write.

marker.valid / destroy()

Whether it exists, and removal.

More blips and text

Local to one client.

new net.RadiusBlip(position, radius)

Area on the map. color, alpha, sprite, valid, destroy().

new net.EntityBlip(entity)

Blip that follows an entity or handle.

new net.TextLabel(text, position, { scale, color, distance })

Text in the world. visible, valid, destroy().

Models, peds and objects

Local entities, visible only to this player. Removed when the resource stops.

net.loadModel(model, timeout)

Requests a model and resolves when it is loaded.

net.loadAnimDict(dictionary, timeout)

Same for an animation dictionary.

net.createPed(model, position, heading)

Resolves to a net.LocalPed.

JSexample
const clerk = await net.createPed('a_m_y_hipster_01', { x: 25, y: -1347, z: 29 }, 90);

net.createObject(model, position, { dynamic })

Resolves to a net.LocalObject.

net.LocalPed / net.LocalObject: scriptID, valid, pos, destroy()

Handle for natives and removal.

Collision shapes

Checked every frame for the local player.

new net.ColShapeSphere(position, radius)

Raises entityEnterColshape and entityLeaveColshape.

shape.contains(position) / destroy()

Point test and removal.

Cursor and controls

net.showCursor(show)

Counted: every true needs a false.

net.isCursorVisible()

Whether the cursor is requested.

net.toggleGameControls(enabled)

Disables or enables game input.

net.gameControlsEnabled()

Current value.

Storage and math

net.storage: get / has / set / delete / keys

Kept on the player's disk per server and resource, at most 64 KB. Players can edit it.

JSexample
net.storage.set('hud', false);

new net.Vector3(x, y, z)

add, sub, mul, dot, length, normalize, distanceTo, toArray.

new net.RGBA(r, g, b, a)

Colour with toArray().

WebView

See WebViews.

new net.WebView(path)

Loads an HTML file of this resource, for example 'client/ui/shop.html'.

view.on / once / off (name, handler)

Events from the page, plus load, error and unfocus.

view.emit(name, ...args)

Sends an event to the page. Queued until it has loaded.

view.focused

Gives mouse and keyboard to the page. F8 always returns them to the game.

view.isVisible

Shows or hides the page.

view.path / valid / destroy()

Source path, whether it exists, and removal.