Online Game GameCenter Send Message Problem???

Forums Programming cocos2d support (graphics engine) Online Game GameCenter Send Message Problem???

This topic contains 4 replies, has 4 voices, and was last updated by  Scryb 2 years, 6 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
September 10, 2011 at 6:55 pm #235137

Thnx4TheXP
Participant
@thnx4thexp

I am making an online shooter and in my pre-game lobby I want players to be able to send messages. I followed Ray Wenderlich’s tutorial and I have had no problems at all sending and receiving CGPoints, Booleans, Integers, Floats but I am having a problem sending NSStrings. The app just crashes. This is what I have…

typedef struct {
Message message;
NSString *messageToSend;
} MessageSendMessage;

-(void)sendData:(NSData*)data {
NSError *error;
BOOL success = [[GCHelper sharedInstance].match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) { NSLog(@"Error sending init packet"); [self matchEnded]; }
}
-(void)sendMessage:(NSString*)messageToSend {
MessageSendMessage message;
message.message.messageType = kMessageTypeSendMessage;
message.messageToSend = messageToSend;
NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageSendMessage)];
[self sendData:data];
}
-(void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
if (otherPlayersID == nil) otherPlayersID = [playerID retain];
otherPlayersID = playerID;
Message *message = (Message*)[data bytes];
switch (message->messageType) {
case kMessageTypeSendMessage:
{
MessageSendMessage *messageSend = (MessageSendMessage*)[data bytes];

// STWS = String that was sent
NSString *stws = messageSend->messageToSend;

CCLabelTTF *label = [CCLabelTTF labelWithString:stws fontName:@"pirulen.ttf" fontSize:15];
label.position = ccp(240, 160);
[self addChild:label];
}
break;

default:
break;
}
}

The app crashes when it is setting the string of the CCLabelTTF. Any help would be appreciated. Thanks

September 10, 2011 at 8:06 pm #346854

Thnx4TheXP
Participant
@thnx4thexp

Do I have to convert the string to some type of data?

September 25, 2011 at 8:31 pm #346855

x0661t
Participant
@x0661t

Hi, how did you send CGPoints?

September 25, 2011 at 10:17 pm #346856

RossyP
@rossyp

CJ helped me out with this a little while ago: http://www.cocos2d-iphone.org/forum/topic/20480

September 26, 2011 at 3:15 am #346857

Scryb
Participant
@scryb

Basically, you can’t use objects in structs and send them as NSData, because the struct only contains the pointers to the objects and not the objects themselves. To send strings, you either have to convert them to cstrings or send them as serialized objects. One way would be to use dictionaries instead of structs, and include a “type” key in the dictionary. You’ll have to rewrite the code to use this method instead of the struct, but if you’re going to be sending strings and perhaps even other objects then this is sure to be an easier way than to mess with conversions to cstrings and such.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.