Reference room by room ID

24 Apr 2018, 19:25

Hi! I was wondering, is it possible to reference a room from room ID, say send message to all clients in room?
I have different classes for state, entites, players etc which don't have reference to the parent room class and therefore can't use 'send' method within those classes.

Thanks!

24 Apr 2018, 19:37

Hi @veggis,

You may store the room reference on those specific entities by using @nosync.

Example:

import { Room, Client, nosync } from "colyseus";

export class Player {
  x: number;
  y: number;

  @nosync client: Client;
  @nosync room: Room;
  
  constructor (client: Client, room: Room) {
    this.client = client;
    this.room = room;
  }

  sendSomething (data: any) {
    this.room.send(this.client, data);
  }
}

This way, the client and room properties won't be synched to the client-side.

Hope this helps. Cheers!

24 Apr 2018, 19:46

This is exactly what I was looking for! Thanks alot @endel

26 Apr 2018, 13:11

@endel Sorry to bother you, but could you give an example with js?

I'm using babel to transcribe decorators, which is working but not for variables.

26 Apr 2018, 18:17

After some digging around in the docs I got it. Defining objects non-enumerable does the trick.

11 May 2018, 14:15

@veggis have you a sample in JS with non enumerable property?

13 May 2018, 09:52

@lezanardi

function setEnumerable(object) {
    let private = {};

    for (let key in object) {
        Object.defineProperty(private, key, {
            value: object[key],
            enumerable: false,
            writable: true
        });
    }

    return private;
}

Will return the passed object with all the items non-enumerable.