Skip to content
ModuleC<T, S>

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

ModuleC<T, S> Class ​

Base class of client module

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

Note: Inherit method names from the Modulus C class. Only methods whose method name prefix is "net_XXX" can be call in the inherited ModuleS.

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);

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

ts
@Component
export default class ModuleCExample extends Script {

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

}

class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{

    protected onAwake(): void {
        Console.log ("-------------- player module creation module ----------");
    }

    protected onStart(): void {
        Console.log ("-------------- player module start -----------");
        //Output the current Player's level
        let playerData = this.data;
        Console. log ("Player level:", playerData. getlevel());
        playerData.onDataChange.add(() => {
            //When Player data changes, output the current Player's level
            Console. log ("Player level:", playerData. getlevel());
        })
        InputUtil.onKeyDown(Keys.F, () => {
            this.server.net_LevelUp();
        })
    }

    protected onEnterScene(sceneType: number): void {
        Console. log ("------------ player module enters the scene ------------");
    }

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

    protected onDestroy(): void {
        Console.log ("--------------- player module destroyed ------------");
    }

}
class PlayerModuleS extends ModuleS<PlayerModuleC, PlayerModuleData>{
    //Player upgrade
    public net_LevelUp(): void {
        //The client Player who call the function
        let player = this.currentPlayer;
        //Player id of the client that call the function
        let playerId = this.currentPlayerId;
        //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 ModuleCExample extends Script {

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

}

class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{

    protected onAwake(): void {
        Console.log ("-------------- player module creation module ----------");
    }

    protected onStart(): void {
        Console.log ("-------------- player module start -----------");
        //Output the current Player's level
        let playerData = this.data;
        Console. log ("Player level:", playerData. getlevel());
        playerData.onDataChange.add(() => {
            //When Player data changes, output the current Player's level
            Console. log ("Player level:", playerData. getlevel());
        })
        InputUtil.onKeyDown(Keys.F, () => {
            this.server.net_LevelUp();
        })
    }

    protected onEnterScene(sceneType: number): void {
        Console. log ("------------ player module enters the scene ------------");
    }

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

    protected onDestroy(): void {
        Console.log ("--------------- player module destroyed ------------");
    }

}
class PlayerModuleS extends ModuleS<PlayerModuleC, PlayerModuleData>{
    //Player upgrade
    public net_LevelUp(): void {
        //The client Player who call the function
        let player = this.currentPlayer;
        //Player id of the client that call the function
        let playerId = this.currentPlayerId;
        //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 ​

data(): S
Module data of local Player
localPlayer(): Player other
Get the current Player
localPlayerId(): number other
Get the current player ID
server(): T other
The server module bound with itself can directly call the server method starting with net_ through this object

Methods ​

onAwake(): void other
Life cycle method - call when create module
onDestroy(): void other
Life cycle method - Destroy Module call
onEnterScene(sceneType: number): void other
Life cycle method - enter the scene call
onExecute(type: number, ...params: any[]): void other
External call an operation of this module
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 ​

data ​

• Protected get data(): S

Module data of local Player

Returns ​

S

localPlayer ​

• Protected get localPlayer(): Player other

Get the current Player

Returns ​

Player

localPlayerId ​

• Protected get localPlayerId(): number other

Get the current player ID

Returns ​

number

server ​

• Protected get server(): T other

The server module bound with itself can directly call the server method starting with net_ through this object

Returns ​

T

Methods ​

onAwake ​

• Protected onAwake(): void other

Life cycle method - call when create module


onDestroy ​

• Protected onDestroy(): void other

Life cycle method - Destroy Module call


onEnterScene ​

• Protected onEnterScene(sceneType): void other

Life cycle method - enter the scene call

Parameters ​

sceneType numberScene type (reserved) range: type:

onExecute ​

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

External call an operation of this module

Parameters ​

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

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