I would like to start by saying this is source code that I purchased to use from another developer. I have been unable to get in contact with them about the issue. Therefore I do not fully understand what all of the lines of code are doing.
Lets get right to the issue. So when I received the code and ran it for the first time I received an error message on the line I have indicated below. The error was a EXC_BAD_ACCESS code=1
Now that I have pretty much got the app ready, I was still receiving the issue. After hours of trying to find information on EXC_BAD_ACCESS. I finally decided to change the line to
return 0; instead of return _state; This fixed the crash but now gamecenter is not signing the user in and the leaderboard button is not working. I have included all the Gamecenter related code below.#import <GameKit/GameKit.h>
@interface BBGameCenterDelegate : NSObject{
}
@end
class BBGameCenter{
static BBGameCenter *_gameCenter;
int _state;
BBGameCenterDelegate *_delegate;
NSMutableArray *_achievements;
public:
BBGameCenter();
static BBGameCenter *GetGameCenter();
bool GameCenterAvail();
void StartGameCenter();
int GameCenterState();
void ShowLeaderboard( String leaderboard_ID );
void ReportScore( int score,String leaderboard_ID );
void ShowAchievements();
void ReportAchievement( float percent,String achievement_ID );
float GetAchievementPercent( String id );
/
GKAchievement *FindAchievement( String id );
void GameCenterViewControllerDidFinish( UIViewController *vc );
};
/
BBGameCenter *BBGameCenter::_gameCenter;
@implementation BBGameCenterDelegate
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController*)vc{
BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)vc{
BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)vc{
BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
@end
BBGameCenter::BBGameCenter():_state(-1),_delegate(0),_achievements(0){
if( !GameCenterAvail() ) return;
_delegate=[[BBGameCenterDelegate alloc] init];
_state=0;
}
BBGameCenter *BBGameCenter::GetGameCenter(){
if( !_gameCenter ) _gameCenter=new BBGameCenter();
return _gameCenter;
}
bool BBGameCenter::GameCenterAvail(){
/
Class gcClass=NSClassFromString( @"GKLocalPlayer" );
/
NSString *reqSysVer=@"4.1";
NSString *currSysVer=[[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported=([currSysVer compare:reqSysVer options:NSNumericSearch]!=NSOrderedAscending);
return (gcClass && osVersionSupported);
}
void BBGameCenter::StartGameCenter(){
if( _state ) return;
GKLocalPlayer *localPlayer=[GKLocalPlayer localPlayer];
if( localPlayer ){
_state=1;
[localPlayer authenticateWithCompletionHandler:^(NSError *error){
if( localPlayer.isAuthenticated ){
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements,NSError *error){
if( achievements ){
_achievements=[[NSMutableArray alloc] init];
[_achievements addObjectsFromArray:achievements];
/
int n=[_achievements count];
for( int i=0;i<n;++i ){
GKAchievement *achievement=[_achievements objectAtIndex:i];
bbPrint( String("Achievement:")+String(achievement.identifier) );
}
*/
}
_state=2;
}];
}else{
_state=-1;
}
}];
}else{
_state=-1;
}
}
int BBGameCenter::GameCenterState(){
return _state;
}
void BBGameCenter::ShowLeaderboard( String leaderboard_ID ){
if( _state!=2 ) return;
GKLeaderboardViewController *vc=[[GKLeaderboardViewController alloc] init];
if( !vc ) return;
vc.leaderboardDelegate=(id)_delegate;
vc.timeScope=GKLeaderboardTimeScopeToday;
vc.category=leaderboard_ID.ToNSString();
_state=3;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
void BBGameCenter::ReportScore( int value,String leaderboard_ID ){
if( _state!=2 ) return;
GKScore *score=[[GKScore alloc] initWithCategory:leaderboard_ID.ToNSString()];
score.value=value;
score.context=0;
[score reportScoreWithCompletionHandler:^(NSError *error){} ];
}
void BBGameCenter::ShowAchievements(){
if( _state!=2 ) return;
GKAchievementViewController *vc=[[GKAchievementViewController alloc] init];
if( !vc ) return;
vc.achievementDelegate=(id)_delegate;
_state=4;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
GKAchievement *BBGameCenter::FindAchievement( String id ){
if( !_achievements ) return 0;
NSString *str=id.ToNSString();
int n=[_achievements count];
for( int i=0;i<n;++i ){
GKAchievement *achievement=[_achievements objectAtIndex:i];
if( [achievement.identifier isEqualToString:str] ) return achievement;
}
return 0;
}
void BBGameCenter::ReportAchievement( float percent,String achievement_ID ){
if( _state!=2 ) return;
GKAchievement *achievement=FindAchievement( achievement_ID );
if( !achievement ){
achievement=[[GKAchievement alloc] initWithIdentifier:achievement_ID.ToNSString()];
[_achievements addObject:achievement];
}
achievement.percentComplete=percent;
achievement.showsCompletionBanner=true;
[achievement reportAchievementWithCompletionHandler:^(NSError *error){} ];
}
float BBGameCenter::GetAchievementPercent( String achievement_ID ){
GKAchievement *achievement=FindAchievement( achievement_ID );
if( !achievement ) return 0;
return achievement.percentComplete;
}
void BBGameCenter::GameCenterViewControllerDidFinish( UIViewController *vc ){
_state=2;
/
/
[BBIosGame::IosGame()->GetUIAppDelegate()->viewController dismissModalViewControllerAnimated:YES];
}If you take a look at line 104 you will notice it says return _state; This is the line causing the issue. My theory is that _state is set to null and not being defined in the lines above the return. I am not sure why this would be the case but I do believe it is possible. When I replace return _state with return 0, everything runs with no crashes but gamecenter does not attempt to longin the player nor does the leaderboard button work. Thank you for you time and patience.