About This File
If you ever used GTA Network's setWorldSyncedData/SetWorldSharedData, you probably have an idea of what this resource does. If you haven't used those, this resource lets you set variables that will be synced to all clients.
Installing
- Put the files you downloaded in their respective places
- Add require('worlddata') to client_packages/index.js, preferably as the first module you require
- All done
Using
There isn't much to it, you can do one of these to set shared data:
mp.world.data.myDataKey = myValue; mp.world.data["myDataKey"] = myValue;
you can do one of these to get shared data:
mp.world.data.myDataKey mp.world.data["myDataKey"]
and you can do one of these to remove shared data:
delete mp.world.data.myDataKey; delete mp.world.data["myDataKey"];
(1.1) now you can do this to set multiple shared data at once, thanks to @kemperrr:
mp.world.setData(updatedObject); // see examples
You can use setting/deleting code on clientside as well but it won't affect the data on serverside. If anything your changes will get overriden when myDataKey is updated, so there's no point in using setting/deleting code on clientside.
Events (Clientside)
// worldDataReady is called when the player receives all shared data. mp.events.add("worldDataReady", () => { // Example, print all data keys to chat mp.gui.chat.push(`World data ready! ${Object.keys(mp.world.data).join(",")}`); }); // worldDataChanged is called when shared data changes. mp.events.add("worldDataChanged", (key, oldValue, newValue) => { // Example, show which data key changed with its old and new value mp.gui.chat.push(`World data changed: ${key} | old: ${oldValue} | new: ${newValue}`); }); // worldDataRemoved is called when shared data is removed. mp.events.add("worldDataRemoved", (key) => { // Example, show which data key got removed mp.gui.chat.push(`World data removed: ${key}`); });
Example (Serverside)
let gameSeconds = 0; // Increase gameSeconds every second and update the world data every 5 seconds, for no reason... setInterval(() => { gameSeconds++; if (gameSeconds % 5 === 0) { // You can use mp.world.data["exampleVariable"] as well mp.world.data.exampleVariable = gameSeconds; console.log(`exampleVariable is now ${mp.world.data.exampleVariable}`); } }, 1000);
// Will update serverUpdated, serverName, serverWebsite shared data. mp.world.setData({ "serverUpdated": Date.now(), "serverName": "RAGE Multiplayer Server #545" "serverWebsite": "https://rage.mp/" });
Source code is available on GitHub in case you don't want to download: https://github.com/root-cause/ragemp-world-data
What's New in Version 1.1.0 See changelog
Released
This feature was contributed by @kemperrr.
- Added mp.world.setData(updatedObject) which provides a nice & efficent way to update multiple shared data.
// Example: Will update serverUpdated, serverName, serverWebsite shared data. mp.world.setData({ "serverUpdated": Date.now(), "serverName": "RAGE Multiplayer Server #545" "serverWebsite": "https://rage.mp/" });