Nestesd states not updated on client (no listen event triggered)

Hi,

I dont understand how nested states needs to be changed on server side to trigger the listen event on client side.
I would like to use following classes as custom states.

Room

...
onInit() {
   this.state.setState(new GameState());
}

GameState

export class GameState {
  players: EntityMap<PlayerState> = {};
...

PlayerState

export class PlayerState {
  tilePosX: number = 0;
  tilePosY: number = 0;
  isNpc: boolean = false;
  moveDirState: MoveDirState = MoveDirState.NONE();

MoveDirState

export class MoveDirState {
  moveDir: MOVE_DIR = MOVE_DIR.NONE;
  time: number = 0;

Problem is that any change to PlayerState.moveDirState does not trigger listen events on client. Even that moveDirState is initially send to client

// Initial update with moveDirState as attribute
{ operation: "add",  attribute: "moveDirState" , value: { moveDir: 4, time: 0} ... }

any update on serverside (this is a player instance)

// Not working tries
// 1 Not working helper to set values inside the class
// this.moveDirState.set(moveDir, time);
// 2 Not working set attributes from player class
this.moveDirState.moveDir = moveDir;
this.moveDirState.time = time;
// 3 Not working creating new instance
this.moveDirState = new MoveDirState(moveDir, time);

// Just to check if update working, this is working
this.tilePosX -= 1;

Listener on client side looks like this.


this.room.listen("players/:id/:attribute", (change: DataChange) => {
   console.log("Update state for " + change.path.id, change);
   ...
});

Thank you very much

Hi @trenrod, welcome!

When listening for changes, you usually need to provide the full path of the variable you're interested in.

In your example ("players/:id/:attribute"), it will listen for additions and removals of the whole moveDirState reference, and not it's children.

To get more data from the moveDirState variable, you can listen for it like this:

this.room.listen("players/:id/moveDirState/:prop", (change: DataChange) => {
   console.log("Player " + change.path.id, " update on moveDirState:", change.path.prop, "changed to", change.value);
   ...
});

Hope this helps! Cheers!