Sign In Register

How can we help you today?

Start a new topic
Answered

Return Achievement Name when player unlocks an achievement

Hello, I'm using the Achievements feature on GameSparks, I follow this post I found to earn an achievement: http://machxgames.com/blog/?p=147

It works, but what I want is to get the Achievement Name of the achievement earned to show on my game, because the shortCode of the achievement is not HumanReadable (it cant contain white spaces) and I want to get the name of the achievement to show on the screen, I was playing with the code, trying to get the AchievementName and return it with Spark.setScriptData 

var playerID=Spark.getData().PlayerID;
var achievementID=Spark.getData().AchievementID;
var player=Spark.loadPlayer(playerID);
player.addAchievement(achievementID);

var achievementMessage = Spark.runtimeCollection("playerMessage");

var achievementMeta = achievementMessage.findOne(
    {
        "playerId": playerID,
        "messageClass": ".AchievementEarnedMessage",
        "message" : {$regex: achievementID}
    });

Spark.setScriptData("response", achievementMeta);

but it doesn't work, it returns None, but when I use the NoSQL explorer and use the query criteria, it shows the data I want, actually I have it working with this extra functions on the client side:

  

void SetAchievementName (string newAchievementName)
{
	achievementName = newAchievementName;
	achievementPanelGUI.txtAchievementName.text = newAchievementName;
}

private void GetAchievementData (string achievementShortCode)
{
	ListAchievementsRequest achivementsRequest = new ListAchievementsRequest ();
	achivementsRequest.Send ((response) =>
	{
		if (!response.HasErrors) {
			foreach (ListAchievementsResponse._Achievement achievement in response.Achievements) {
				if (achievement.ShortCode == achievementShortCode) {
					SetAchievementName (achievement.Name);
					break;
				}
			}
		} else {
			Debug.Log (response.Errors.JSON);
		}
	});
}

I want to avoid making extra API calls just having the Achievement Name returned the first time it is earned, because making extra API calls is slow; I have the screen looks like the screenshot above when an achievement is earned but with the actual extra API call there's a significant delay until the Achievement Name is shown on the bubble, and it looks empty for a while: 








Best Answer
Then you need something like this
// get all achievements
// response should be something like this
// [
//   {
//   "shortCode": "A_First",
//   "name": "My First Achievement",
//   "description": "For the very first time",
//   "earned": true
//   }
// ]
var myAchievements = Spark.sendRequest(new SparkRequests.ListAchievementsRequest()).achievements;
var achievementName = "";
for (i = 0; i < myAchievements.length; i++) {
    if (myAchievements[i].shortCode == achievementID) {
        achievementName = myAchievements[i].name;
    }
}

Spark.setScriptData("achievementAdded", achievementName);

 

 


Hi there,

You would get the response below if you forget to set AchievementID in your Event. Head to the Events tab (not the one in cloud code, but the one parallel with Cloud Code) and define AchievementID there. I've attached in case it helps.
{
 "@class": ".LogEventResponse",
 "error": {
  "AchievementID": "UNKNOWN"
 },
 "scriptData": null
}

 Now your response should be

{
 "@class": ".AchievementEarnedMessage",
 "messageId": "557a93b5e4b04bc33857339c",
 "notification": true,
 "summary": "You just earned My First Achievement",
 "achievementShortCode": "A_First",
 "achievementName": "My First Achievement",
 "playerId": "55541643e4b066559b808acf"
}

 

png

I have this on  Events tab:



And this are some demo achievements:



What I want to do is when I call my Cloud script to earn an achievement, return the achievement name in the ScrptData, now it only returns True or False

 

var playerID=Spark.getData().PlayerID;
var achievementID=Spark.getData().AchievementID;
var player=Spark.loadPlayer(playerID);
var achievementAdded = player.addAchievement(achievementID);

Spark.setScriptData("achievementAdded", achievementAdded);

 



Answer
Then you need something like this
// get all achievements
// response should be something like this
// [
//   {
//   "shortCode": "A_First",
//   "name": "My First Achievement",
//   "description": "For the very first time",
//   "earned": true
//   }
// ]
var myAchievements = Spark.sendRequest(new SparkRequests.ListAchievementsRequest()).achievements;
var achievementName = "";
for (i = 0; i < myAchievements.length; i++) {
    if (myAchievements[i].shortCode == achievementID) {
        achievementName = myAchievements[i].name;
    }
}

Spark.setScriptData("achievementAdded", achievementName);

 

 

Yeah! that is what I was looking for, I owe you a couple of beers :D