SixL client API / events / EventMpPool

Interface: EventMpPool

The event registry: mp.events. It holds the handlers of the client's events, the procedures the server can call, and the messages to the server.

Example

mp.events.add('render', () => { });

Methods

add()

Call Signature

add<K>(eventName, callback): void

Registers an event handler.

Type Parameters
K

K *extends* keyof IClientEvents

Parameters
eventName

K

A client event (see IClientEvents), or any name. A name the server sends arrives as the arguments the server gave.

callback

IClientEvents[K]

The handler.

Returns

void

Remarks

A value that is not a function is not registered; the error is logged.

Example
mp.events.add('playerReady', () => { });

Call Signature

add(eventHandlers): void

Registers several handlers from an object of event name to handler.

Parameters
eventHandlers

{[eventName: string]: (...args) => void; }

The handlers by event name.

Returns

void

Example
mp.events.add({ playerReady: () => { }, render: () => { } });

Call Signature

add(eventName, callback): void

Registers a handler for any event name.

Parameters
eventName

string

The event name.

callback

(...args) => void

The handler.

Returns

void

Example
mp.events.add('shop:opened', (count) => { });

addDataHandler()

addDataHandler<T>(keyName, callback): void

Registers a handler for a key of entity data that the server sets with entity.setVariable.

Type Parameters

T

T *extends* EntityMp

Parameters

keyName

string

The variable's name.

callback

(entity, value, oldValue) => void

Called with the entity, the new value (undefined when the variable was removed) and the old value.

Returns

void

Remarks

Not called for the local player's own variables (they have no handlers). Not called for an entity the scripts cannot see yet: its value is stored, and getVariable returns it, but no handler runs for that change.

Example

mp.events.addDataHandler('cash', (entity, value, oldValue) => { });

addProc()

Call Signature

addProc(procedureName, callback): void

Registers a procedure the server can call and wait for (player.callProc on the server).

Parameters
procedureName

string

The procedure's name.

callback

(...args) => any

Called with the arguments the server gave; its return value, or the value its promise resolves to, goes back to the server.

Returns

void

Remarks

A throw or a rejection sends the error text back, and the server's call fails with it.

Example
mp.events.addProc('getSpeed', () => 0);

Call Signature

addProc(procedures): void

Registers several procedures from an object of name to function.

Parameters
procedures

{[name: string]: (...args) => any; }

The functions by name.

Returns

void

Example
mp.events.addProc({ getSpeed: () => 0 });

call()

Call Signature

call<K>(eventName, ...args): void

Raises an event locally: calls this client's handlers of it at once. Nothing is sent to the server.

Type Parameters
K

K *extends* keyof IClientEvents

Parameters
eventName

K

The event name.

args

...any[]

The arguments the handlers get.

Returns

void

Example
mp.events.call('shop:opened', 3);

Call Signature

call(eventName, ...args): void

Raises any event locally, by name: calls this client's handlers of it at once. Nothing is sent to the server.

Parameters
eventName

string

The event name.

args

...any[]

The arguments the handlers get.

Returns

void

Example
mp.events.call('shop:opened', 3);

callRemote()

callRemote(eventName, ...args): void

Sends an event to the server. On the server its handler gets (player, ...args).

Parameters

eventName

string

The event name.

args

...any[]

The arguments. They are sent as JSON, so they must be JSON-serialisable.

Returns

void

Example

mp.events.callRemote('shop:buy', 'ammo', 3);

callRemoteProc()

callRemoteProc<T>(procedureName, ...args): Promise<T>

Calls a procedure the server registered with mp.events.addProc and waits for its answer.

Type Parameters

T

T = any

Parameters

procedureName

string

The procedure's name on the server.

args

...any[]

The arguments the server's procedure gets.

Returns

Promise<T>

A promise of what the server's procedure returned. It is asynchronous: use await or .then.

Remarks

The promise rejects with the server's error text, or after 10 seconds without an answer.

Example

const cash = await mp.events.callRemoteProc('getCash');

callRemoteUnreliable()

callRemoteUnreliable(eventName, ...args): void

Sends an event to the server, as callRemote.

Parameters

eventName

string

The event name.

args

...any[]

The arguments. They are sent as JSON, so they must be JSON-serialisable.

Returns

void

Remarks

Partial: delivered reliably; the server has no unreliable client events yet.

Example

mp.events.callRemoteUnreliable('position', 1, 2, 3);

cancelPendingProc()

cancelPendingProc(procName?): void

Stops waiting for the server's answer to callRemoteProc: each such promise rejects now.

Parameters

procName?

string

The procedure's name; without one, every procedure.

Returns

void

Remarks

Partial: the server's procedure is not stopped; its late answer is dropped. Not yet confirmed in the game.

Example

mp.events.cancelPendingProc('getCash');

hasPendingProc()

hasPendingProc(procName?): boolean

Whether a call of callRemoteProc still waits for the server's answer.

Parameters

procName?

string

The procedure's name; without one, any procedure.

Returns

boolean

true while one waits.

Remarks

Partial: not yet confirmed in the game.

Example

if (mp.events.hasPendingProc('getCash')) { }

remove()

Call Signature

remove<K>(eventName, callback?): void

Removes a handler of a client event.

Type Parameters
K

K *extends* keyof IClientEvents

Parameters
eventName

K

The event name.

callback?

IClientEvents[K]

The handler to remove. Optional: when left out, every handler of the event is removed.

Returns

void

Example
mp.events.remove('render', onRender);

Call Signature

remove(eventName, callback?): void

Removes a handler of any event name. Optional callback: when left out, every handler of the name is removed.

Parameters
eventName

string

The event name.

callback?

(...args) => void

The handler to remove.

Returns

void

Example
mp.events.remove('shop:opened', onOpened);

Call Signature

remove(eventNames): void

Removes every handler of several event names.

Parameters
eventNames

string[]

The event names.

Returns

void

Example
mp.events.remove(['render', 'playerReady']);

removeProc()

removeProc(procedureName): void

Unregisters a procedure added with addProc. A server call to it then fails with "No such procedure".

Parameters

procedureName

string

The procedure's name.

Returns

void

Example

mp.events.removeProc('getSpeed');