Skip to content
ModuleS<T, S>

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

ModuleS<T, S> Class ​

Base class of server module

All server modules must inherit this class in order to be managed by Modulus Service.

Note: Inherits method names from the Modulus S class. Methods with the prefix 'net_XXX' can only be called in the client.

In the Script class, it is said that the classes inherited from Script enjoy the life cycle of onStart(), onUpdate() and onDestroy() script. On this basis, when the client server and data module are registered in the onStart() function

ModuleService.registerModule(YourModS, YourModC, YourData);

Classes inherited from Modulus S also encapsulate a set of lifecycles.

Usage example: create a script named ModuleSExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, the server log will output the log of the player module's execution in each life cycle, and press the F key to see the Player level information in the server log.

ts
@Component
export default class ModuleSExample extends Script {

    protected onStart(): void {
        ModuleService.registerModule(PlayerModuleS, PlayerModuleC, PlayerModuleData);
    }

}

class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
    protected onStart(): void {
        InputUtil.onKeyDown(Keys.F, () => {
            this.server.net_LevelUp();
        })
    }
}
class PlayerModuleS extends ModuleS<PlayerModuleC, PlayerModuleData>{

    protected onAwake(): void {
        Console.log ("---------- Server Layer Module Creation Module ----------");
    }

    protected onStart(): void {
        Console.log ("------------ Server layer module start ----------");
    }

    protected onPlayerEnterGame(player: Player): void {
        Console.log ("---------- Server layer module player enters game ----------");
    }

    protected onPlayerJoined(player: Player): void {
        Console.log ("---------- Server layer module player joining -----------");
    }

    protected onPlayerLeft(player: Player): void {
        Console.log ("---------- Server layer module player leaving ----------");
    }

    protected onUpdate(dt: number): void {
        //Each frame call dt is the time difference between two frames
        // Console. log ("---------- Server layer module update ----------"+dt);
    }

    //Player upgrade
    public net_LevelUp(): void {
        //The client player data that calls this function
        let playerData = this.currentData;
        playerData.levelUp();
        Console. log ("Player level:", playerData. getlevel());
    }
}
class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number;

    protected initDefaultData(): void {
        this.level = 0;
    }

    public getlevel(): number {
        return this.level;
    }

    public levelUp(): void {
        this.level++;
        //Save data
        this.save(true);
    }
}
@Component
export default class ModuleSExample extends Script {

    protected onStart(): void {
        ModuleService.registerModule(PlayerModuleS, PlayerModuleC, PlayerModuleData);
    }

}

class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
    protected onStart(): void {
        InputUtil.onKeyDown(Keys.F, () => {
            this.server.net_LevelUp();
        })
    }
}
class PlayerModuleS extends ModuleS<PlayerModuleC, PlayerModuleData>{

    protected onAwake(): void {
        Console.log ("---------- Server Layer Module Creation Module ----------");
    }

    protected onStart(): void {
        Console.log ("------------ Server layer module start ----------");
    }

    protected onPlayerEnterGame(player: Player): void {
        Console.log ("---------- Server layer module player enters game ----------");
    }

    protected onPlayerJoined(player: Player): void {
        Console.log ("---------- Server layer module player joining -----------");
    }

    protected onPlayerLeft(player: Player): void {
        Console.log ("---------- Server layer module player leaving ----------");
    }

    protected onUpdate(dt: number): void {
        //Each frame call dt is the time difference between two frames
        // Console. log ("---------- Server layer module update ----------"+dt);
    }

    //Player upgrade
    public net_LevelUp(): void {
        //The client player data that calls this function
        let playerData = this.currentData;
        playerData.levelUp();
        Console. log ("Player level:", playerData. getlevel());
    }
}
class PlayerModuleData extends Subdata {
    @Decorator.persistence()
    private level: number;

    protected initDefaultData(): void {
        this.level = 0;
    }

    public getlevel(): number {
        return this.level;
    }

