Skip to content
Subdata

[Base Class](../groups/Extension.Base Class.md) / Subdata

Subdata Class

Base class of data control class

  1. Why do you need a data control center?
  • The data control center can help us permanently store data.

  • The data control center achieve the data sync between the server and the client.

  • The data control center has implemented data caching to reduce the frequency of interaction with KV servers.

  • The data control center has achieved unified management of module data.

【 Convenient Data Definition 】 The data body only needs to inherit SubData, and the data can be automatically managed by DataCenter

[Convenient to save data] To save data, just call the save method of the parent SubData to achieve saving

【 Convenient data retrieval 】 To retrieve data, simply input the class name of the data body to obtain the corresponding data

  1. How does the data control center work?

The @ Decorator.saveProperty decorator marked above the data has two functions:

  • Enable fields to be permanently stored (permanent storage can be considered as storing the last game data to continue playing when the game is restarted after exiting the game)

  • Enable the field to be sync to the client

  • If the @ Decorator.saveProperty decorator is not marked, it will lose the ability of permanent storage and sync to the client.

  1. How to use the data control center by rewriting the example in Module Service:

Example usage: C&S and data module combination.

ts
@Component
export default class GameStart extends Script {

    protected onStart(): void {
        ModuleService.registerModule(AppleModS, AppleModC, AppleData);
    }

}
class AppleData extends Subdata {

    @Decorator.persistence()
    appleNum : number = 10;

    public removeApple() {
        this.appleNum -= 1;
        this.save(true);
    }
    public addApple() {
        this.appleNum += 1;
        this.save(true);
    }
}
class AppleModS extends ModuleS<AppleModC,AppleData> {

    public net_appleChange(player:Player) {
        let curPlayer = DataCenterS.getData(this.currentPlayer, AppleData);
        curPlayer.removeApple();
        const otherPlayer = DataCenterS.getData(player, AppleData);
        otherPlayer.addApple();
    }
}
class AppleModC extends ModuleC<AppleModS, AppleData> {

    public npc:Player;

    protected onStart(): void {
        InputUtil.onKeyDown(Keys.P, () => {
            Player.getAllPlayers().forEach( (element) => {
                if(element != this.localPlayer){
                    this.npc = element;
                }
            });
            ModuleService.getModule(AppleModC).sendApple(this.npc);
        });
        InputUtil.onKeyDown(Keys.O, async () => {
            await DataCenterC.ready();
            let apple = DataCenterC.getData(AppleData).appleNum;
            console.log("The current number of apples of the client player is:" + apple);
        });
    }
    public sendApple(player:Player) {
        this.server.net_appleChange(player);
    }
}
@Component
export default class GameStart extends Script {

    protected onStart(): void {
        ModuleService.registerModule(AppleModS, AppleModC, AppleData);
    }

}
class AppleData extends Subdata {

    @Decorator.persistence()
    appleNum : number = 10;

    public removeApple() {
        this.appleNum -= 1;
        this.save(true);
    }
    public addApple() {
        this.appleNum += 1;
        this.save(true);
    }
}
class AppleModS extends ModuleS<AppleModC,AppleData> {

    public net_appleChange(player:Player) {
        let curPlayer = DataCenterS.getData(this.currentPlayer, AppleData);
        curPlayer.removeApple();
        const otherPlayer = DataCenterS.getData(player, AppleData);
        otherPlayer.addApple();
    }
}
class AppleModC extends ModuleC<AppleModS, AppleData> {

    public npc:Player;

    protected onStart(): void {
        InputUtil.onKeyDown(Keys.P, () => {
            Player.getAllPlayers().forEach( (element) => {
                if(element != this.localPlayer){
                    this.npc = element;
                }
            });
            ModuleService.getModule(AppleModC).sendApple(this.npc);
        });
        InputUtil.onKeyDown(Keys.O, async () => {
            await DataCenterC.ready();
            let apple = DataCenterC.getData(AppleData).appleNum;
            console.log("The current number of apples of the client player is:" + apple);
        });
    }
    public sendApple(player:Player) {
        this.server.net_appleChange(player);
    }
}

Table of contents

Properties

onDataChange: Action
Commission for Data Changes

Accessors

currentVersion(): number
The current data version number, when used in conjunction with version, can achieve data upgrade
version(): number
Data version number, if there are changes in the data that need to be rewritten, it can be used in conjunction with the current version to achieve data upgrade

Methods

getSavedProperty<T: >(propertyName: string): T: other
Retrieve stored attribute values through attribute names (used for data upgrade, reading previously stored data)
initDefaultData(): void other
Initialize default data, please overwrite it
onDataInit(): void other
Call after data initialization. Please copy if necessary. In this method, achieve data can be achieved
save(syncToClient: boolean): Subdata other
Save data
syncToClient(): Subdata other
Sync data to client

Properties

onDataChange

Readonly onDataChange: Action

Commission for Data Changes

Accessors

currentVersion

Protected get currentVersion(): number

Protected set currentVersion(value): void

The current data version number, when used in conjunction with version, can achieve data upgrade

Returns

number

The current data version number, when used in conjunction with version, can achieve data upgrade

Parameters

valuenumber

version

Protected get version(): number

Data version number, if there are changes in the data that need to be rewritten, it can be used in conjunction with the current version to achieve data upgrade

Returns

number

Methods

getSavedProperty

Protected getSavedProperty<T>(propertyName): T other

Retrieve stored attribute values through attribute names (used for data upgrade, reading previously stored data)

Parameters

propertyName stringProperty name range:

Returns

TProperty value

Type parameters

Name
T

initDefaultData

Protected initDefaultData(): void other

Initialize default data, please overwrite it


onDataInit

Protected onDataInit(): void other

Call after data initialization. Please copy if necessary. In this method, achieve data can be achieved

Precautions

After this method is called, you need to ensure that the current Version and version are equal


save

save(syncToClient): Subdata other

Save data

Parameters

syncToClient booleanWhether to sync to the client

Returns

Subdataoneself

When modifying data, it will only be synchronized to the corresponding client if save is called and the parameter is set to true.


syncToClient

syncToClient(): Subdata other

Sync data to client

Returns

Subdataoneself