Multiplayer problem: entity.destroy() not working if user 2 switched tabs


I have a multiplayer game that has the feature that when object A and object B collide, one is destroyed, leaving only object B.

Before any action, both players see object A and B in the correct place.
If player 1 collides object A and B, player 1 sees them collide and sees A get destroyed.
If player 2 is active, and watches as player 1 collides object A and B, player 2 also sees A get destroyed.
If player 1 collides object A and B while player 2 is inactive (switched away from the tab), when player 2 switches back, player 2 will NOT see that entity A was destroyed. In fact, entity A still exists in the scene.

Debug logs confirm that the destroy() method was called, but it had no effect because it “happened” when the player was not on the tab.

Has this happened to anyone else? Any way to ensure objects get destroyed even if player is not active? My default solution would be to create a “destroy buffer” and only execute the destruction when the user is “active” but this feels pretty hacky … not sure why playcanvas will not allow logic to be executed successfully when player in inactive (but console logs still execute normally)

Another way to describe what is happening: Player 1 executes an event that propagates to server, and NodeJS server propagates that event to all players including player 2. Player 2 was not active when this propagation happened. Debug logs in the target function on player 2’s console still appear in chrome console, but any playcanvas actions like entity.destroy() that were called while player 2 was not on the tab are ignored.

Maybe I just don’t understand how Javascript works?

thank you,


It seems the right way to do this is accept any incoming network calls and add any actions performed in functions targeted there into a buffer, then execute the buffer in the update() loop. If the user is active this will happen within 1 frame, otherwise the app will wait for the user to be active before running the update loop. Placing game logic in the update loop ensures that when the code is received from the server, that it will only execute when the user is active, so the execution will actually affect the app state. Please let me know if you know a better way than this or if you’ve had similar issues and had a different solution. Thanks!!