Generic stats system for easy prototyping


© 2017 Valve Corporation.

Most games embed a statistics or attributes systems and build the gameplay around it.

But when you start prototyping a new game, you don’t know how will look like your final architecture, which algorithms you’ll use and certainly not all the variables you’ll use/need.

That’s why I was searching for a simple and flexible stats structure to start with, that would also remove the burden of cleaning your code after some feature testing (aka. I want to keep my core classes as clean as possible).

On that purpose, I wrote a stats class that implements some recurrent needs in video games. Basically, it works as an enhanced C# dictionary that handles stats operations (add, mult, sub, div) on single and multiple fields.

Pros :

  • Lightweight integration : Add one file to your project and you’re good to go
  • Easy to use : Manipulate stats as a dictionary or with custom API (allowing specific behaviors)
  • Flexible : Add/remove new stats at run-time without declaring it as a members. No need to pollute your core classes.
  • Extensible : Stats class only inherit from IDictionary interface, you can make it inherit from any concrete class of yours
  • Enumerable : As it’s a dictionary, it’s easily loop through your stats (great for early UI development)
  • Stats operators are really useful : Add, Sub, Div, Mult on multiple field helps building basic items/boost (potions, weapons, etc.)

Cons :

  • Source code inspection can be harder as you search by key usage instead of references — you might end up using a helper class (see code samples on git)
  • Subject to mistake as you can easily overwrite any stats you didn’t knew was used (bigger team, higher mistake risks)
  • Only handles floats : You must work with ID’s when referencing external resources — but that seems to be a good practice to me
  • Generic dictionary serialization can be a burden depending on your system
  • Useful but still basic (no linked stats, no clamped values, etc.) : you might need to extend core structure

Usage

Here is a code sample of how you can use this class :

Code Insight – Dirty Flag Pattern

I take advantage of this article to present a little optimization technique that you might want to reuse.

The way I implemented Hashcode computation involves iteration through the whole container, summing all key/value pair together. Relative to the size of the container, hashcode computation can become costly and if you use GetHashCode() method, you don’t want to do a full computation when your stats collection didn’t changed.

I prevent this extra computation by caching hashcode value and setting a flag (isDirty) to true when value change operations has been performed (add, set, remove).

Thereby, when GetHachCode() method is called, we only recompute the hashcode value if IsDirty is set to true. Otherwise, we return the cached value.

This technique is often refered as “Dirty Flag Pattern” and is used in various optimization context. You can learn more details about the pattern in this online book : http://gameprogrammingpatterns.com/dirty-flag.html

Get The Code

Get the full code on my git repository

Use it any way you want (MIT Licence) and feel free to contact me if you have any remark or question.

I hope sharing this can be of any use in your current and future projects.

10 Comments

  1. Kristen

    Hey just wanhted to givge yoou a quick heads up. Thhe tect
    in youir article seeem too bee rynning ooff thee scrteen inn Firefox.
    I’m nott surte iif this is a format issue orr somethijng too doo wth browser compatibility but I thougght I’d pot to lett youu know.
    Thhe layouyt loook grea though! Hoppe yoou gett
    tthe issue fuxed soon. Cheesrs It iis the bestt time too make
    a feew plans forr tthe fuhture annd it is time too be happy.
    I have lean his poat aand iif I coud I want too counsel yoou soe faqscinating issues or suggestions.
    Perhaps youu ould write subseqent artcles referring to thjs article.
    I desire tto rdad even mopre issuues approximatyely it!
    I hve beedn suring online more han thrree houres aas off late, bbut I bby noo means founhd anny attention-grabbing
    article llike yours. It iis beautiful value enogh foor
    me. In myy view, if alll webmaster and bbloggers mae exccellent conntent
    materizl ass you did, tthe weeb hall bee
    much mpre hellpful tuan ever before. http://Www.cspan.net/

    • Clément Grenier

      Hey, thank you for your comment, it cheers me up, as it’s my first article, it pumps up my motivations to do more. 🙂

      Regarding browser text issue, I thought I fixed that thing. I’m not able to reproduce the issue (tested browsers : Firefox, Edge, IE, Chrome). What are your settings (Firefox version and type of device you’re using)? I’d be glad to fix this once for all.

      My future plans are, as you said, writing more articles and some related to this one. I’m thinking about a more advanced stats system version (addressing listed issues) or a complete ability system for RPG or MOBA. And I have plenty of other topics in mind that I would like to talk about (and hopefully share some useful code).

      Have you some specific topics you’d like to hear about?

      Thanks again for commenting and I hope you’ll fix your letters issue. 😉

  2. 1

    It’s an incredibly neat write-up about Generic stats for easy prototyping.
    In fact, this subject worried me for some time and so i
    couldn’t find a bit of good information on it.
    That is definitely hwy I’m quite thankful, you helped me
    a great deal!

  3. Mitch

    Excellent blog you have got here.. It’s hard to find high quality writing like yours
    these days. I honestly appreciate individuals like
    you! Take care!! https://kennelbreeds.com/groups/driving-games-strategies-for-the-entrepreneurially-challenged/

  4. Shanice

    What’s up! Magnificent blog post! I prefer the method illustrated
    Generic stats system for easy prototyping. Additional excellent
    piece of writing produced by good editor. We appreciate your an individual article!
    Number of washes significant to check out most people works.

    Some time ago I assumed with regards to operating personalized web blog nevertheless not have day even to write down case for school considering my
    very part-time process .
    For this purpose, About how exactly to acquire paper outside of paper writing online services.

    And just make sure that you Naturally i won’t devote this
    is my funds fraudulence, While i visit this website editing service reviews where exactly I
    often see considerable and impressive critiques on numerous composing business employers

  5. Margherita

    Hey all! Terrific report! I really enjoy the way you
    illustrated Generic stats system for easy prototyping.
    Nice one for specific well-arranged content with reference to Generic stats system
    for easy prototyping! This is exactly what I realized i was finding.

    I’m a few details inexperienced with envy because I can’t write down as great as anyone.
    Text sports activity brands our company meow furthermore nervous
    since i want to publish plenty as a result of our modern illuminating system .

    There Is Nothing would like to show the connection regarding the homepage college essay writing service reviews that will help us a fantastic deal with the my brand new article
    writing drawbacks.For this reason Investigate about how use
    re-writing products and services. To find efficient retailers Cash in this incredible movie site proofreading services review where you can find a gigantic amount in-depth consumer reviews regarding different internet based writing articles agents

  6. Fannie

    cheap jersey direct.Buy cheap jersey direct from China cheap
    tickets for jersey boys factory, free shipping and easy returns also
    best service.

  7. zero up price

    Time and convenience :- Your online retailer is open 24?7. https://thecallybear.tumblr.com/post/166154616384/look-into-the-lovable-little-business-owners-on

Leave a Reply

Your email address will not be published.

Why ask?