5
\$\begingroup\$

I am looking to track many in-game events, e.g. the score of each move, how many moves are taken, what types of moves, etc. A lot of stats can simply be tracked with a counter. In some cases I need to aggregate data in order to calculate the value (e.g. most common move). How are you tracking in-game stats for your games? How do you avoid creating a class with tens or hundreds of fields? How do you avoid littering the code with tracking invocations? How do you abstract the aggregate data so as to avoid rewriting it for each scenario?

\$\endgroup\$
5
\$\begingroup\$

The way I usually end up doing it is to first use a messaging system. Let's say that you send a message each time the player fires his weapon. Then, the weapon system (or the weapon object) acts on that message when it is received and the weapon is fired. If you then want to keep stats of when a weapon is fired, simply log the weapon.fire messages to a list somewhere. Then it would be easy to go from that list and generate statistics from that.

You could consider introducing an object that has the sole responsibility for tracking game stats. It could simply listen to all the relevant messages, and append them to their respective lists

\$\endgroup\$
  • 4
    \$\begingroup\$ +1 This is a nice solution. I would say "definitely introduce an object" instead of "you could consider", though. \$\endgroup\$ – Marton Nov 20 '12 at 11:54
  • \$\begingroup\$ Thanks for your suggestion, I ended up implementing a messaging queue and have various publishers and subscribers working with it. \$\endgroup\$ – Alex Schearer Nov 21 '12 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.