Skip to content

KernelAPI reference

Class AchievementTracker

Namespace: MicroEngine.Core
Assembly: MicroEngine.Achievements.dll
Guide: Game lifecycle and time

Owns a game’s achievements: the catalog, progress, unlocking and persistence. Games decide when something happened and report it with Unlock, AddProgress or SetProgress; the tracker guarantees that an achievement unlocks (and raises Unlocked) exactly once, across sessions.

C#
public sealed class AchievementTracker

Inheritance

object ← AchievementTracker

Remarks

State lives in the SaveSlot slot of the game’s SaveStore, so each game keeps its own file. Missing or corrupt data starts from scratch (the store keeps the corrupt file aside); a file that exists but cannot be read is never overwritten during that session. Entries for ids that are no longer in the catalog are kept untouched, and new catalog entries start locked. Unlocks are written at once; plain progress is written by Flush (the game loop calls it about once a second and on exit).

Constructors

AchievementTracker(IEnumerable<Achievement>, SaveStore?)

C#
public AchievementTracker(IEnumerable<Achievement> catalog, SaveStore? store = null)

Parameters

catalog IEnumerable<Achievement>

The game’s achievements, in display order.

store SaveStore?

Where progress persists; null keeps it in memory only.

Fields

SaveSlot

The SaveStore slot that holds achievement state: “achievements”.

C#
public const string SaveSlot = "achievements"

Field Value

string

Properties

All

Every achievement in catalog order.

C#
public IReadOnlyList<AchievementStatus> All { get; }

Property Value

IReadOnlyList<AchievementStatus>

Catalog

The achievements in display order.

C#
public IReadOnlyList<Achievement> Catalog { get; }

Property Value

IReadOnlyList<Achievement>

Count

Number of achievements in the catalog.

C#
public int Count { get; }

Property Value

int

HasUnsavedChanges

True when progress changed since the last successful save.

C#
public bool HasUnsavedChanges { get; }

Property Value

bool

UnlockedCount

Number of unlocked achievements in the catalog.

C#
public int UnlockedCount { get; }

Property Value

int

Methods

AddProgress(string, int)

Adds to an achievement’s progress; unlocks it when the goal is reached. Returns true only on that unlock.

C#
public bool AddProgress(string id, int amount = 1)

Parameters

id string

amount int

Returns

bool

Flush()

Writes pending progress. Failures (read-only disk, unreadable existing file) keep it in memory and return false; the next call tries again.

C#
public bool Flush()

Returns

bool

Get(string)

The current state of one achievement; progress is capped at its goal.

C#
public AchievementStatus Get(string id)

Parameters

id string

Returns

AchievementStatus

Exceptions

KeyNotFoundException

id is not in the catalog.

IsUnlocked(string)

Whether the achievement is unlocked.

C#
public bool IsUnlocked(string id)

Parameters

id string

Returns

bool

Exceptions

KeyNotFoundException

id is not in the catalog.

Progress(string)

The achievement’s progress, from 0 to its goal.

C#
public int Progress(string id)

Parameters

id string

Returns

int

Exceptions

KeyNotFoundException

id is not in the catalog.

SetProgress(string, int)

Reports an absolute count (“rooms visited: 7”). Progress never goes down, so recounting after a new game cannot erase what was achieved before. Returns true only when this call unlocks it.

C#
public bool SetProgress(string id, int value)

Parameters

id string

value int

Returns

bool

Unlock(string)

Unlocks an achievement now. Returns true only the first time.

C#
public bool Unlock(string id)

Parameters

id string

Returns

bool

Unlocked

Raised once when an achievement becomes unlocked (never for ones loaded already unlocked).

C#
public event Action<Achievement>? Unlocked

Event Type

Action<Achievement>?