Room properties blank

Just starting out with the Javascript SDK in Playcanvas. I would like to use room properties to communicate damage/pickups for scene objects so that clients joining later get all the same info.

If I join a room with two players in separate browsers, when I test setting custom properties on the current room with this:
this.photon.currentRoom.setCustomProperty(id, amount);
(id is the predetermined scene object's id, amount is how much damage is being applied)

Both players receive a response from this:
this.currentRoom.onPropertiesChange = function(change, byClient) {
console.log("onPropertiesChange:", change, byClient);
};

However, in onPropertiesChange the change variable is populated with the correct info on the player that did the damage, while on the other player the change object is empty. I also tried debugging currentRoom._customProperties and it had the same problem (data populated on player that did it, blank for other player).

Is this expected behavior? Should I be using some other method?

Comments

  • What is the type of 'id' parameter used as name of custom property?
    It should be string as stated in the doc https://doc-api.photonengine.com/en/javascript/current/Photon.LoadBalancing.Room.html#setCustomProperty
  • The id being passed is a string, just to be sure I tried this:
    this.photon.currentRoom.setCustomProperty(id.toString(), amount);

    and this:
    this.photon.currentRoom.setCustomProperty(id.toString(), amount.toString());

    Same results. I'm wondering if it matters when I override the onPropertiesChange? I'm currently doing it in onActorJoin:
    this.photonEntity.photon.onActorJoin = function(actor) {
                if(actor.actorNr == this.myActor().actorNr) {                                      
                    this.currentRoom.onPropertiesChange = function(change, byClient) {
                        console.log("onPropertiesChange:", change, byClient);
                    };
                    game.ShowPhotonUI(false);                
                    game.app.fire('OnLocalPlayerJoined', photon, actor, localPlayerEntity, this.myActor().actorNr);
                }
                else {                
                    game.app.fire('OnRemotePlayerJoined', actor, remotePlayerEntity, actor.actorNr);
                }
            };
    
  • vadimvadim mod
    edited October 15
    When you assign onActorJoin , the room is not joined yet probably and this.currentRoom references wrong room (default local room).
    Photon API does not have 'currentRoom' property. Instead it provides myRoom() method https://doc-api.photonengine.com/en/javascript/current/Photon.LoadBalancing.LoadBalancingClient.html#myRoom
    Try it.
  • Gave these two a try:

    this.myRoom().onPropertiesChange = function (change, byClient) {
    console.log("OnPropertiesChange", change, byClient);
    };

    actor.getRoom().onPropertiesChange = function(change, byClient) {
    console.log("onPropertiesChange:", change, byClient);
    };

    I get the same result with both; change object is populated on client that did it, change object is empty on the other client.

    To check if I'm getting the right room I also logged what the room is inside onActorJoin and it seems right, both clients show the same room name and when the 2nd client joins it shows 2 players in the room properties for both clients.

    This is all based on an example I found here: https://github.com/utautattaro/Photon-for-PlayCanvas
    I did use the latest SDK, but maybe what's in demoloadbalancing.js is just too far out of date and missing something..
  • When do you assign this.myRoom().onPropertiesChange? It should be done after joining the room. Doing so in onActorJoin as in your previous post is ok but onJoinRoom is better.
    Assigning photon client onActorPropertiesChange property is more convenient because can be done any time, e.g. when client is initialized. Photon-for-PlayCanvas does this via prototype but if you work with client instance, assign instance field directly.
    https://doc-api.photonengine.com/en/javascript/current/Photon.LoadBalancing.LoadBalancingClient.html#onActorPropertiesChange
  • I was able to figure out the problem, sorta.. more of a theory on what was happening.

    I was doing this:
    setCustomProperty(id.toString(), amount);

    id was a single digit, so I was passing "1" into setCustomProperty as the name. Somewhere along the line this was getting converted and lost on the other client. I didn't dig too deep into the SDK but I suspect it's just going from string to float in there as the single digit.

    Anyways, I did a simple concat to make it "id1" instead of "1" and now it's working :]
    Thanks for the help though.
  • Interesting finding. This happens probably because of limited type support by javascript. Photon forced to guess if the dictionary's key is a number or a strings.
    So never use anything that can be converted to a number.
Sign In or Register to comment.