Skip to content
PlayerState

[Character System](../groups/Character System.Character System.md) / PlayerState

PlayerState Class

PlayerState base class

Usage example: create a script named "PlayerStateExample", place it in the object bar, open the script, and enter the following code to save it. Change the number of Player in the startup parameter to 2, run the game and press the R key to see one of the clients receive test sync. Press the P key to print the test value of the client

ts
// Each Player on the server will automatically create an instance when entering the game
  export class GamePlayerState extends mw.PlayerState {

      @Property({replicated: true, onChanged: "onRepTest"})
      test = "";

      onRepTest(path: string[], value: string, oldVal: string) {
          console.log(`onRepTest path: ${path} value: ${value} oldVal: ${oldVal}`);
      }
  }

  @Component
  export default class PlayerStateExample extends mw.Script {

      protected onStart(): void {

          // Press R to randomly select a player on the server to modify the test property of GamePlayerState
          InputUtil.onKeyDown(Keys.R, () => this.random());

          // Press P to print the test property of Player GamePlayState on the main control terminal
          InputUtil.onKeyDown(Keys.P, () => {
              const playerState = Player.localPlayer.getPlayerState(GamePlayerState);
              console.log(`test: ${playerState.test}`);
          });

      }

      @RemoteFunction(Server)
      random() {
          const players = Player.getAllPlayers();
          // Random a Player
          const luckPlayer = players[Math.floor(Math.random() * players.length)];
          // Get GamePlayerState instance
          const playerState = luckPlayer.getPlayerState(GamePlayerState);
          playerState.test = `random: ${ Math.floor(Math.random() * 100)}`;
      }
  }
// Each Player on the server will automatically create an instance when entering the game
  export class GamePlayerState extends mw.PlayerState {

      @Property({replicated: true, onChanged: "onRepTest"})
      test = "";

      onRepTest(path: string[], value: string, oldVal: string) {
          console.log(`onRepTest path: ${path} value: ${value} oldVal: ${oldVal}`);
      }
  }

  @Component
  export default class PlayerStateExample extends mw.Script {

      protected onStart(): void {

          // Press R to randomly select a player on the server to modify the test property of GamePlayerState
          InputUtil.onKeyDown(Keys.R, () => this.random());

          // Press P to print the test property of Player GamePlayState on the main control terminal
          InputUtil.onKeyDown(Keys.P, () => {
              const playerState = Player.localPlayer.getPlayerState(GamePlayerState);
              console.log(`test: ${playerState.test}`);
          });

      }

      @RemoteFunction(Server)
      random() {
          const players = Player.getAllPlayers();
          // Random a Player
          const luckPlayer = players[Math.floor(Math.random() * players.length)];
          // Get GamePlayerState instance
          const playerState = luckPlayer.getPlayerState(GamePlayerState);
          playerState.test = `random: ${ Math.floor(Math.random() * 100)}`;
      }
  }

Hierarchy

Table of contents

Properties

Accessors

click

Accessors

gameObject(): GameObject other
Get the gameobject mounted by the script
useUpdate(): boolean other
Get whether script enable onUpdate lifecycle function

Methods

click

Methods

destroy(): void other
Destroy component object
onDestroy(): void other
Lifecycle function - call when destroyed
onReplicated(path: string, value: unknown, oldVal: unknown): boolean void other
The attribute is synchronized with the event Client Only
onStart(): void other
Lifecycle function - called at the beginning of script execution
onUpdate(dt: number): void other
Lifecycle Function - Execute Function per Frame

Properties

Accessors

Methods