[Base Class](../groups/Extension.Base Class.md) / DataCenterS
DataCenterS Class
The server data center manages the data of all Player.
Example usage: Create a script called DataCenterSEample, place it in the object bar, open the script, modify the original content to the following, save and run the game. When a player joins, the current player's level and the levels of all current players will be output. When a player leaves, the current player will level up and output (on the pie, players need to click the x key to leave)
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isServer()) {
DataCenterS.onPlayerJoin.add(this.onPlayerJoin, this);
DataCenterS.onPlayerLeave.add(this.onPlayerLeave, this);
}
}
//Player joins and data is ready
private onPlayerJoin(player: mw.Player): void {
let playerData = DataCenterS.getData(player, PlayerModuleData);
Console.log ("Player joined, current player level:", playerData. getlevel());
Console. log ("Display the level of all current Player:");
const playerIds = DataCenterS.getReadyPlayerIds();
playerIds.forEach(playerId => {
let playerData = DataCenterS.getData(playerId, PlayerModuleData);
Console.log ("Player playerId is:"+playerId, "level:", playerData. getlevel());
});
}
//Players leave
private onPlayerLeave(player: mw.Player): void {
let playerData = DataCenterS.getData(player, PlayerModuleData);
playerData.levelUp();
Console.log ("Player leaves, level upgraded to:", playerData. getlevel());
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
//Player upgrade
public levelUp(): void {
this.level++;
//Save data
this.save(false);
}
}
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isServer()) {
DataCenterS.onPlayerJoin.add(this.onPlayerJoin, this);
DataCenterS.onPlayerLeave.add(this.onPlayerLeave, this);
}
}
//Player joins and data is ready
private onPlayerJoin(player: mw.Player): void {
let playerData = DataCenterS.getData(player, PlayerModuleData);
Console.log ("Player joined, current player level:", playerData. getlevel());
Console. log ("Display the level of all current Player:");
const playerIds = DataCenterS.getReadyPlayerIds();
playerIds.forEach(playerId => {
let playerData = DataCenterS.getData(playerId, PlayerModuleData);
Console.log ("Player playerId is:"+playerId, "level:", playerData. getlevel());
});
}
//Players leave
private onPlayerLeave(player: mw.Player): void {
let playerData = DataCenterS.getData(player, PlayerModuleData);
playerData.levelUp();
Console.log ("Player leaves, level upgraded to:", playerData. getlevel());
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
//Player upgrade
public levelUp(): void {
this.level++;
//Save data
this.save(false);
}
}
Table of contents
Properties
onPlayerJoin: Action1 <Player > |
---|
When the player enters the game's commission, it can ensure that the player's data is ready when the commission is called |
onPlayerLeave: Action1 <Player > |
The player's commission to leave the game can be used as the player's final data processing |
Methods
getData<T : extends Subdata <T >>(player : string number Player , SubdataType : TypeName <T : extends Subdata <T >>): T : extends Subdata <T > other |
---|
Obtain player's sub data |
getReadyPlayerIds(): number [] other |
Get all Player IDs that are online and data ready |
setDataSource(gameId : string ): void other |
Set data source |
Properties
onPlayerJoin
▪ Static
Readonly
onPlayerJoin: Action1
<Player
>
When the player enters the game's commission, it can ensure that the player's data is ready when the commission is called
onPlayerLeave
▪ Static
Readonly
onPlayerLeave: Action1
<Player
>
The player's commission to leave the game can be used as the player's final data processing
Methods
getData
• Static
getData<T
>(player
, SubdataType
): T
other
Obtain player's sub data
Parameters
player string number Player | Player Player userId Player instanceId |
---|---|
SubdataType TypeName <T > | Data class |
Returns
T | data object |
---|
Usage example: create a script named DataCenterSExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and when Player joins, you will see the information of Player level 0 in the server's log
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isServer()) {
DataCenterS.onPlayerJoin.add((player)=>{
let playerData = DataCenterS.getData(player, PlayerModuleData);
Console. log ("Player level:", playerData. getlevel());
});
}
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
}
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isServer()) {
DataCenterS.onPlayerJoin.add((player)=>{
let playerData = DataCenterS.getData(player, PlayerModuleData);
Console. log ("Player level:", playerData. getlevel());
});
}
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
}
Type parameters
T | extends Subdata <T > |
---|
getReadyPlayerIds
• Static
getReadyPlayerIds(): number
[] other
Get all Player IDs that are online and data ready
Returns
number [] | Player id array |
---|
Example usage: Create a script called DataCenterSEample, place it in the object bar, open the script, modify the original content to the following, save and run the game, press the F key, and you will see the playerid and level of all players whose data is ready in the server log
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isClient()) {
InputUtil.onKeyDown(Keys.F, () => {
this.traceAllLevel();
})
}
}
//Test the level of all data ready Player
@mw.RemoteFunction(mw.Server)
public traceAllLevel(): void {
if (SystemUtil.isServer()) {
const playerIds = DataCenterS.getReadyPlayerIds();
playerIds.forEach(playerId => {
let playerData = DataCenterS.getData(playerId, PlayerModuleData);
Console.log ("Player playerId is:"+playerId, "level:", playerData. getlevel());
});
}
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
}
@Component
export default class DataCenterSExample extends Script {
protected onStart(): void {
if (SystemUtil.isClient()) {
InputUtil.onKeyDown(Keys.F, () => {
this.traceAllLevel();
})
}
}
//Test the level of all data ready Player
@mw.RemoteFunction(mw.Server)
public traceAllLevel(): void {
if (SystemUtil.isServer()) {
const playerIds = DataCenterS.getReadyPlayerIds();
playerIds.forEach(playerId => {
let playerData = DataCenterS.getData(playerId, PlayerModuleData);
Console.log ("Player playerId is:"+playerId, "level:", playerData. getlevel());
});
}
}
}
class PlayerModuleData extends Subdata {
@Decorator.persistence()
private level: number = 0;
public getlevel(): number {
return this.level;
}
}
setDataSource
• Static
setDataSource(gameId
): void
other
Set data source
Parameters
gameId string | The gameId of the data source needs to be authorized in the developer's background range: the string length depends on the gameId length |
---|