Where can I find a generic game state singleton for Objective C?

I've been able to get a basic game state manager working from watching tutorials from 71squared. Com I use the SynthesizeSingleton. H from CocoaWithLove and am able to save and load states using NSCoder / archiving As it uses the singelton, I can reference it by writing: GameStateManager *gsm = GameStateManager sharedGameStateManager As below: GameStateManager.

H file @interface GameStateManager : NSObject { NSMutableArray *listOfPlayers; } @property (nonatomic, retain) NSMutableArray *listOfPlayers; + (GameStateManager *)sharedGameStateManager; -(void)loadGameState; -(void)saveGameState; // GameStateManager. M #import "GameStateManager. H" #import "SynthesizeSingleton.

H" #import "Player. H" @implementation GameStateManager SYNTHESIZE_SINGLETON_FOR_CLASS(GameStateManager) @synthesize listOfPlayers; #pragma mark - Init - (id)init { self = super init; if (self) { // Initialization code here. Self.

ListOfPlayers = NSMutableArray array; } return self; } #pragma mark - Memory management -(void) dealloc { super dealloc; } #pragma mark - Save/Load -(void)saveGameState { NSLog(@"saveGameState"); // Set up the game state path to the data file that the game state will be save to NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = paths objectAtIndex:0; NSString *gameStatePath = documentsDirectory stringByAppendingPathComponent:@"gameState. Dat"; // Set up the encoder and storage for the game state data NSMutableData *gameData; NSKeyedArchiver *encoder; gameData = NSMutableData data; encoder = NSKeyedArchiver alloc initForWritingWithMutableData:gameData; // Archive the object encoder encodeObject:self. ListOfPlayers forKey:@"playerObjects"; // Finish encoding and write to disk encoder finishEncoding; gameData writeToFile:gameStatePath atomically:YES; encoder release; } -(void)loadGameState { NSLog(@"loadGameState"); // Set up the game state path to the data file that the game state will be save to NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = paths objectAtIndex:0; NSString *gameStatePath = documentsDirectory stringByAppendingPathComponent:@"gameState.

Dat"; NSMutableData *gameData; NSKeyedArchiver *decoder; gameData = NSData dataWithContentsOfFile:gameStatePath; // Check to see if the . Dat file exists, and load contents if (gameData) { decoder = NSKeyedUnarchiver alloc initForReadingWithData:gameData retain; self. ListOfPlayers = decoder decodeObjectForKey:@"playerObjects" retain autorelease; NSLog(@"Returned %d players", self.

ListOfPlayers count); for (Player *p in self. ListOfPlayers) { NSLog(@"p. Name = %@", p.

Fname); } // Finished decoding, release decoder release; decoder autorelease; } else { } } In my Player. H/. M file I do this: interface Player : NSObject { NSString *fname; } @property(nonatomic,retain) NSString *fname; // Player.

M file #pragma mark - Encoding -(id)initWithCoder:(NSCoder *)aDecoder { //self initWithObject:aDecoder decodeObject; self = super init; if (self) { // Initialization code here. Self. Fname = aDecoder decodeObjectForKey:@"fname"; } return self; } -(void)encodeWithCoder:(NSCoder *)aCoder { aCoder encodeObject:self.

Fname forKey:@"fname"; } My follow-up questions are: 1) Am I meant to put a listOfPlayers in the GameStateManager? --> Moved to a new question 2) In my encoder, I have to store each field individually, is it not possible to just store the actual object itself? 3) Does encoding of an object also encode all its children?

Lets say I have a Parent class (Player) and a Child class (Weapons). If I encode the Player object only, will it encode all the Weapons too automatically because they are linked? Thanks.

I've been able to get a basic game state manager working from watching tutorials from 71squared. Com I use the SynthesizeSingleton. H from CocoaWithLove and am able to save and load states using NSCoder / archiving.As it uses the singelton, I can reference it by writing: GameStateManager *gsm = GameStateManager sharedGameStateManager; As below: // GameStateManager.

H file @interface GameStateManager : NSObject { NSMutableArray *listOfPlayers; } @property (nonatomic, retain) NSMutableArray *listOfPlayers; + (GameStateManager *)sharedGameStateManager; -(void)loadGameState; -(void)saveGameState; // GameStateManager. M #import "GameStateManager. H" #import "SynthesizeSingleton.

H" #import "Player. H" @implementation GameStateManager SYNTHESIZE_SINGLETON_FOR_CLASS(GameStateManager) @synthesize listOfPlayers; #pragma mark - Init - (id)init { self = super init; if (self) { // Initialization code here.Self. ListOfPlayers = NSMutableArray array; } return self; } #pragma mark - Memory management -(void) dealloc { super dealloc; } #pragma mark - Save/Load -(void)saveGameState { NSLog(@"saveGameState"); // Set up the game state path to the data file that the game state will be save to NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = paths objectAtIndex:0; NSString *gameStatePath = documentsDirectory stringByAppendingPathComponent:@"gameState.

Dat"; // Set up the encoder and storage for the game state data NSMutableData *gameData; NSKeyedArchiver *encoder; gameData = NSMutableData data; encoder = NSKeyedArchiver alloc initForWritingWithMutableData:gameData; // Archive the object encoder encodeObject:self. ListOfPlayers forKey:@"playerObjects"; // Finish encoding and write to disk encoder finishEncoding; gameData writeToFile:gameStatePath atomically:YES; encoder release; } -(void)loadGameState { NSLog(@"loadGameState"); // Set up the game state path to the data file that the game state will be save to NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = paths objectAtIndex:0; NSString *gameStatePath = documentsDirectory stringByAppendingPathComponent:@"gameState. Dat"; NSMutableData *gameData; NSKeyedArchiver *decoder; gameData = NSData dataWithContentsOfFile:gameStatePath; // Check to see if the .

Dat file exists, and load contents if (gameData) { decoder = NSKeyedUnarchiver alloc initForReadingWithData:gameData retain; self. ListOfPlayers = decoder decodeObjectForKey:@"playerObjects" retain autorelease; NSLog(@"Returned %d players", self. ListOfPlayers count); for (Player *p in self.

ListOfPlayers) { NSLog(@"p. Name = %@", p. Fname); } // Finished decoding, release decoder release; decoder autorelease; } else { } } In my Player.

H/. M file I do this: @interface Player : NSObject { NSString *fname; } @property(nonatomic,retain) NSString *fname; // Player. M file #pragma mark - Encoding -(id)initWithCoder:(NSCoder *)aDecoder { //self initWithObject:aDecoder decodeObject; self = super init; if (self) { // Initialization code here.

Self. Fname = aDecoder decodeObjectForKey:@"fname"; } return self; } -(void)encodeWithCoder:(NSCoder *)aCoder { aCoder encodeObject:self. Fname forKey:@"fname"; } // --- My follow-up questions are: 1) Am I meant to put a listOfPlayers in the GameStateManager?

--> Moved to a new question 2) In my encoder, I have to store each field individually, is it not possible to just store the actual object itself?3) Does encoding of an object also encode all its children? Lets say I have a Parent class (Player) and a Child class (Weapons). If I encode the Player object only, will it encode all the Weapons too automatically because they are linked?Thanks.

Ask your follow-up questions as new questions, there is nowhere for people to answer them here! – jrturton Nov 16 at 18:37 Oh sorry, I didn't know that. My fault!

– zardon Nov 16 at 19:08 I have figured out that you must encode anything you want to store, regardless of whether it is inherited or not. – zardon Nov 16 at 19:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions