GitHub | Documentation | Discord (chat) | Become a Patron

Listen to state not getting proper data [Unity Client]



  • My state is like this:

    onInit(options)

    this.maxClients = 5;
    this.minClients = 3;
    
    this.setState({
      players: {},
      apid: [],
      dpid: [1, 2, 3, 4, 5],
    }); 
    

    onJoin(client, options) {

    var id;
    id = this.state.dpid.shift();
    this.state.apid.push(id);
    
    this.state.players[client.sessionId] = {
      playerId: id,
      cards: [],
      state: 1,
      pack: 0,
      trickData: {
        Rank: 0,
        Sum: 0,
        c1: 0,
        c2: 0,
        c3: 0
      }
    }
    

    C# code for listen to state change is:

    room.Listen("players/:id", OnPlayerChange);
    //room.Listen("players/:id/:playerId", OnPlayerIdChange);
    //room.Listen("players/:id/:pack", OnPlayerPack);
    room.Listen("players/:id/:cards/:number", OnCardsChange);

    Problem is when palyersId change the OnPlayerIdChange and OnPlayerPack both getting the this data of state

      trickData: {
        Rank: 0,
        Sum: 0,
        c1: 0,
        c2: 0,
        c3: 0
      }
    

    i want to listen to only playerId and pack when this variable changes


  • administrators

    Hi @hardik_sl! When you specify a segment starting with :attrName in the listeners, it will actually match any attribute, and assign the attribute name in the change.path.attrName variable.

    To listen for a particular value, you can specify it directly, without starting with :. Example:

    room.Listen("players/:id/playerId", OnPlayerIdChange);
    room.Listen("players/:id/pack", OnPlayerPack);
    

    BTW, I see you have an array of cards there. I wouldn't recommend mutating arrays in the room's state, unless you use them as a stack - and just push and pop on them. It's very tricky to keep the client state correct when removing or inserting values in-between the array.

    Hope this helps! Cheers!



  • @endel thank you. and i am just using cards array for push and pop only i am not mutating and thanks again for help.