Table Realms emits a Playcanvas event when a player connects to the game.
You can subscribe this event TR.new_player.
Read more about Playcanvas events [in the next section].
The TR.new_player event contains a playerId. This is an auto-generated id that identifies the player for the session.
Note: Table Realms emits other events too, such as
TR.new_player,TR.player_disconnectandTR.ready.
You can find out more about PlayCanvas event handlers and listeners on the PlayCanvas forum here.
To detect whether a player has joined the game, place the following script into the game.js file within PlayCanvas:
Spawning.prototype.initialize = function() {
var self = this;
this.app.on("TR.new_player", function (newEntity) {
newEntity.enabled = 'true';
self.app.root.addChild(newEntity);
self.app.root.findByName('QRImage').enabled=false;
newEntity.setPosition(0, 2.5, 3);
});
This code detects when a player joins the game by scanning the QR code. It then adds an entity for the player, and sets the entity’s position on the grid. Thereafter, the QR Image is disabled.