Skip to content

How to use sharpchievements

sebingel edited this page Dec 12, 2015 · 5 revisions

Setting up achievementtracking with sharpchievements ist pretty easy. You just need to register an Achievement and some AchievementConditions in the AchievementManager and call AchievementManager.GetInstance().RegisterProgress("SomeConditionString");

Lets begin with creating an AchievementCondition:

AchievementCondition

AchievementCondition aC = new AchievementCondition("This is the Titel", "aCKey", 5);
AchievementManager.GetInstance().RegisterAchievementCondition(aC);

An AchievmentCondition describes what has to be done to progress in the completion of an achievement. AchievementCondition instances are created with three parameters in the constructor:

  1. The UniqueId The UniqueId is an... tadaa... unique identifier which... tadaa... uniquely identifies the AchievementCondition within the AchievementManager.

  2. The Key The Key is used to track the progress of an AchievementCondition. The Key can be shared by multiple instances of AchievementCondition in order to track more than one AchievementCondition with only one action i.e. if you have an Achievement for starting the program once and another Achievement for starting the programm twice you can create two instances of AchievementCondition with the key "appStarted" and both would register a progress when the key is called.

  3. The Counter Defines how often a key has to be called to unlock the AchievementCondition.

Now after creating an AchievementCondition and registering it in the AchievementManager we need the Achievement that we want to unlock by progressing with our AchievementCondition:

Achievement

Achievement a = new Achievement("a", "The Title", "The Description", aC, "/sebingel.sharpchievements;component/Images/award71.png");
AchievementManager.GetInstance().RegisterAchievement(a);

Achievement instances are created with the following parameters:

  1. The UniqueId As you may have guessed the UniqueId is an unique identifier which uniquely identifies the Achievement within the AchievementManager.

  2. The Titel The Titel of the Achievement which should give a short description or the name of the Achievement object. The Titel is shown in the AchievementNotificationWindow and the AchievementControl.

  3. The Description The Description should be a more detailed description of the Achievement. It is also shown in the AchievementNotificationWindow and the AchievementControl.

  4. The AchievementCondition or a List<AchievementCondition> At least one AchievementCondition is needed in order to be ale to unlock an Achievement.

  5. The Imagepath (optional) URI or whatever to an image that is also shown in the AchievementNotificationWindow and the AchievementControl.

Now we have created an AchievementCondition and an Achievement and registered both with the AchievementManager. We are now ready and set to track some progress:

AchievementManager

The AchievementManager is a Singleton class that needs to know every Achievement and every AchievementCondition you want to track. Tracking progress is as easy as AchievementManager.GetInstance().ReportProgress("aCkey"). This reports progress for an AchievementCondition with the key "aCkey". Making progress with an AchievementCondition counts toward the counter given in the constructor of the AchievementCondition. When the counter reaches the given value the AchievementCondition counts as unlocked. When every AchievementCondition of an Achievement is unlocked the Achievement itself counts as unlocked. When an Achievement is unlocked the AchievementManager fires the event AchievementCompleted. The AchievementManager also provides a method to delete Achievements: AchievementManager.GetInstance().DeleteAchievementByUniqueId("a") deletes the Achievement with the uniqueId "a" and AchievementManager.GetInstance().Reset() deletes every registered Achievement and every registered AchievementCondition.

Conclusion

using sebingel.sharpchievements;

namespace MyAchievementTest
{
    public class Program
    {
        static void Main()
        {
            AchievementManager am = AchievementManager.GetInstance();

            AchievementCondition aC = new AchievementCondition("aC", "aCKey", 1);
            AchievementManager.GetInstance().RegisterAchievementCondition(aC);

            Achievement a = new Achievement("a", "The Title", "The Description", aC);
            AchievementManager.GetInstance().RegisterAchievement(a);

            am.RegisterAchievementCondition(aC);
            am.RegisterAchievement(a);

            am.ReportProgress("aCKey");
        }
    }
}

That is everything you need to track and unlock your first achievement!