    public levelUp(): void {
        this.level++;
        //Save data
        this.save(true);
    }
}

Type parameters ​

TT
Sextends Subdata

Hierarchy ​

Table of contents ​

Accessors ​

currentData(): S
DataOwner of the Player who calls the call method
currentPlayer(): Player other
Players who call server methods
currentPlayerId(): number other
Get the player ID that calls the server method

Methods ​

getAllClient(): T other
Get the 'All Clients' calling object
getClient(player: number Player): T other
Obtain the "single client" calling object based on the player
getPlayerData(player: string number Player): S other
Get the data of this module of the designated Player
onAwake(): void other
Life cycle method - call when create module
onDestroy(): void other
Life cycle method - Destroy Module call
onExecute(type: number, ...params: any[]): void other
External call an operation of this module
onPlayerEnterGame(player: Player): void other
Life cycle method - Player enters the game (client is ready, data is ready, and front and rear ends can communicate normally)
onPlayerJoined(player: Player): void other
Life cycle method - Player enters the room (Player has just connected to the server, and data and front and rear communication are not ready)
onPlayerLeft(player: Player): void other
Lifecycle Method - Player Leaving Room
onStart(): void other
Life cycle method - call when starting the module
onUpdate(dt: number): void other
Life cycle method - Refresh Module call

Type parameters ​

TT
Sextends Subdata<S>

Accessors ​

currentData ​

• Protected get currentData(): S

DataOwner of the Player who calls the call method

Precautions

Can only be used in server-side RPC methods (methods starting with net_). After the method is executed, it will be cleared. Do not use it elsewhere or asynchronously

Returns ​

S

currentPlayer ​

• get currentPlayer(): Player other

Players who call server methods

Precautions

Can only be used in server-side RPC methods (methods starting with net_). After the method is executed, it will be cleared. Do not use it elsewhere or asynchronously

Returns ​

Player

currentPlayerId ​

• get currentPlayerId(): number other

Get the player ID that calls the server method

Precautions

Can only be used in server-side RPC methods (methods starting with net_). After the method is executed, it will be cleared. Do not use it elsewhere or asynchronously

Returns ​

number

Methods ​

getAllClient ​

• getAllClient(): T other

Get the 'All Clients' calling object

Returns ​

TAll Clients call Object

getClient ​

• getClient(player): T other

Obtain the "single client" calling object based on the player

Parameters ​

player number PlayerTarget player Target player ID

Returns ​

T"Single client" call object

getPlayerData ​

• Protected getPlayerData(player): S other

Get the data of this module of the designated Player

Parameters ​

player string number PlayerTarget Player target Player userId target Player instanceId

Returns ​

Sdata

onAwake ​

• Protected onAwake(): void other

Life cycle method - call when create module


onDestroy ​

• Protected onDestroy(): void other

Life cycle method - Destroy Module call


onExecute ​

• Protected onExecute(type, ...params): void other

External call an operation of this module

Parameters ​

type numberOperation type range: type:
...params any[]Operation parameters

onPlayerEnterGame ​

• Protected onPlayerEnterGame(player): void other

Life cycle method - Player enters the game (client is ready, data is ready, and front and rear ends can communicate normally)

Parameters ​

player PlayerPlayer

onPlayerJoined ​

• Protected onPlayerJoined(player): void other

Life cycle method - Player enters the room (Player has just connected to the server, and data and front and rear communication are not ready)

Parameters ​

player PlayerPlayer

onPlayerLeft ​

• Protected onPlayerLeft(player): void other

Lifecycle Method - Player Leaving Room

Parameters ​

player PlayerPlayer

onStart ​

• Protected onStart(): void other

Life cycle method - call when starting the module


onUpdate ​

• Protected onUpdate(dt): void other

Life cycle method - Refresh Module call

Parameters ​

dt numberTime difference between two frames (unit: seconds) range: type: floating-point number