Events::addDataHandler

From RAGE Multiplayer Wiki
Jump to navigation Jump to search

This Function is used to alert client-side with entity's data change for a specified variable

Client-Side Function

C.png C# Js.png JavaScript



C# Syntax
C.png

RAGE.events.AddDataHandler(string name, OnEntityDataChangeDelegate handler);

Parameters

  • name
  • handler


JavaScript Syntax
Jslogo.png

mp.events.addDataHandler(key, handlerFunction);

Parameters

  • key: String - Shared data key's name.
  • handlerFunction: Handler function with parameters:
    • entity: Entity - The entity that had the data change.
    • value: Any - The new value of the data key.
    • oldValue: Any - The old value of the data key.

Example

/*
    Handles testVar change.
    Do "player.data.testVar = 1234;" on server-side to trigger this.
*/

mp.events.addDataHandler("testVar", (entity, value, oldValue) => {
    mp.gui.chat.push(`testVar changed to ${value} on entity ${entity.handle}.`);
});

Example

This example sets nickname for player and triggers players' clientside data handler

Server-Side
mp.events.addCommand('setNick', (player, nickname) => {
  player.setVariable('oldNick', player.name);
  player.name = nickname;
  player.setVariable('newNick', nickname);
});


Client-Side
mp.events.addDataHandler('newNick', function (entity, value, oldValue) {
  if (entity.type === 'player') mp.gui.chat.push(`${player.getVariable('oldNick')} has changed nickname to ${value}`)
})


See Also