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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Class holding integers vs a List of integers

Discussion in 'Scripting' started by Lamprey, Jul 29, 2015.

  1. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    I have a class called character. This class holds the name, id, attributes, mentalities, and psyche of the character. Should I make seperate classes that hold integers for attributes, mentalities, and pysche. Or put lists inside the Character class. If there is a totally different way I could implement this, I'm willing to learn.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    can you show how you have it now ? What attributes will you have or mentalities ? little bit more context. :)
     
  3. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    Essentially, for attributes it's 4 integer values, mentalities 4 integer values, and psyche 4 integer values each in there own class. Now that I think of it, this may be a bit of memory hog and not be efficient. Instead of lists I could use an array of integers. Not sure though.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    If you have a fix size you can use Arrays instead.
     
  5. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    Is there any advantages to using classes? Or even lists? I know it will be a fixed size but if I use a class won't I be able to reference the object later.

    Edit: No matter what kind I use it needs to be contained within the base class Character, as it will be a holder for the character's stats.
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Something like that?

    Code (CSharp):
    1. public class Character {
    2.     public string name;
    3.     public int id;
    4.     public int[] attributes = new int[4];
    5.     public int[] mentalities = new int[4];
    6.     public int[] psyche = new int[4];
    7. }
     
  7. Lamprey

    Lamprey

    Joined:
    May 26, 2015
    Posts:
    36
    Wow! That's way simpler. And it works way better. Thanks!