Navigation

  • Recent
  • Tags
  • Users
  • Search
  • Login
Colyseus
  • Login
  • Search
  • Recent
  • Tags
  • Users

Documentation GitHub

We're migrating to GitHub Discussions. This forum does not accept new registrations since April 6, 2023.
  1. Home
  2. lars
L

lars

@lars

Chat Follow Unfollow
0
Reputation
1
Posts
635
Profile views
0
Followers
0
Following
Joined 27 Jul 2022, 13:24 Last Online 27 Jul 2022, 15:06

  • Profile
  • More
    • Continue chat with lars
    • Flag Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups
lars Follow

Posts made by lars

Use client-side callbacks like "onAdd" in typescript (solved)

Hello,

is it possible, to use client-side callbacks like "onAdd" in typescript?

In the documentation is no example for typescript: https://docs.colyseus.io/colyseus/state/schema/#onadd-instance-key

I tried the following code on my client-side:

import * as Colyseus from "colyseus.js"
var colyseusSDK = new Colyseus.Client("ws://localhost:2567");
colyseusSDK.joinOrCreate("my_room").then(function (room) {
    room.state.players.onAdd((player, sessionId) => {
    });
});

but I get Property 'players' does not exist on type 'unknown'. as error in my IDE.

On the server side I have the following implementations:

MyRoomState.ts

import { Schema, MapSchema, type } from "@colyseus/schema";

export class Player extends Schema {
  @type("number") x: number;
  @type("number") y: number;
  @type("number") z: number;
}

export class MyRoomState extends Schema {
  @type({ map: Player }) players = new MapSchema<Player>();
}

MyRoom.ts

import { Room, Client } from "colyseus";
import { MyRoomState, Player } from "./schema/MyRoomState";

export class MyRoom extends Room<MyRoomState> {

  onCreate (options: any) {
    this.setState(new MyRoomState());

    this.onMessage("type", (client, message) => {
    });

  }

  onJoin (client: Client, options: any) {
    console.log(client.sessionId, "joined!");

    const player = new Player();

    player.x = 1;
    player.y = 1;
    player.z = 1;

    this.state.players.set(client.sessionId, player);
  }

  onLeave (client: Client, consented: boolean) {
    console.log(client.sessionId, "left!");
    this.state.players.delete(client.sessionId);
  }

  onDispose() {
    console.log("room", this.roomId, "disposing...");
  }
}

posted in Questions & Help • 27 Jul 2022, 13:38

© 2023 Endel Dreyer