I am making an iOS game and need to save the highest level the player has reached. I can successfully change a data element in a plist, but for some reason, that data keeps reverting to its original value every time the game restarts. Here is the basic flow of my code:
In the game's init, get the highest level the player has reached (the original value is 1)
pData = [[PlayerData alloc] init];
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
[self startNewLevel:currentLevel];
'data' is an NSMutableDictionary that gets initialized like this in PlayerData:
self.data = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"]];
later, if the player beats the highest level, I increment the 'highest level' value and write to the file by calling this function in PlayerData:
-(void) newHighestLevel:(NSString*)path :(int)level{
[self.data setValue:[NSNumber numberWithInt:level] forKey:path];
[self.data writeToFile:@"playerdata.plist" atomically:YES]
I know all this is working because I have a level menu that the player can access while playing the game. Each time the player presses the level menu button, a subclass of a UITableView gets created that displays level 1 through the highest level reached. The initialization looks like this:
levelMenu = [[LevelMenu alloc] init:[[pData.data valueForKey:@"Highest Level"] intValue]];
The level menu displays the correct number of levels while in the game (e.g. if the player hasn't beat any levels and goes to the level menu, it just displays "level 1"; but if the player beats level 1 and goes to the level menu, it displays "level 1" and "level 2"). However, whenever the app gets terminated or the user quits to the games main menu, the value for 'highest level' reverts back to 1 and this code:
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
always sets currentLevel to 1 when the player pushes start, no matter how many levels the user beat while playing.
Why is the value changing back? Am I missing something that would make my plist edits permanent?
EDIT:
here is my new 'newHighestLevel' method:
-(void) newHighestLevel:(NSString*)path :(int)level{
[self.data setValue:[NSNumber numberWithInt:level] forKey:path];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
[self.data writeToFile:docfilePath atomically:YES];
self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
BOOL write = [self.data writeToFile:docfilePath atomically:YES];
}
write gets set to YES. If I change 'docfilePath' to @"playerdata.plist", it gets set to NO. Nothing seems to change with the game in either case.
SOLUTION:
-(void) newHighestLevel:(NSString*)path :(int)level{
[self.data setValue:[NSNumber numberWithInt:level] forKey:path];
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
[self.data writeToFile:docfilePath atomically:YES];
}
and in init
-(id) init{
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:docfilePath]){
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:docfilePath error:nil];
}
self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
return self;
}
[self.data writeToFile:@"playerdata.plist" atomically:YES]
call return? My suspicion is that's a read-only file and the write is failing but you're not catching the error. – BainterNSUserDefaults
. – Silma