Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3665832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:48:30+00:00 2026-05-19T01:48:30+00:00

EDIT: I have made a clean, new project, but still can’t get it working.

  • 0

EDIT:
I have made a clean, new project, but still can’t get it working. Please download it, there is a little code to look at and probably easy for a professional or anyone remotely experience to see whats I am doing wrong. Just trying to send that integer.

http://www.2shared.com/file/fPOCLlg5/gkTest.html

Hi

I am trying to implement Game Center multiplayer in my iphone game and having trouble understanding the samples I have at hand in the Apple Docs and from third parties concerning sending and receiving data.

Could someone please explain the code samples in the Official Apple docs here please:
http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/MatchesandVoice/MatchesandVoice.html#//apple_ref/doc/uid/TP40008304-CH10-SW4

Or help me figure out this sample code I was supplied with. Its a prebuilt class, made to handle all the game center tasks and a sample from it for sending and receiving data would be this:

- (void) sendPosition
{
    NSError *error;
    PositionPacket msg;
    msg.messageKind = PositionMessage;
    msg.x = currentPosition.x;
    msg.y = currentPosition.y;
    NSData *packet = [NSData dataWithBytes:&msg length:sizeof(PositionPacket)];
    [match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error:&error];
    if (error != nil)
    {
        // handle the error
    }
}

And receiving:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
    Packet *p = (Packet*)[data bytes];
    if (p.messageKind == PositionMessage)
        // handle a position message.
}

My big question about this code form the official docs is:

Where does PositionPacket/Packet come from?
And assuming when you want to send/receive data you call them like so:

[self sendPosition];

or

[self match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID];

What do I enter as the match, data and playerID?

E.g. I have an int named ‘score’ but is there not a special key I need to use something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:48:31+00:00Added an answer on May 19, 2026 at 1:48 am

    In this example, the PositionPacket is just a struct. The following line then puts that struct into an NSData which is just a “byte bucket” object.

    NSData *packet = [NSData dataWithBytes: &msg length: sizeof(PositionPacket)];
    

    So if you just wanted to send an int score, you could have a sendScore method that looked like this:

    - (void) sendScore
    {
        NSError *error;
        int myScore = self.score;
        NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
        [match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error: &error];
        if (error != nil)
        {
            // handle the error
        }
    }
    

    Typically, you’ll want a struct so that there’s some additional information that lets the receivers know what kind of data it is. In the example, that would have been the purpose of this line:

    msg.messageKind = PositionMessage;
    

    In general, you can send anything you want encapsulated in an NSData object, since it’s just a byte bucket. You can send primitive types like ints, or structs as in the example, or even NSObjects (as long as they implement NSCoding). You should read up on NSKeyedArchiver, NSCoding, and NSData for more information on sending and receiving NSObjects in this way. Here is Apple’s reference document on Archving.

    As for receiving, YOU don’t call the method, it gets called ON you by the Kit. It’s what’s called a “delegate method” (in Cocoa parlance) or a “callback method.” You can think of it like a “phone call” that your app can receive asynchronously. By implementing a method with the signature:

    - (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID;
    

    …you are saying “I can receive this kind of phone call.” So when the GameKit receives data on your behalf from another player, it will see that you want to receive callbacks of that kind and will then call that method — then it’s up to you to update your internal application state based on what is received.

    To continue with this example, if you had sent the simple “nothing but an integer” message described above, you might implement that method like this:

    - (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
    {
        int* receivedScorePtr = (int*)[data bytes];
        int receivedScore = *receivedScorePtr;
        [self updateScore: received forPlayer: playerID];
    }
    

    That is, of course, assuming that you have a method called updateScore:forPlayer: that would update a table of scores.

    You can find a more general discussion/explanation of how delegates and delegate methods work at this blog entry: http://mohrt.blogspot.com/2010/01/cocoa-and-delegates.html

    ADDED: Using code the asker posted, I made a few modifications and produced a version that “works” for this bare bones use case. Working Version of Test App To Send One Integer Through GameCenter I make no claims about the quality of the code, or its suitability for, well, anything at all. I didn’t write 99.9% of it – please don’t take my posting of it here as an endorsement of anything that appears in it.

    One lesson learned (that I didn’t know, so I’m putting here in hopes that it helps others) is that you can’t use the Matchmaking service with the simulator. This means that you need two development-provisioned iOS devices in order to test this scenario, and likely, for non-trivial programs, two development machines to debug both devices simultaneously. This problem cost me the most time while figuring this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a compiled swf file and a I can't edit it , but
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have recently been working with Python using Komodo Edit and other simpler editors
Edit: I have solved this by myself. See my answer below I have set
EDIT: I have submitted a bug report and Microsoft have acknowledge that it is
EDIT: I have added Slug column to address performance issues on specific record selection.
What are extension methods in .NET? EDIT: I have posted a follow up question
EDIT: I also have access to ESXLT functions. I have two node sets of
I am looking for a way to edit data and have values dynamically calculated
have downloaded Orca to edit an MSI file. I want to remove some banner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.