Hi,
I need help getting a variable from a foreach in this callback.
void Start()
{
Social.LoadAchievements(achievements => {
if (achievements.Length > 0)
{
Debug.Log("Got " + achievements.Length + " achievement instances");
string myAchievements = "My achievements:
";
foreach (IAchievement achievement in achievements)
{
myAchievements += " " +
achievement.id + " " +
achievement.percentCompleted + " " +
achievement.completed + " " +
achievement.lastReportedDate + "
";
}
Debug.Log(myAchievements);
}
else
Debug.Log(“No achievements returned”);
});
}
I can print results in log and it shows all my achievement. But how to I get the achievement.completed out of the foreach as an int?
.
I tried making an int called achievementsCompleted = 0; and after the foreach I said;
achievementsCompleted = achievement.completed;
This however doesnt work. Please help
Try the following code:
private int achievementsCompletedCount = 0 ;
void Start()
{
Social.LoadAchievements(achievements => {
if (achievements.Length > 0)
{
Debug.Log("Got " + achievements.Length + " achievement instances");
string myAchievements = "My achievements:
";
foreach (IAchievement achievement in achievements)
{
if( Mathf.Approx( achievement.percentCompleted, 100f ) )
achievementsCompletedCount++
myAchievements += " " +
achievement.id + " " +
achievement.percentCompleted + " " +
achievement.completed + " " +
achievement.lastReportedDate + "
“;
}
myAchievements += achievementsCompletedCount + " achievements completed”;
Debug.Log(myAchievements);
}
else
Debug.Log(“No achievements returned”);
});
}
It doesn’t work. I gives errors in this line; if (Mathf.Approximately(achievement.percentCompleted, 100f))
.
I’m able to increment the integer each time a achievement is completed and save it to PlayerPrefs. However, if the user deletes the game and re-installs progress is lost. If I run the example code, I get the unlocked achievements just fine, however I can’t pull the amount.
if( achievement.completed )
achievementsCompleted++;
This adds 1 to the amount each time, so that part works. However, I can’t pull the amount out.
The initial with Mathf.Approx made an error with “Approx”. Changed it to Approximately and the (achievement.percentCompleted) came out as an error.
.
I tried saving the int to Google Play Services - Cloud Saving, but I didn’t managed to get it to work. I figured, if I could pull the number of unlocked achievements out from the foreach, I could save it in PlayerPrefs and ve done with it. So if the user deletes and re-installs, the script would run again and fetch the amount and place it in the PlayerPrefs.
.
For the moment, I’m able to use your first solution to increment the int each time an achievement is unlocked. However I can’t just place it as the value of the integer. I have to alter it either by increment, decrement etc.
.
if (achievement.completed)
{
achievement.completed = achievementsCount;
}