Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Every character attribute a separate object instance.

Discussion in 'Scripting' started by Hoogley, Mar 3, 2019.

  1. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    Hi,

    Complete novice here. Just started coding in Unity/C# about a week ago.

    I'm making a base unit class with a set of nine or so attributes/stats. There's likely to be ten to fifteen different variants of Unit in the game.

    In writing the code to incorporate properties for each Attribute I found I was going to end up repeating myself a lot writing the same get/set code over and over. I also wanted to avoid having to modify each individual Attribute if I decided to change the allowed value range at a later stage.

    The solution I came up with was to make a base class for the Attribute type which is instantiated for each different attribute. This means that for each instance of a unit it has nine instances of the attribute type embedded within it.

    Each Attribute object is basically just a field and a property, and has a get method and a set method in order to access it.

    I won't lie; I learnt just how much I actually didn't and still don't know trying to get this to work, but I did at least get it to function as intended by the end of it. That said, I've only made one instance of the unit game object, and the end goal is to have a few; I'd guess twenty to thirty. So, potentially 270 instances of Attribute all floating about simultaneously.

    I supposed my question is: is this a ridiculous use of object instances just to simplify code, or is it a viable solution to avoid needless repetition?

    Thanks in advance for useful feedback.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So what's the advantage over having the values in a base class that all units derive from? Or, alternatively, having a single Attributes component that defines all of them and attaching that to each unit? Why make each strength, dexterity, etc. its own generic attribute instance?
     
  3. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    Good question.

    If I understand things correctly, you should avoid rewriting code wherever possible. If I make a property for each individual attribute then I have nine slight variations of this piece of code repeated in the Unit class:


    public class UnitAttribute
    {
    private int _maxValue = 9
    private protected int? _attribute = null;
    public int? Attribute
    {
    get
    {
    return _attribute;
    }

    set
    {
    if (value != null)
    {
    if (value <= 0)
    {
    _attribute = 1;
    }
    else if (value > _maxValue)
    {
    _attribute = _maxValue;
    }
    else
    _attribute = value;
    }
    else
    return;
    }
    }


    So, I figured why not make a separate class for Attribute, and instantiate it for each use. Seemed a way to avoid repeating myself. Needless overkill?

    Again, I've just started using Unity and C#, so I don't know if the advantage of extracting out code in this way is outweighed by some gross oversight like resource usage, or if it's even just pointless faffing about.

    This is, in fact, the reason for my question. I wanted to know if I was going about things the wrong way, or if this is a totally viable solution.

    What do you think?
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    You are talking about C# objects, not unity GameObjects, right? Creating your own data containers is a good use for C# classes and having an attribute class is perfectly reasonable. As you work on your game, you'll probably find it even more useful than you first thought.

    Also, you referred to your attribute class as a "base class", so I think GroZZleR was just trying to make sure you weren't planning on making a separate, derived class for each and every attribute.
     
    Last edited: Mar 5, 2019
  5. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    Thanks, kdgalla.

    Yes, it's a C# object, not a game object, and each attribute is an instance, not a derived class. Sorry for my poor use of terms.

    That's really good to know. It was partly an endeavour in trying to better understand C# as well as solve a problem. I'm glad it turned out to be a viable solution

    Cheers.