Search Unity

Bitwise stat increases for RPG level ups?

Discussion in 'Scripting' started by dragonice501, Sep 25, 2021.

  1. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    I'm recreating an RPG where each class has guaranteed increases to attributes such as Strength, Intelligence, etc... for each level.

    Capture.PNG

    I'm new to bitwise operations but this seems like a good case to do so. Would it be possible to keep an array of integers consisting of 1's and 0's to determine whether each attribute gets increased?
     
  2. NocturnalWisp

    NocturnalWisp

    Joined:
    Oct 2, 2019
    Posts:
    62
    Hi!

    Normally for stats I would use an implementation of Scriptable Objects to store the various types of stats.

    If you are dead set on bitwise operations, you can use what is called Enum Flags. It's implemented in the C# language, and you can find documentation on it here: FlagsAttribute Class (System) | Microsoft Docs. Basically it allows you to have a compile time array container each collection of values you would like stores in a bitwise operator situation. (read the docs, it explains it better than me)

    So for your situation you would want to have a 2D array of Enum flags with the values assigned in a table fashion. You could also store the collection in a dictionary or property drawer if you prefer.

    My personal suggestion would be to use a different data structure such as Scriptable objects then you can have a collection of stats and set up a table through monobehaviours/scriptable object containers.

    Hope this helps!
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    You can do stuff like this with bitwise operators but it's a little bit of an unnecessary complication, plus it won't have any meaningful optimization, and it will be much harder to debug and reason about in the future, once you have forgotten the details of the bitfields.

    I'd stick with what @NocturnalWisp suggests, just use ScriptableObjects. They are actually first class citizen assets in your project and you can use them for all kinds of semantic meanings.

    In your specific case above you might have a ScriptableObject class that specifies what happens to each stat on a given level.

    You would create instances of this class for Level1, Level2, etc. and just slot them all into an array, look them up when you attain a new level and adjust stats accordingly.
     
    NocturnalWisp likes this.