does anyone have a nice burst-compatible pattern for global gameplay counters that can be updated "anywhere" fully in parallel but are generally updated relatively sparsely?
things like "damage done" etc that could be used for achievements and quests and stats and so on
the simplest solution that i'm thinking of is something like
public enum CounterId {
DamageDone,
GoldGained,
EnemyKilled,
...
// might have hundreds or even thousands of these,
// potentially with some computed indices like "DamageDone + damageType"
}
static readonly SharedStatic<NativeArray<int>> counters;
public static void IncreaseCounter(CounterId id, int amount) {
unsafe {
var pdata = (int*)counters.Data.GetUnsafePtr()
Interlocked.Add(ref pdata[(int)id], amount);
// later: detect changes using a shadow array or something
// like that in a system after completing all jobs
}
}```
* the big benefit is that i don't have to specifically declare access to it in job structs etc, it can just be used "anywhere" in both bursted and non-bursted code, and in parallel jobs etc
* theoretically this is subject to false sharing though i don't expect to run into that in practice
* adding time- and state-based conditions ("do y damage within z seconds", "kill x enemies while airborne") will get pretty hairy