u/lifeinbackground

▲ 2 r/bevy

How to synchronize two peers in a locksteep-based multiplayer

Hello. This questions is not specific bevy, but I'm not sure where else to ask.

I'm building 1v1 multiplayer game in bevy for browsers (WebRTC). The purpose of this project is to learn game networking a bit, that's why I'm not using crates like bevy_ggrs.

In my understanding of locksteep netcode, you basically have wait until inputs from both clients arrive (i.e. both peers ready to proceed to the next tick) and buffer those inputs for a few ticks to avoid freezing the game. That's what I understand from gafferongames' "Deterministic Lockstep" article. This is quite understandable.

What I don't understand is how do you synchronize two peers to start at the same time (tick). It seems impossible to me.

My current flow is to have a lobby (2 players), where one peer is marked as leader, the other as follower. Everyone knows who is who.

  1. Starting in NotReady state, both peers send 'readiness' packet to each other.
  2. After receiving readiness from the other peer, each peer transitions to Ready.
  3. When Ready, the leader schedules a start by sending a 'start tick' to the follower (current_tick + 30). Then goes to StartScheduled state.
  4. The follower receives the start ticket from the leader, saves it, transitions to StartScheduled and also waits.
  5. When the start tick is reached by both sides (tick advances in FixedUpdate), the game naturally starts.

This is very wrong. There is no guarantee about the current_tick being the same for both peers. And it seems like the 'Chicken or the egg' problem to me.

So how do you synchronize peers initially so they stay (hopefully) synchronized in future by using only deterministic systems?

reddit.com
u/lifeinbackground — 24 hours ago