Events::addDataHandler

From RAGE Multiplayer Wiki
Revision as of 08:16, 26 December 2022 by Xabi (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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


Client-Side Function

 C#  JavaScript



C# Syntax

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


JavaScript Syntax

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