Sharing scene observables in a multiplayer game

Happy Thanksgiving!

I’m working on a multiplayer immersive experience (similar to https://hubs.mozilla.com/) but written in Babylon.js, where anyone with a browser can join into a virtual “room” and hang out in a 3D environment.

What I have so far is the ability to create a room, join a room, hear each other over webrtc (share video camera if you are on a desktop), move around and explore the space, teleport in vr, and see each other’s avatar movements. I have setup web-sockets and event emitters that broadcast player active camera position and rotation with a debounce of 100ms.

I want to go a step further and be able to load and unload different “games” on top of the room that the members can play together. For example maybe paint ball or an escape room etc, by changing the scene environment and game state.

The aspect that I’m trying to figure out now is finding a good mechanism to recreate the game that’s already in progress for the next joining member. Since new players are allowed to join a room at any time, they need a way to receive the current state, which not only includes the meshes, lights in the scene, but also (and this is the part I’m struggling with) the logic, the behaviors, the observers that have been added to objects that are present in the scene that act on and capture player activity.

For example:

If we have player A and player B, and they join the room at the same time we can start them off with the same initial game state. Then any action A or B takes will send a message to each other and they update their internal state to remain in sync.

The challenge I have is if A and B have been playing for a while, their game may have needed to add or remove observables and behaviors (doors that open when they observe a button is clicked, drag and drop when something is picked, grab and throw when mesh is intersected, gravity on/off, fire bullets, unsubscribe to certain observables after actions have taken place), and then when player C joins at a much later time, we now need to give C a working game with the same observerables attached and working up to this point.

I find this challenging because the game state is not merely the meshes, lights in the scene (which can be easily serialized and shared), but also all the behaviors and logic we attach and remove on various things as the game progresses.

I’m not sure if what I’m asking makes a lot of sense… but are there known techniques and approaches to dealing with this sort of problem?

Unfortunately there is no easy solution for that. The two best that come to my mind are:

  1. Keeping track on you side of a current Functional state that you could share at any time to be in the exact same condition.
  2. Keeping track of all actions to be able to replay them in order to arrive in the same state.
1 Like

Thanks. I’ve basically implemented the former. I keep track of the names of functions that are used to build things and attach behaviors. When a new player joins I send them this list of strings and those functions of the same name are executed on the other side to get to the same state mesh-wise and observer-wise.

1 Like