Hi, I’m using Photon for my multiplayer game, where every time a player joins the room, their model or entity changes. However, it seems that only the ‘other player’ has their model or entity changed. Player 1’s model or entity does not change, even though its index updates.
PhotonLoadBalancingPlayCanvas.prototype.initialize = function () {
// Initialize Photon client
this.loadBalancingClient = new Photon.LoadBalancing.LoadBalancingClient(this.wss ? 1 : 0, this.appId, this.appVersion);
this.loadBalancingClient.app = this.app;
// Connect to Photon
if (!this.loadBalancingClient.isInLobby()) {
this.loadBalancingClient.connectToRegionMaster(this.region);
}
// Event listeners for Photon
this.loadBalancingClient.onRoomList = this.onRoomList.bind(this);
this.loadBalancingClient.onJoinRoom = this.onJoinRoom.bind(this);
this.loadBalancingClient.onActorJoin = this.onActorJoin.bind(this);
this.loadBalancingClient.onActorLeave = this.onActorLeave.bind(this);
this.loadBalancingClient.onEvent = this.onEvent.bind(this);
// PlayCanvas events
this.app.on("createOtherPlayerEntity", this.createOtherPlayerEntity, this);
this.app.on("UpdatePosition", this.sendPlayerPosition, this);
// Track Shift key state
this.isShiftPressed = false;
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
// Avatar list
this.playerEntitiesToClone = [
{ player1: this.app.root.findByName("Avatar1"), player2: this.app.root.findByName("Avatar1Clone") },
{ player1: this.app.root.findByName("Avatar2"), player2: this.app.root.findByName("Avatar2Clone") },
{ player1: this.app.root.findByName("Avatar3"), player2: this.app.root.findByName("Avatar3Clone") },
{ player1: this.app.root.findByName("Avatar4"), player2: this.app.root.findByName("Avatar4Clone") },
{ player1: this.app.root.findByName("Avatar5"), player2: this.app.root.findByName("Avatar5Clone") },
{ player1: this.app.root.findByName("Avatar6"), player2: this.app.root.findByName("Avatar6Clone") },
{ player1: this.app.root.findByName("Avatar7"), player2: this.app.root.findByName("Avatar7Clone") },
{ player1: this.app.root.findByName("Avatar8"), player2: this.app.root.findByName("Avatar8Clone") }
];
if (this.playerEntitiesToClone.some(entry => !entry.player1 || !entry.player2)) {
console.error("Error: One or more avatars are missing from the scene.");
return;
}
// Set initial avatar index for the first player
this.selectedAvatarIndex = 1; // Start with Avatar1
this.selectedAvatar = this.playerEntitiesToClone[this.selectedAvatarIndex];
};
PhotonLoadBalancingPlayCanvas.prototype.onRoomList = function () {
this.loadBalancingClient.joinRandomOrCreateRoom();
};
PhotonLoadBalancingPlayCanvas.prototype.onJoinRoom = function (createRoom) {
this.loadBalancingClient.myRoomActorsArray().forEach((actor) => {
if (actor.isLocal) return;
this.app.fire("createOtherPlayerEntity", actor);
});
// Increment the avatar index for Player 1 when they join (check if they created the room)
if (createRoom) {
// If Player 1 is creating the room, increment the avatar index
this.selectedAvatarIndex = (this.selectedAvatarIndex + 1) % this.playerEntitiesToClone.length;
this.selectedAvatar = this.playerEntitiesToClone[this.selectedAvatarIndex];
}
// Clone selected avatar for Player1
if (!this.selectedAvatar.player1) {
console.error("Error: Selected player entity not found.");
return;
}
const entity1 = this.selectedAvatar.player1.clone();
entity1.enabled = true;
this.app.root.addChild(entity1);
const { x, y, z } = entity1.getPosition();
const { x: rx, y: ry, z: rz } = entity1.getEulerAngles();
this.sendPlayerPosition({ x, y, z, rx, ry, rz });
};
PhotonLoadBalancingPlayCanvas.prototype.onActorJoin = function (actor) {
// Increment the avatar index for every player who joins
if (!actor.isLocal) {
this.selectedAvatarIndex = (this.selectedAvatarIndex + 1) % this.playerEntitiesToClone.length;
this.selectedAvatar = this.playerEntitiesToClone[this.selectedAvatarIndex];
}
if (actor.isLocal) return;
this.app.fire("createOtherPlayerEntity", actor);
const { x, y, z } = this.selectedAvatar.player1.getPosition();
const { x: rx, y: ry, z: rz } = this.selectedAvatar.player1.getEulerAngles();
this.app.fire("UpdatePosition", { x, y, z, rx, ry, rz });
};
PhotonLoadBalancingPlayCanvas.prototype.onActorLeave = function (actor) {
const { actorNr } = actor;
const otherPlayer = this.app.root.findByName(actorNr);
if (!otherPlayer) return;
otherPlayer.destroy();
};
PhotonLoadBalancingPlayCanvas.prototype.createOtherPlayerEntity = function (actor) {
const { actorNr } = actor;
if (!this.selectedAvatar.player2) {
console.error("Error: Cloned player entity not found.");
return;
}
const entity2 = this.selectedAvatar.player2.clone();
entity2.enabled = true;
entity2.name = actorNr;
this.app.root.children[0].addChild(entity2);
};
PhotonLoadBalancingPlayCanvas.prototype.sendPlayerPosition = function (transform) {
// Raise event for position update
this.loadBalancingClient.raiseEvent(1, transform);
// Check if the player is walking (based on position change)
const isWalk = transform.x !== this.lastPosition?.x || transform.z !== this.lastPosition?.z;
this.lastPosition = { x: transform.x, z: transform.z };
// Check if the player is running (based on Shift key press)
const isRun = this.isShiftPressed;
// Raise event for walking/running state
this.loadBalancingClient.raiseEvent(2, { actorNr: this.loadBalancingClient.myActor().actorNr, isWalk, isRun });
// If not walking and not running, explicitly set isWalk and isRun to false
if (!isWalk && !this.isShiftPressed) {
this.loadBalancingClient.raiseEvent(2, { actorNr: this.loadBalancingClient.myActor().actorNr, isWalk: false, isRun: false });
}
};
PhotonLoadBalancingPlayCanvas.prototype.onEvent = function (code, content, actorNr) {
switch (code) {
case 1: {
const otherPlayer = this.app.root.findByName(actorNr);
if (otherPlayer) {
const { x, y, z, rx, ry, rz } = content;
otherPlayer.setLocalPosition(x, y, z);
otherPlayer.setEulerAngles(rx, ry, rz);
}
break;
}
case 2: {
this.app.fire("playWalk", content.actorNr, content.isWalk);
this.app.fire("playRun", content.actorNr, content.isRun);
break;
}
}
};
PhotonLoadBalancingPlayCanvas.prototype.onKeyDown = function (event) {
if (event.key === pc.KEY_SHIFT) {
this.isShiftPressed = true;
}
};
PhotonLoadBalancingPlayCanvas.prototype.onKeyUp = function (event) {
if (event.key === pc.KEY_SHIFT) {
this.isShiftPressed = false;
}
};
player 1 does not change it model/entity