here some code:
I initialize stats like this
- Code: Select all
if (!stats.Contains("FightWinRatio")) stats.Set("FightWinRatio", 0.0f);
if (!stats.Contains("FightCount")) stats.Set("FightCount", 0);
and I manipulate with data like this
- Code: Select all
DatabaseObject stats = PlayerObject.GetObject("Stats");
int fightCount = stats.GetInt("FightCount");
int wonSum = (int)(stats.GetFloat("FightWinRatio") * fightCount);
fightCount++;
// this is here only to make sure we don't divide by zero (but it shouldn't happen it anyway)
if (fightCount < 1) fightCount = 1;
stats.Set("FightCount", fightCount);
if (won) {
wonSum++;
}
stats.Set("FightWinRatio", (float)wonSum / (float)fightCount);
PlayerObject.Save();
Those are only two places I use those stats in my code.