Skip to content

Achievement

Fridh edited this page May 6, 2024 · 7 revisions

Achievements are stored in two locations: first in their own unique classes (containing title, description, and unique logic), as well as in the Storage class keeping track of what achievements have been reached. The developer does however not need to interact directly with the Storage class as this goes via the Achievement's own class.

Achievement Classes

Each achievement uses the achievement abstract declared in AchievementAbstract.cs which holds a set of variables and methods. It sets a foundation for how each achievement class should be constructed but leaves room for more complex progress (session-based).

An achievement has the following main variables:

protected int     Index;          // Index of the achievement (must be unique).
protected string  Title;          // Title of the achievement.
protected string  Description;    // Description of the achievement.
protected bool    IsAchieved;     // If the achievement is archived or not.
protected int     Progress;       // Progress of the achievement.
protected int     MaxProgress;    // Maximum Progress of the achievement (Progress = MaxProgress -> archived).
private string    SpritePath;     // Full sprite path.

"Progress" and "MaxProgress" are used to add support for non-binary achievements. "Index" has to be unique and should match the filename to easier understand how that specific achievement maps to the progress stored in the Storage class.

Storage Class

The storage class has three methods made for achievements. All these functions are seen in the example snippet below:

# Example
Storage.SetAchievementProgress(0, 8);
Storage.SetAchievementArchieved(0, true);
Storage.GetAchievement(0);

The method Storage.SetAchievementProgress/2 is used for keeping track of the progress of more complex achievements. Such as "Survive 10 times".

Creating A New Achievement

When creating a new achievement, follow these steps:

  1. Create a new class file called "Achievement[Index number].cs" in the achievement-folder.
  2. The content of the files should look something likes this:
public class AchievementZero : AchievementAbstract {
    public AchievementZero() {
        Index = 0;
        Title = "Master Escapist";
        Description = "Escape 10 times.";
        MaxProgress = 10;
        SetSpritePath("Assets/Resources/Images/baseline_person_white_icon");
    }
}
  1. Add an empty game object inside the "Script" object in the achievement scene and map it to the new script.
  2. Increase the number of achievements to store in the JSON file by going to Storage.cs and adjusting StorageData class.

Clone this wiki locally