1

I have a problem with sending an array of integers via GKSession.

This is how I did it:

To send it.

-(void)sendData {
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    [myArray addObject:[NSNumber numberWithInteger:snakeHead.position.x]];
    [myArray addObject:[NSNumber numberWithInteger:snakeHead.position.y]];
    [myArray addObject:[NSNumber numberWithInteger:direction.x]];
    [myArray addObject:[NSNumber numberWithInteger:direction.y]];
    [myArray addObject:[NSNumber numberWithInteger:bodyOffsetX]];
    [myArray addObject:[NSNumber numberWithInteger:bodyOffsetY]];
    [myArray addObject:[NSNumber numberWithInteger:amountBodies]];


    NSData* encodedArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];

    [snakeSession sendData:encodedArray toPeers:snakePeers withDataMode:GKSendDataReliable
                                                            error:nil];
    [encodedArray release];
}

The sendData function is called from a scheduler.

To receive it.

-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
                                                       context:(void *)context {
    NSArray *hisArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

    snakeHead2.position = ccp([[hisArray objectAtIndex:0] integerValue], 
                          [[hisArray objectAtIndex:1] integerValue]);

    direction2 = ccp([[hisArray objectAtIndex:2] integerValue], 
                          [[hisArray objectAtIndex:3] integerValue]);

    bodyOffsetX2 = [[hisArray objectAtIndex:4] integerValue];
    bodyOffsetY2 = [[hisArray objectAtIndex:5] integerValue];
    amountBodies2 = [[hisArray objectAtIndex:6] integerValue];
}

This does not work and the game crashes.

So what am I doing wrong here?

Marco
  • 6,692
  • 2
  • 27
  • 38
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    Try to avoid sending "archived" data over the network frequently, that is a HUGE waste of bandwidth. Rather send a simple memory buffer with each value followed by the other (a "packet" of data). As for your question: how does it crash? Where? What does it say? – CodeSmile Feb 24 '13 at 20:29
  • Tread 1:EXC_BAD_ACCES (code=1, address=0xd0000010) Thats the error and and it chrashes directly when sendData is called. – Arbitur Feb 24 '13 at 20:45
  • encodedArray is not nil? snakePeers is not nil and contains NSString with player names? Try sending a newly allocated NSData with nothing in it, or perhaps just a string to see if the problem is with the data. – CodeSmile Feb 24 '13 at 20:55
  • It works when i send 1 string like this [snakeSession sendData:[string dataUsingEncoding:NSASCIIStringEncoding] toPeers:snakePeers withDataMode:GKSendDataReliable error:nil]; but i need to send many integers and this doesnt work sending an array. – Arbitur Feb 24 '13 at 21:37
  • try just one number in the array, and again with a string in the array. – CodeSmile Feb 24 '13 at 21:50
  • i have already tried that and it wint work, is there another way to send an array over bluetooth? – Arbitur Feb 24 '13 at 21:58
  • Yes, like I said: a memory buffer (also called a C array). This is also a lot more efficient. – CodeSmile Feb 26 '13 at 09:13
  • I think i have finally fixed it. It was the [encodedArray release] that made it crash. – Arbitur Mar 01 '13 at 20:55
  • 1
    should be autorelease, or even better: ARC – CodeSmile Mar 02 '13 at 00:27

1 Answers1

2

heres what I would do. it even sends less bytes! (faster)

// to send
int snakeHead_position_x = 0;
int snakeHead_position_y = 0;
int direction_x = 0;
int direction_y = 0;
int bodyOffsetX = 0;
int bodyOffsetY = 0;
int amountBodies = 0;

NSString *package = [NSString stringWithFormat:@"%i|%i|%i|%i|%i|%i|%i",
        snakeHead_position_x, snakeHead_position_y, direction_x,
        direction_y, bodyOffsetX, bodyOffsetY, amountBodies];

NSData *data = [package dataUsingEncoding:NSASCIIStringEncoding];
assert(data); // this is critical don't remove!

//printf("sending %u bytes...\n", data.length);

[snakeSession sendData:data toPeers:snakePeers withDataMode:GKSendDataReliable error:nil];


// to retrieve

NSArray *intArray = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] componentsSeparatedByString:@"|"];

int snakeHead_position_x = [intArray[0] intValue];
int snakeHead_position_y = [intArray[1] intValue];
int direction_x = [intArray[2] intValue];
int direction_y = [intArray[3] intValue];
int bodyOffsetX = [intArray[4] intValue];
int bodyOffsetY = [intArray[5] intValue];
int amountBodies = [intArray[6] intValue];
  • Wow thank you you tought me a BIG thing now, I didnt know about that componentsSeparatedByString. :D – Arbitur Mar 19 '13 at 17:53