Tag Archives: openfeint

OpenFeint 2.7.5 and Achievement Notifications

I’ve been adding OpenFeint 2.7.5 to my iOS game Pawns!, and ran into what I suspect is a bug. When an achievement is earned, the user is not getting notified. (I found developers on a Torque forum who ran into something similar with 2.7.5.)

This seems to have been introduced when OpenFeint added their new API for setting achievements to be partially complete. Or maybe it has something to do with their new GameCenter integration.

Poking around the OpenFeint code and its Unity wrapper, I found that when the Unity script calls

OpenFeint.UnlockAchievement(MY_ACHIEVEMENT_ID);

the following Objective-C code eventually executes in AppController+OpenFeint.mm:

// Unlock the achievement.
[OFAchievementService updateAchievement:[args objectAtIndex:1]
					 andPercentComplete:100
					andShowNotification:FALSE
					  onSuccess:success
					  onFailure:failure];

Why is notification explicitly turned off here? Was it deliberate? I have no idea. A quick fix might be to set FALSE to TRUE. But, you risk losing that change the next time you upgrade OpenFeint’s code.

I noticed just below this code that the OpenFeint plugin is prepared to handle another command from Unity, “UpdateNotification”, that takes additional arguments, namely the percent complete and whether to show notifications:

	// UpdateAchievement //
	else if(OF_CMD(@"UpdateAchievement"))
	{
		// Set up the delegates.
		OFDelegate success(self, @selector(onAchievementSuccess));
		OFDelegate failure(self, @selector(onAchievementFailure));
		
		// Unlock the achievement.
		[OFAchievementService updateAchievement:[args objectAtIndex:1]
							 andPercentComplete:[[args objectAtIndex:2] doubleValue]
							andShowNotification:[[args objectAtIndex:3] boolValue]
									  onSuccess:success
									  onFailure:failure];
	}

Looks useful! The only problem is that they haven’t yet added this command to the Unity C# wrapper.

But that’s easy to fix! Just add the following to OpenFeint.cs in your Unity project:

	// UpdateAchievement //
	//
	// Unlock an achievement.
	//  - achievementId:      The ID of the achievement.
	//  - percentComplete:  How much to unlock
	//  - showNotification:   Whether to notify
	//
	//	events				OnAchievementUnlocked
	//
	static public void UpdateAchievement(long achievementId, int pct, bool showNotif)
	{
		PerformCommand(String.Format("UpdateAchievement|{0}|{1}|{2}", achievementId, pct, showNotif));
	}

Now to unlock an achievement I call:

OpenFeint.UpdateAchievement(MY_ACHIEVEMENT_ID, 100, true);

This seems to fix the notification issue, and gives Unity scripts a way to set the percentage-complete of an achievement (I didn’t test this.) I also prefer this solution because it is very likely that OpenFeint will themselves add this to their Unity wrapper class in the future.

(If you are really paranoid, you could put this code somewhere else so that future OpenFeint wrapper changes won’t overwrite it. All it does is format a particular PlayerPref string that triggers the OpenFeint plugin.)