Skip to content
Data storage and data sharing

Data storage and data sharing

INFO

It takes about 20 minutes to read this article This article provides an overview of how to store data for your games through the APIs provided by the editor.

1. Data Storage

Used for processing data persistent storage, each Player in each game, a single data storage capacity maximum support 64Kb . Any data that exceeds this limit is not guaranteed to be stored and may result in the loss of stored data. When the limit is exceeded, a warning is displayed in the editors' Log.

Steps to use the data storage:

1.1 Storage environments

Multiplayer networked gamesandSingle-player games have different ways of storing data.

In Single-player mode, the data is stored in the local memory of the phone, and the data is persisted without uninstalling the APP.

In Multiplayer mode, a permanent data record is created on a game basis and does not disappear when the game room is destroyed.

1.2 Local Storage

  • Use the asyncSetLocalData() method to add/overwrite data locally, which is only saved locally and persistently without uninstalling the APP.
  • Similarly, data can be retrieved and deleted using the asyncGetLocalData() and asyncRemoveData() methods.
TypeScript
Data storage
SaveLocalData(value: any) {
    Execute on the client side
    if (!SystemUtil.isClient()) {

        let myData = new CustomData();

        getDataSize() gets the size of the current data in bytes to ensure that the storage limit of the encoded data is not exceeded
        let myDataSize = DataStorage.getDataSize(myData);
        console.log('Current data size: ${myDataSize}`);

        /* Stores the score properties in myData by the "score" key name
          - Parameter settings: key: string, value: unknown
          - Return value: Promise<DataStorageResultCode>
        */
        DataStorage.asyncSetLocalData("score", value).then(async (state) => {
            Check whether the storage is successful
            if (state == DataStorageResultCode.Success) {
                /* Get local data asynchronous via key name
                  - Parameter settings: key: string
                  - Return value: Promise<unknown>
                */
                Read the data
                this._CustomData = await DataStorage.asyncGetLocalData("score");
                console.log(`asyncGetLocalData: ${this._CustomData}`);
            } else {
                If the storage fails, the storage status is returned
                console.log(`asyncSetLocalData: "${DataStorageResultCode[state]}"`);
            }
        });
    }
}
Data storage
SaveLocalData(value: any) {
    Execute on the client side
    if (!SystemUtil.isClient()) {

        let myData = new CustomData();

        getDataSize() gets the size of the current data in bytes to ensure that the storage limit of the encoded data is not exceeded
        let myDataSize = DataStorage.getDataSize(myData);
        console.log('Current data size: ${myDataSize}`);

        /* Stores the score properties in myData by the "score" key name
          - Parameter settings: key: string, value: unknown
          - Return value: Promise<DataStorageResultCode>
        */
        DataStorage.asyncSetLocalData("score", value).then(async (state) => {
            Check whether the storage is successful
            if (state == DataStorageResultCode.Success) {
                /* Get local data asynchronous via key name
                  - Parameter settings: key: string
                  - Return value: Promise<unknown>
                */
                Read the data
                this._CustomData = await DataStorage.asyncGetLocalData("score");
                console.log(`asyncGetLocalData: ${this._CustomData}`);
            } else {
                If the storage fails, the storage status is returned
                console.log(`asyncSetLocalData: "${DataStorageResultCode[state]}"`);
            }
        });
    }
}

::: warning: The local storage capacity is limited to 5 MB, and the local storage does not participate in data sharing. :::

1.3 Online Storage

  • If you use the asyncSetData() method to store data, you can manage the same numbers by using a customized key value.
  • For data stored with customized index values, you can use asyncGetData() to read its data.
  • Data stored online can be viewed on the controls console and set to be shared, see Part 2 for details.
TypeScript
Data storage
SaveOnlineData(value) {
    Execute on the server side
    if (SystemUtil.isServer()) {

        let myData = new CustomData();

        getDataSize() to get the size of the current data in bytes to ensure that the storage limit of the encoded data is not exceeded
        let myDataSize = DataStorage.getDataSize(myData);
        console.log('Current data size: ${myDataSize}`);

        /* Store the score properties in myData by the "score" key name
          - Parameter settings: key:string, value:unknown
          - Return value: Promise<DataStorageResultCode>
        */
        DataStorage.asyncSetData("score", value).then(async (state) => {
            Check whether the storage is successful
            if (state == DataStorageResultCode.Success) {
                /* Get customized data asynchronous by key name
                  - Parameter setting: key:string
                  - Return value: Promise<unknown>
                */
                Read the data
                this._CustomData = await DataStorage.asyncGetData("score");
                console.log(`asyncGetData:${this._CustomData}`);
            } else {
                If the storage fails, the storage status is returned
                console.log(`asyncSetCustomData:"${DataStorageResultCode[state]}`);
            }
        });
    }
}
Data storage
SaveOnlineData(value) {
    Execute on the server side
    if (SystemUtil.isServer()) {

        let myData = new CustomData();

        getDataSize() to get the size of the current data in bytes to ensure that the storage limit of the encoded data is not exceeded
        let myDataSize = DataStorage.getDataSize(myData);
        console.log('Current data size: ${myDataSize}`);

        /* Store the score properties in myData by the "score" key name
          - Parameter settings: key:string, value:unknown
          - Return value: Promise<DataStorageResultCode>
        */
        DataStorage.asyncSetData("score", value).then(async (state) => {
            Check whether the storage is successful
            if (state == DataStorageResultCode.Success) {
                /* Get customized data asynchronous by key name
                  - Parameter setting: key:string
                  - Return value: Promise<unknown>
                */
                Read the data
                this._CustomData = await DataStorage.asyncGetData("score");
                console.log(`asyncGetData:${this._CustomData}`);
            } else {
                If the storage fails, the storage status is returned
                console.log(`asyncSetCustomData:"${DataStorageResultCode[state]}`);
            }
        });
    }
}

1.4 Notes and Recommendations

  • Data storage-related APIs need to be requested in the corresponding environments, and attempting to access the online store in the client script will result in an error, the same on the server sideAccessing local storage in scripts also causes errors.
  • Currently, the storable data types only include the basic data types, and map types, GameObjects, and customized functions are not supported.

2. Data Sharing

Based on document, we learned how to store game data through the API provided by editor, and if we set up the data to be stored online, we can control its read/write access through the developer platform so that we can share this data with other game, achieve multiple game share one game data.

Steps to use the data storage:

2.1 Storage of Data

We use the asyncSetData() function to store the data online on the developer platform, so that we can see the data in the developer platform. You can also use the asyncSetData() function to get this data.

2.2 Local Storage

  • Use the asyncSetLocalData() method to add/overwrite data locally, which is only saved locally and persistently without uninstalling the APP.
  • Similarly, data can be retrieved and deleted using the asyncGetLocalData() and asyncRemoveData() methods.
TypeScript
Set a data with the key set to hp and the value to 50.
await DataStorage.asyncSetData("hp", "50");
Set a data with a key of lv and a value of 1.
await DataStorage.asyncSetData("lv", "1");
Set a data with a key of "coin" and a value of "9999".
await DataStorage.asyncSetData("coin", "9999");
Obtain data whose key is hp
let data1 = await DataStorage.asyncGetData("hp");
Obtain the data whose key is lv
let data2 = await DataStorage.asyncGetData("lv");
Get the data whose key is coin
let data3 = await DataStorage.asyncGetData("coin");
Set a data with the key set to hp and the value to 50.
await DataStorage.asyncSetData("hp", "50");
Set a data with a key of lv and a value of 1.
await DataStorage.asyncSetData("lv", "1");
Set a data with a key of "coin" and a value of "9999".
await DataStorage.asyncSetData("coin", "9999");
Obtain data whose key is hp
let data1 = await DataStorage.asyncGetData("hp");
Obtain the data whose key is lv
let data2 = await DataStorage.asyncGetData("lv");
Get the data whose key is coin
let data3 = await DataStorage.asyncGetData("coin");

2.3 View games data in the Developer Platform

  • After we publish the s and pass the review, we open the developer platform and find the games we want to modify in the data store.
Example
  • Select the game data and select the data type as customized.
Example
  • Finally, search according to the key, or directly click the search button, you can see all the data stored in our game!
Example

::: warning: The game data on the platform needs to wait for the actual game to run, that is, after the game logic of the data storage is executed, and the corresponding data will exist on the developer platform. Only data stored online can be edited on the platform, locally stored data cannot be edited. :::

2.4 Set developer platform database permissions

  • Find the License Management button and click to open the interface for managing games licenses.
Example
  • click Add button and add authorization game.

    • games name: The games that need to share data
    • Permission scope: is the ability to set permissions for data changes
    • Licensed games: Licensed games that can be specified by filling in the gameid for certain games
    • Status: Enable authorization or turn off authorization
Example

2.5 Other games read and modify shared data

  • When the games has activated data authorization, another game can read and modify the data!
  • Then we use the gameid in the asyncGetOtherGameData() function to get the platform data of the corresponding game. Of course, we can also use the asyncSetOtherGameData() function to modify this data.
TypeScript
Obtain shared platform data of other games through games ID and key
this.hp = await DataStorage.asyncGetOtherGameData("gameid", "hp");
Modify the shared platform data of other games through the games ID and key
DataStorage.asyncSetOtherGameData("gameid", "hp", "600");
Obtain shared platform data of other games through games ID and key
this.hp = await DataStorage.asyncGetOtherGameData("gameid", "hp");
Modify the shared platform data of other games through the games ID and key
DataStorage.asyncSetOtherGameData("gameid", "hp", "600");