I am trying to make a simple two player snake game that uses gameKit to send movement information back and forth. I start the game out with the server sending a command to start the game. Once the game starts a timer fires every .5 seconds that moves the snakes. No matter what I do I end up with the timers on the two iPhones firing at different times. Does anyone have any suggestion or advice for a solid way of synching the information between the devices?
Thanks in advance for your help!
What I finally went with was to have one device do all the number crunching and send the other device information on where its snake (it was a snake game) should be instead of having both devices calculate where there snakes should be and sending that info to the other device. That way led to syncing issues 100% of the time.
A quick search for network game synchronization techniques yielded plenty of results... Each game is unique, I imagine you could adapt some of the techniques from these articles to help:
http://www.mine-control.com/zack/timesync/timesync.html
http://www.softlookup.com/tutorial/games/ch17.asp
http://www.flipcode.com/archives/Network_Game_Programming-Issue_07_I_bent_my_Wookie.shtml
There were even a few IEEE articles if you have access to them.
Related
I am new to networking over iOS and i am having a lot of difficulty in finding a good tutorial to learn about it. I am making a game and implemented it for a single player but now i have to make it multiplayer. What i have to do on server is only comparing the final score of the two players playing the game and display the message that who won the game. There are no asynchronous calls required the final score is only needed when the timer ends and reach 0. Please help me with for a kick start, really need it. I have been scratching my head over bonjour but unable to understand it as i am very new to networking.
Multipeer Connectivity will work if all players are in close proximity or on the same network. However since you did not specify that those conditions would necessarily be met, you probably want to look at the Game Center networking functionality at https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Matchmaking/Matchmaking.html
First, I will confess that I haven't attempted this in code, so I apologize if this is jumping the gun. I realize that adequate effort and research are a prerequisite to posting good questions. But after reading GameKit and Game Center documentation, as well as iOS Developer Cookbooks, I can't seem to find the right functionality in GameKit and was wondering how I might venture outside it API-wise to solve this problem.
At the simplest level, I would like to implement a time-limited two-player game that does not shut down after one player disconnects, accidentally or maliciously (I currently make no distinction and do not know if I technically can). Essentially, once a match is made, and the two-player match begins, I want the match to continue until one player wins, or match time (say, 10 minutes) expires.
The thing that I want advice on is how to keep a match going when one player disconnects. (Currently, both players disconnecting simultaneously and prematurely leads to immediate termination of the match and no visible result, so no problem there.) How do I set the iOS client to select an action randomly if the user is disconnected? Do I need this program to run on the server, or on both iOS clients? Is it best done in Objective-C to match the iOS applications, or should it be server-side code in some other networking language, like PHP? Are there any APIs that handle this behavior well? On each turn, much like rock-paper-scissors, players have a one-minute time limit to select an action like attack or defend, and if the timer expires without a selection, the computer program randomly selects an action for the player. I'd like to treat a disconnected player the same way: If he or she isn't connected to the match to supply the choice of action, then the computer will do it randomly for him or her at the end of a minute, as if he or she were connected. My purpose for keeping matches online like this is to (a.) ensure that players that disconnect shortly before losing will be assessed a proper loss, (b.) players that stay online in good faith will be assessed their deserved win, and (c.) players that disconnect accidentally or haphazardly can still salvage their match by returning in a timely fashion. I'm sure lots of online games do this, but I just haven't found the proper API or past example for iOS, or two players connecting on their iPhones. Game Center seems to drop the match, or in turn-based games, there isn't some enforceable turn timer (to my knowledge); I didn't see a way to tell who disconnected in the documentation, and I also didn't see some obvious way to keep something online in the case of failure, but with a timer continuing to run. Everything in peer-to-peer seemed to simulate a client-server relationship (one peer the client, one the server), where if the server unit disconnects, the match must end, and if the disconnected player reconnects, the match cannot resume.
In short, I will not object to a code solution, but even some pointers or advice about a framework that supports this behavior will be very much appreciated. Thank you!
Did you consider turn-based matches with timeouts? If a remote player does not respond (times out) control could return to the previous player, which could then conduct intermediate random moves as and while necessary. The application of timeouts for similar scenarios is e.g. discussed in a WWDC 2013 session video.
If you've also implemented a mode where the user can start a game with a computer player locally, then it would be trivial to take any GameCenter game that's timed out and reload it as a local game against a UI.
My assumption would be that once you've implemented a local game with an AI whereby you're saving turns locally each time a player acts. Further, I'm assuming you would use the same data format for both types of games (perhaps changing a bit that says "computer player" instead of "remote player").
When the GC player times out, you code your game to swap out one type of player for another. Register in GC as a win/timeout, and allow the player to continue with the local/AI player.
I've made an iOS game that works over both Bluetooth and bonjour. It works great both ways, but I'd really like it if players could play with eachother with any Internet connection, be it edge, 3G, or different wi-fi addresses.
The catch: I don't have the resources or knowledge to set up dedicated servers or anything like that. It seems that surely GameKit has something in it that will let me do this, but despite spending dozens of hours over the last week figuring out how to use it, I've found nothing.
So, does anyone have ideas for solutions? My game is just one on one, if that makes it any easier.
Why don't go through the Game Center with GameKit?
Apple provides all the infrastructure to play all around the world via their servers (in 3G or Wifi). With the Game Center you can play up to 4 players (so in your case there is no problem), it's free, you don't have to manage any server, there is a mechanism of invitation between friends, and many more.
The logic of your game stay in the client side, so it'll be same design as your actual game (expect for the API I think).
You can find all information on the official documentation here.
If you want, there is a very tuto on the ray wenderlich's blog (but it uses the cocos2d framework instead of the CocoaTouch).
Yep, try GameCenter with GKMatch - works really well if game doesnt need to send tons of data. If you make additional layer between your app and GKMatch and GKSession, you can use both transparently (so your game supports local AND global multiplayer). Tested, it works
I'm developing a little 1-4 multiplayer jump & run for the iPad and have a problem regarding the synchronization.
The game uses the Game-Center p2p Connection to connect the devices.
Now my problem is,
what data should i send?
-> I have enemies, moving platforms, destructible blocks and Items.
when should I send the data?
Here are my first approaches to sync the PCs (playable characters):
1)
send position (x,y) every frame
--> physics and character don't interact, the character is set to the transmitted position
Problem:
lots of data to send
the PC lags sporadicly
2)
send user input whenever it changes
--> physics and character interact, only the opponents button actions are simulated
Problem:
the latency is not always the same, e.g. the time one button is pressed is not exactly the same on all devices. This means one PC runs longer/shorter on one device.
So what is the best approach to sync a little p2p jump & run game?
Thanks in advance. I appreciate your help.
what data should i send?
Send everything that you would need to know in order to perform the neccesary game logic. Send new locations of enemies, updated health info, etc.
when should I send the data?
Very frequently. You'll want fresh data when you update the screen so that the game appears to be very smooth. Ideally around 30 times per second. Some of the less important data may be able to be sent using the Unreliable method to cut down on network traffic.
i've searched and read many articles and looked at the GKTank apple sample (can't see any specifics about my problem, seems to process events as they occur) but i can't see a sample or tutorial that seems to answer the following
in a multiplayer game over bluetooth/internet (maybe) how do you synchronise player interactions so that the correct game outcome occurs taking into account latency etc ...
for example:
A button is displayed to each player A & B on two separate devices connected via bluetooth
Player A presses the button (Player A is hosting the game and so latency is not an issue)
Player B presses the button just before Player A but his connection to Player A's device has at least a 200ms network latency
problem: the game needs to know Player B pressed first even though Player A's touch request would reach the game code first - i.e. no advantage to the hosting player.
i know the touch event has the timestamp and so i could ensure that the actual time of the press was sent from Player B to the game code... but not sure if thats the right approach and if it is where to go from there..
i expect the answer lies in some game time cycle where touches aren't processed immediately but within a game cycle...
any help on this or pointers to tutorial or specific source code that handles this would be appreciated.
adam
You might want to ask this question on https://gamedev.stackexchange.com/ , as it's a general question about multiplayer lag or latency, and not particularly specific to the iPhone.
You might be able to use timestamps to order the action messages. I think iPhones are synchronized to a date time server maintained by AT&T.
Well I have no actual experience with creating multiplayer games. But as is the case with most game developement questions, I suppose there is no wrong or right, so some logic thinking should do the trick.
Here are some thoughts you might want to consider:
Even if no latency is occuring on Player A's device, you'll have to introduce some to compensate for player B (and vice versa, since from Player B's point of view Player A is late too)
Thus, you'll need to introduce some kind of a "command stack" to buffer input from both players and execute the commands once both Players had the opportunity to contribute their inputs for a given point in gametime
You may want to introduce ping commands to measure the actual latency from time to time
Only one Device (the host) should be measuring the latency and announce it to the client
Based on the measured latency, calculate a time offset (relative to the hosts time) or delay for the commands in the stack (use the latency to convert Player B's timestamps to "local" time)
Keep the delay dynamic if possible, to compensate for varying latency (keep measuring latency throughout the session
If the actual latency peaks beyond the calculated one, commands from Player B may be put onto the stack late - make sure they will be executed anyway (lag may be experienced at this point)
You'll actually need two "layers" - the input layer, which exchanges, buffers and synchronizes the actual input, and a game layer, which receives the delayed commands from the input layer
So far, my 2 cents ;-)