David Bullard
David Bullard

Reputation: 23

Send and Receive data from Game Center

I'm having a lot of trouble sending data between devices from game center multiplayer. I can establish a match and have both user connected but for some reason I can't send data. Here is my code:

-(void)sendData {
    NSError *error;
    int myScore = scoreInt;
    NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
    [theMatch sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error: &error];
    if (error != nil)
    {
        NSLog(@"ERROR: %@", error);
    }    
}

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

    NSLog(@"called");

}

I'm carrying my match from another view and I don't know if thats the problem but here is the code when game center finds the match:

 - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
    MultiplayerView *mpv = [[MultiplayerView alloc] init];

    [self dismissModalViewControllerAnimated:NO];

    mpv.theMatch = match; // Use a retaining property to retain the match.

    match.delegate = self;

    NSLog(@"Matched");
    if (!self.matchStarted && match.expectedPlayerCount == 0)
    {
        self.matchStarted = YES;
        NSLog(@"Lets Go");
        MultiplayerView *mpv = [[MultiplayerView alloc] init];
        [mpv setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:mpv animated:YES];
    }
}

Any Ideas?

Upvotes: 2

Views: 618

Answers (1)

lykant
lykant

Reputation: 516

You must assign current view controller to your match delegate, otherwise match:didReceiveData:fromPlayer: will not work.

Upvotes: 1

Related Questions