How to send array or dictionary over real-time game center game network??

I'm trying to have the host player send a dictionary with the locations of all the map objects and tiles to be accepted by the multiplayer players and load the "map" from that dictionary.


Does anyone know how to send a dictionary over the network? Do I need to compress it somehow before sending it off and then unzipping it on the receiving end?

You need to create a data package and then send it using something like:

[myMatch sendDataToAllPlayers:data withDataMode:GKSendDataReliable error:nil];


You can create a data package lots of ways. For example for short variables (short, int, float, etc.):

NSMutableData *data= [NSMutableData dataWithBytes:&variableOne length:sizeof(variableOne)];

[dataToPeer appendBytes:&variableTwo length:sizeof(variableTwo)];


or perhaps this will work for any dictionary (myDictionary):

NSError *error1=nil;

NSData *data = [NSPropertyListSerialization dataWithPropertyList:myDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:&error1 ];

Do you know how I can unwrap the dictionary data on the receiving end?


Like:


var dictionary = data(....)??

For (I know this works)

NSMutableData *data= [NSMutableData dataWithBytes:&variableOne length:sizeof(variableOne)];

[dataToPeer appendBytes:&variableTwo length:sizeof(variableTwo)];


use:


NSRange range;

range.location=0;


range.length=sizeof(variableOne);

[data getBytes:&variableOne range:range];

range.location=range.location+range.length;

range.length=sizeof(variableTwo);

[data getBytes:&variableTwo range:range];

range.location=range.location+range.length;

// repeat the above three lines for each variable you wish to transmit.



For: (I think this will work, it might be less compressed than above)

NSData *data = [NSPropertyListSerialization dataWithPropertyList:myDictionary format:NSPropertyListXMLFormat_v1_0 options:0 error:&error1 ];

use:

myDictionary =[NSPropertyListSerialization

propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:&format error:&errorDesc];

How to send array or dictionary over real-time game center game network??
 
 
Q