Saving and Loading Data
This scripting API is no longer supported. Refer to the new Scripting API.
Saving and loading data in Bedrock is tricky because scripts cannot access the local file system directly.
Read about the
executeCommand and the
broadcastEvent functions here.
Be sure to take note of how to create a custom event.
We will use
executeCommand to create and modify tags - note that this function can only be called in a server script.
For saving tags, you can either use one tag, a JSON string for all your data, or multiple tags and remember what the order of them means. I go with a single JSON string, so I don't remember anything as the keys will tell me what the values are for.
If your data is meant to be displayed to the user, make sure they've entered the world first! For example, you could have a listener for
In Bedrock, we can create or listen to events, but we don't send events directly to a specific consumer. Just remember to have the listener and broadcast events set up everywhere they need to be. For example, if I want data loaded for the client to handle, I'd do: client -> server -> client
The client broadcasts an event the server listens for
The server broadcasts an event the client listens for, so it can reply
- Add the dummy JSON files from the dummy entity tutorial to your behavior and resource packs
- Add a save function to your server script, calling
executeCommand. This function will check if the dummy entity is created. If not, it will create it and save to it.
- Add a listener to your server's initialize function, which will listen to a save event broadcast by your client script. Call your save code here
- broadcast an event in your client to let the server know when we want to save data
- Add a listener to your server script's call to initialize. This listener will be for a load request from the client and broadcast the saved data after reading it
- In your client script, you will want to broadcast a load event to let the server know saved data is needed.
- In your client script, add a listener to receive the saved data the server read
Example code coming soon...