It’s finally done. It was a frustrating pain in the butt, but mostly the issues came from my own lack of understanding about how multiplayer works.

Let’s get techy.

When you connect to a server, you’re handed a unique id. Let’s call you 12345. When another player connects to the server, they’re handed a unique id also. Let’s call player 2 54321.

The server knows that when you join the game, you need to have a player object spawned for you, so it plops you into the game. The server then reaches out to the player object and says “Hey you, you can only be controlled by 12345.”

Then the server reaches out to the client and says “Just so you know, something spawned, so you’re gonna need to spawn it on your side too so that your player can see it.”

Then when player 2 joins, the server again knows that a player object needs to spawn for player 2. After it spawns the player object, it reaches out to the player object and says “Hey you, you can only be controlled by 54321.”

Then the server reaches out to both clients and says “Just so you know, something spawned, so you’re gonna need to spawn it on your side too so that your player can see it.”

Something’s Missing…

When the server reaches out to the clients, it just tells them “Hey, spawn a thing.” It never tells them who they’re allowed to be controlled by. Since the server never tells them, they both assume they can be controlled by 0, which is nobody.

Well… I didn’t know that was happening, so I wrote some code to account for it… code that didn’t even need to exist. That code was instead telling everyone that they were allowed to control everything.

Once I figured out that the newly spawned objects didn’t know who they were allowed to be controlled by, it was fairly simple to hand that information from the server to the client. It’s a bit of a hack, and I don’t like it, and I feel like there should be a better way, but it works, and it’s the solution that’s recommended by basically everyone.

Once the ids were getting populated properly, I deleted my crappy code which is allowing everyone to control everything, and voila! It all works!

  • There’s a dedicated server running for players to connect to
  • Player 1 can connect to it
  • Player 1 can move their little guy left and right
  • Player 2 can connect to it
  • Player 2 can move their little guy left and right
  • The players are not able to move anyone else’s players
  • All of the clients are synchronized, so movement works on all clients including the server
  • The server doesn’t have the ability to control anyone

This was the foundation for everything else. Getting this to work was always going to block everything else.

Now I have to make sure I design all of my future work around multiplayer, which requires a bit of a paradigm shift….

But it works.

Now I can move on.