Multipeer Connectivity and data to send along the way
Asked Answered
A

2

8

I am developing a platform game trying to make it as well for multiplayer using the IOS Multipeer connectivity. I am stuck/confused/do not know what is the best way to send messages between peers (mostly like 4 peers in the game). The game style is like fun run and I have 4 hero running and firing each other and stuff like that. The thing is that keeping the positions of the other players is pushing me crazy!! Sometimes other players are positioned in the wrong position (keeping in mind all devices are with the same screen size). Is that because of the lag of connectivity. How much information I can send per seconds and what is the best way to manage data transferring?

Any idea/thoughts are appreciated.

Ashling answered 9/8, 2015 at 20:40 Comment(1)
Do you have stats on how many packets you are trying to send from each device/second? How big are your packets? Are you processing your MPC packets on a background thread and then applying the updates to your UI on the main thread?Tjon
T
4

The throughput of MPC depends mostly upon the connection. What I've seen as the biggest performance problem is when one of the peers has WiFi disabled. This forces MPC to communicate over bluetooth which is incredibly slow. I've seen it run 20x slower than MPC over Wifi.

You don't need to be connected to a WiFi network. Just enable WiFi on all of the devices and iOS will leverage shared WiFi networks or create its own adhoc network.

The second thing you want to ensure is that your peers don't invite each other. You can only have one inviter in your network. When multiple peers invite and accept connections the MPC network become unstable.

Third thing is sendData:(NSData *)data toPeers:(NSArray *)peerIDs withMode:(MCSessionSendDataMode)mode error:(NSError **)error. If you are broadcasting your packets to all peers in the toPeers: array AND mode is MCSessionSendDataReliable then MPC waits until all of the connected peers ACK the message before moving on to the next packet.

UPDATE: I did some testing with my own app and over WiFi and two devices I can put about 100kbps. I'm using an iPhone 6 Plus and an iPhone 5S.

UPDATE 2: Thinking more about your question, there are a couple things to keep in mind with MPC communications:

  • Do all of your sendData and didReceiveData calls on a background thread and have that thread update your position data in your model. Tell your viewController that updates are available with a delegate method or notification.

  • Keep your data packets small, but design them so an update received represents the current state of your player. This allows you to miss an update and not be completely out of sync -- "send player 1 moved to (10, 10)" instead of "player 1 moved by (1, -1)"

  • Number your packets and use MCSessionSendDataUnreliable. If you get a packet with an earlier number than the last one you processed, throw it away. If you follow the second guideline above, you won't need this packet.

Tjon answered 14/8, 2015 at 17:56 Comment(3)
Appreciate your reply, but It still not solving my problem of misplaced heros in the game. I am already connected using WiFi and using only one inviter.Ashling
Dear Dan, I already did the numbering to get the latest update from the other players. You see I am only sending two integers and two double numbers presenting the position and the player state (hit- fire ..), in the two players only every thing works fine knowing that I am sending the position every 0.1 seconds. However in the four players as I am trying to get. Things missed out!! Knowing that each player send its own data to every body else. I am only using "didreceivedata" from the MPC DELEGATE METHOD!! I am out in the mountains right now. But I will try to post some codes later to clear UAshling
When The game starts say three players A, B, and C. Player A moves All 3 players to right by 50 pnts each 0.1 sec. If Player B got hit then player B send its position and stateHit to other players along with its position. The same thing for other states "fire, jump, grenade, ...". Now sending the position is troubling me because I do not know when to send the position. Do I send it 60 times/frame or maybe send it once each 0.1 sec as the movement? I got stuck with this. The problem is not the size of the packet, but when to send it?Ashling
E
2

If you follow Dan Loughney's suggestions, I think your best bet in debugging is to log the received coordinates from the other players. That should help you identify whether the problem is due to timing issues, misinterpreted data or something else.

Expert answered 18/8, 2015 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.