Scores API

The Scores API for scores lets game developers build social leaderboards and game matching by storing players' scores as they play. These scores will appear on players' timelines and in the Games Feed on Facebook.

Note that this API is only available to applications categorized as games. You can categorize your app as games in the App Dashboard by navigating to About --> Basic Info and selecting Games in the drop-down menu in the category field.

 

Read score for a player

  • If the player has granted your app the user_games_activity permission then this call will give you the latest scores for all apps for that player. Otherwise it will give you scores only for your app.
  • The friends_games_activity permission will enable you to access scores for the player's friends for all apps

You can read the score for people playing your game with:

SPFacebook.OnPlayerScoresRequestCompleteAction += OnPlayerScoreRequestComplete
SPFacebook.instance.LoadPlayerScores();

private void OnPlayerScoreRequestComplete(FB_Result result) {
	if(result.IsSucceeded) {
			string msg = "Player has scores in " + SPFacebook.instance.userScores.Count + " apps" + "\n";
			msg += "Current Player Score = " + SPFacebook.instance.GetCurrentPlayerIntScoreByAppId(SPFacebook.Instance.AppId);
			SA_StatusBar.text = msg;
			
		} else {
			SA_StatusBar.text = result.RawData;
		}	
}

 

Read scores for players and friends

You can read the set of scores for a player and their friends. The user_friends permission is required in order to view friends' scores. This returns a list of scores for a player and friends who have authorized the app. The list is sorted by descending score value, so it returns friends with the highest scores first. You can use this call to generate a leader board for a player and friends.

SPFacebook.OnAppScoresRequestCompleteAction += OnAppScoreRequestComplete; 
SPFacebook.instance.LoadAppScores();



private void OnAppScoreRequestComplete(FB_Result result) {
		if(result.IsSucceeded) {
			string msg = "Loaded " + SPFacebook.instance.appScores.Count + " scores results" + "\n";
			msg += "Current Player Score = " + SPFacebook.instance.GetScoreByUserId(SPFacebook.instance.UserId);
			SA_StatusBar.text = msg;
			
		} else {
			SA_StatusBar.text = result.RawData;
		}
}

 

After score request is loaded scores list is available under the SPFacebook.instance.appScores with is represented as:

public  Dictionary<string,  FB_Score>  appScores {get;}
public class FBScore  {
	public string UserId;
	public string UserName;

	public string AppId;
	public string AppName;

	public int value;
}

 

You can also get score owner profile image using FBScore classes, as showed on code snippet bellow:

FB_Score score =  SPFacebook.instance.GetScoreObjectByUserId(UserID);
score.OnProfileImageLoaded +=OnProfileImageLoaded;
score.LoadProfileImage(FB_ProfileImageSize.large);

void OnProfileImageLoaded (FB_Score score) {
	Texture2D tex = score.GetProfileImage(FB_ProfileImageSize.large);
}	
	

 

Create or update a score

You can post a score for a player as long as the player has granted the publish_actions permission for your app.

SPFacebook.OnSubmitScoreRequestCompleteAction += OnSubmitScoreRequestComplete;
SPFacebook.instance.SubmitScore(score);

private void OnSubmitScoreRequestComplete(FB_Result result) {
	if(result.IsSucceeded) {
			string msg = "Score successfully submited" + "\n";
			msg += "Current Player Score = " + SPFacebook.instance.GetScoreByUserId(SPFacebook.instance.UserId);
			SA_StatusBar.text = msg;
			
		} else {
			SA_StatusBar.text = result.RawData;
		}
}

 

Delete scores for a player

you can delete the score for a player as long as the player has granted thepublish_actions permission for your app.

SPFacebook.OnDeleteScoresRequestCompleteAction += OnDeleteScoreRequestComplete; 
SPFacebook.instance.DeletePlayerScores();
	
private void OnDeleteScoreRequestComplete(FB_Result result) {
	if(result.IsSucceeded) {
			string msg = "Score successfully deleted" + "\n";
			msg += "Current Player Score = " + SPFacebook.instance.GetScoreByUserId(SPFacebook.instance.UserId);
			SA_StatusBar.text = msg;
			
		} else {
			SA_StatusBar.text = result.RawData;
		}
}

 

Full implementation can be found on Facebook Example scene.