GitHub | Documentation | Become a Patron

Listen to non-primitive variables



  • Hey!

    I have a question regarding the .listen method for coloseus.js client.

    So, the thing is, that I send the current position & rotation to the server, which looks something like this:

    serverRoom.send({
        action: 'entity:transform:update',
        detail: {
            id: mesh.id,
            transformMatrix: [
                meshTransform.position.x,
                meshTransform.position.y,
                meshTransform.position.z,
                meshTransform.rotation.x,
                meshTransform.rotation.y,
                meshTransform.rotation.z,
                meshTransform.rotation.w,
            ],
        },
    });
    

    my room's state looks something like this:

    public onInit (options) {
        this.setState({
            entities: {}, // key_id: { id: 1, transformMatrix: [0,0,0,0,0,0,0] }
        });
    }
    

    Now, I want to listen for changes of other entities like that:

    serverRoom.listen('entities/:id/transformMatrix', (change: DataChange) => {
    // listen for changes
    });
    

    But that doesn't seem to work (it only fires for the first add operation & the remove operation; It doesn't ever fire the replace operation), if the transformMatrix variable is an array. My temporary workaround is, to set it to a string, and also send the transformMatrix as a string.

    Is there a way I could get the array inside the serverRoom.listen() method?


  • administrators

    Hi @bobalazek,

    This section needs more documentation and examples indeed.

    You won't receive "replace" operations on "entities/:id/transformMatrix" this way because the listen() checks for the exact location of the variable, and it won't go deeper.

    You can listen to arrays like this:

    serverRoom.listen("entities/:id/transformMatrix/:index", (change: DataChange) => {
      console.log(change.path.index + " is now " + change.value);
    });
    

    Hope this helps! Cheers!



  • Ok, thank you. Oh yeah, I did actually figure it this way, but in this case, I'm worried about performance, as it would need to do 7x (10x, if I would also include the scale) more callback calls as if it would just return the array. So the string solution does work ok (.split('|')/.join('|')), but it sends/receives more bits of data.
    Mean, for now, there is no way to hack around, so I would be able to get an array in "one batch"?