Page 1 of 1
access room variables without joining the room?
Posted: 20 Aug 2009, 01:36
by roocell
is this possible?
i'm iterating all the rooms for a lobby view and I want to pull out some custom room variables. I am able to get the room vars if I join the room (some other code does this), but in this case I cant retrieve the room vars.
are the room vars only accessable when in a room?
Code: Select all
NSInteger gameRoomCount = 0;
for (id roomId in [smartFox getAllRooms]) {
INFSmartFoxRoom *room = [[smartFox getAllRooms] objectForKey:roomId];
NSDictionary *vars = [room getVariables];
if ([[[smartFox getAllRooms] objectForKey:roomId] isGame]) {
for(NSString *aKey in vars){
NSLog(aKey);
}
gameRoomCount++;
}
}
return gameRoomCount;
Posted: 21 Aug 2009, 15:25
by roocell
looks like the server doesnt ship along the room vars for
Code: Select all
INFSmartFoxSysHandler.m:- (void)handleRoomList:(id)o
{
...
// Handle Room Variables
NSLog(@"%s testing if room has vars", __FUNCTION__);
NSArray *roomVarsA = [roomNode nodesForXPath:@"./vars/var" error:&error];
if ([roomVarsA count] > 0) {
NSLog(@"%s there are rooms variables", __FUNCTION__);
[self populateVariables:[room getVariables] xmlData:roomVarsA changedVars:nil];
}
...
}
Posted: 21 Aug 2009, 17:00
by roocell
i also did something similar in handleRoomAdded(), but I also printed out the XML, in which the 'vars' portion is empty.
But I think i have figured it out in theory anyways.
Problem is i dont create the room vars until after the creator has joined the room. By this point the handleRoomAdded() has aleady been triggered to the other client with empty room vars.
I think what I need to do is update my room list on the onRoomVariablesUpdate(). Then when I traverse the list of rooms again they should have variables.
I'll post again when i'm done testing this theory.
Posted: 21 Aug 2009, 18:23
by roocell
nope that wont work because onRoomVariablesUpdate() only gets called if you're in the room.
somehow i need to vars set in the room at creation time or delay the handleRoomAdded() from the server.
i've also though about entering each room as an observer and getting notified of the rooms vars that way.
Does anyone know how to get room variables on handleRoomAdded() ???
Posted: 21 Aug 2009, 18:28
by roocell
ok - according to the user docs you can create room vars at creation time - i just need to figure out how to do that with the iphone client API.
Posted: 21 Aug 2009, 18:56
by roocell
ok - i figured it out. you CAN create room vars at creation time.
hopefully someone will find this useful
Code: Select all
INFSmartFoxRoomCreateParams *params = [INFSmartFoxRoomCreateParams roomCreateParamsWithName:createGameField.text];
params.password = createPasswordField.text;
params.maxUsers = 2;
params.maxSpectators = 0;
params.isGame = YES;
[params addVar:[INFSmartFoxRoomVariable roomVariableWithInteger:@"version" value:1]];
[smartFox createRoom:params roomId:-1];
INFSmartFoxRoomCreateParam:addVar crashing on room create
Posted: 21 Aug 2009, 22:11
by roocell
i've tried adding a variable using addVar below. this is the createGame function in the tictactoe demo. but this is making the app crash.
any ideas?
Code: Select all
- (IBAction)createGame:(id)sender {
NSLog(@"createGame");
INFSmartFoxRoomCreateParams *params = [INFSmartFoxRoomCreateParams roomCreateParamsWithName:gameName.text];
params.password = password.text;
params.maxUsers = 2;
params.maxSpectators = 0;
params.isGame = YES;
[params addVar:[INFSmartFoxRoomVariable roomVariableWithInteger:@"version" value:1]];
[smartFox createRoom:params roomId:-1];
// [appDelegate switchToView:@"INFSmartFoxiPhoneClient_TicTacToe_InGameViewController" uiViewController:[INFSmartFoxiPhoneClient_TicTacToe_LobbyViewController alloc]];
}
Posted: 21 Aug 2009, 22:45
by roocell
ok - fixed the crash.
i had to change INFSmartFoxRoomCreateParams.
it was crashing on the [_vars release];
i'm a little fresh to Objective-C, but I think it's because it was freeing something that this object never allocated.
what I did was change the creation of _vars to use an alloc, then the release makes sense.
here's the bottom of INFSmartFoxRoomCreateParams.m
Code: Select all
- (void)addVar:(INFSmartFoxRoomVariable *)var
{
if (_vars == nil) {
_vars = [[NSMutableArray alloc] initWithCapacity:0];
}
[_vars addObject:var];
}
- (void)dealloc {
[_name release];
[_vars release];
[super dealloc];
}
[/code]
Posted: 25 Aug 2009, 09:17
by cemuzunlar
Thanks for noticing the issue.
The fix should be:
Code: Select all
- (void)addVar:(INFSmartFoxRoomVariable *)var
{
if (_vars == nil) {
_vars = [[NSMutableArray array] retain];
}
[_vars addObject:var];
}
We'll include this fix in the next release.