Navigation

    Colyseus
    • Login
    • Search
    • Recent
    • Tags
    • Users
    1. Home
    2. lars
    L

    lars

    @lars

    Chat Follow Unfollow
    0
    Reputation
    1
    Posts
    261
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    • 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 •