Persisted Player Data
Persisted Player Data was created for your game to save progress and data related to each unique player of your game that can be retrieved in the future even after they leave and rejoin your game. This is useful for things like player progression systems, lifetime stats tracking, player quest completion and unlocks, and so much more.
The Persisted Player Data system builds on top of the Persisted Data concepts and exposes a number of convenience methods for retrieving and updating player data.
Player Data Types
Any JSON compatible data can be stored for a player. Typically, the best practice is to use a single object with many arbitrary properties that reflect the data you want to track for a player for your game.
You can learn more about supported data type here: Supported Data Types
Retrieving Player Data
You can retrieve player data by using the player.getPersistedData()
method. Any instance of a player object has this available.
Here's an example of a common usage pattern of retrieving a player's data when they join a game.
startServer(world => {
world.on(PlayerEvent.JOINED_WORLD, async ({ player }) => {
// ... other common init code, like spawning an entity, etc
// get player data
const playerData = await player.getPersistedData();
// do something with the playerData
});
// ... other code
});
Setting & Updating Player Data
Similar to retrieving player data, you can set or update player data, which utilizes Shallow Merging, using the player.setPersistedData()
method. Any instance of a player object has this available.
Here's an example of using this.
// other code..
await player.setPersistedData({ "someData": 123 });
Last updated