Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game Kit Peer to Peer

I coded a bomberman application that uses a gamekit peer to peer connection. The problem is that after a while the game isn't in sync anymore.

I looked at the sample code for GKTanks and used their model. There is no client/server relation between peers so I didn't use one in my game. Both peers maintain a gamestate which they update based on received data.

I have a NSTimer that's used for running the gameloop at each frame. The NSTimers aren't in sync so sometimes the gamestates become different ex: players pick up a powerup at approximately the same time and they both get the powerup because it takes a while to send data.

I would appreciate any idea on making the app work. I'm thinking of rewriting the code to use client-server but I'm not sure if it's a good idea... yet

Thank you!

EDIT: I changed the code such that a random player is picked to be the host. Every time a player places a bomb he asks the server where to place it. The server returns the players position(as seen on the server) and then tells the player where to place the bomb.

For powerups the server checks if a player picked up a powerup and if he did it sends a packet informing him.

Another problem has appeared now. The latency between devices is high(I'm using a bluetooth connection). It takes around 0.2 seconds to place a bomb after the client tapped the button to place it.

I'm sending all data reliably. Am I doing this right?

like image 491
silviupop Avatar asked Oct 10 '22 15:10

silviupop


1 Answers

Well preferbly you want a host-client relationship where only the host can manipulate the game state, so in your case it would be:

Both players rush to the powerup. Host picks it up first.
It gets registered and the host recieves the power-up.
Meanwhile player#2 also picks up the power-up, sends the action to the host.
The host informs player#2 that the power-up is already disappeared.

The thing with your situation you are bound to get desynchs from packet loss.
With host-client relationship that cannot happen, the only problem is the host always has an advantage that becomes greater when the latency increases between devices especially on smartphones.

In a game like bomberman it's perfectly plausible to send the entire gamestate each time something changes instead of the action that was performed, this is to ensure both devices are in sync.

To sum it up: both users have their gamestate but only the host can manipulate both.

like image 198
Rohan Avatar answered Oct 13 '22 12:10

Rohan