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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

When initializing an array of objects can I set the object's values?

Discussion in 'Scripting' started by astracat111, Mar 27, 2018.

  1. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Hey there,

    I'm wondering if I can set an object's values if I'm initializing an array of objects? Here's an example of what I mean...I don't want to use a list, because a character needs to have a specific number of stats and I don't want it expandable, and I want it all in the same exact order every time/not sortable:

    Code (CSharp):
    1. public namespace RPG {
    2.  
    3.     public class Stat_Base {
    4.         public enum Stat { HP, MP, AP }
    5.         public Stat stat;
    6.     }
    7.     public class Character_Stat : Stat_Base {
    8.         public int Power;
    9.     }
    10.  
    11.     public class Character {
    12.         public string name;
    13. '
    14.        //This is the part that's messed up that I don't get. I want to do something like this...
    15.        public CharacterStat[3] stats {
    16.            new CharacterStat.stat = Stat_Base.HP;
    17.            new CharacterStat.stat = Stat_Base.MP;
    18.            new CharacterStat.stat = Stat_Base.AP;
    19.        }
    20.    }
    21.  
    22. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Okay, so let's say I'm using a List instead of an array, which I'm much more used to.

    Here is a program that won't compile on dotnetfiddle.net:

    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3.     public class Stat_Base {
    4.         public enum Stat { HP, MP, AP }
    5.         public Stat stat;
    6.     }
    7.     public class Character_Stat : Stat_Base {
    8.         public int Power;
    9.     }
    10.     public class Character {
    11.         public string name;
    12.         public List<Character_Stat> Character_Stats = new List<Character_Stat>()
    13.         {
    14.             new Character_Stat().stat = Stat_Base.Stat.HP,
    15.             new Character_Stat().stat = Stat_Base.Stat.MP,
    16.             new Character_Stat().stat = Stat_Base.Stat.AP
    17.         };
    18.  
    19.    }
    20.  
    21. public class Program
    22. {
    23.     public static void Main()
    24.     {
    25.  
    26.         Character character = new Character();
    27.  
    28.  
    29.     }
    30. }
    You see what I'm getting at? I'm not sure whether there's a way to create a list of Character_Stat objects that has the enum of stat set to Stat.HP, Stat.MP or Stat.AP on initialization.

    I want it so that every time a new character is created the object's have initial values of HP, MP or AP.

    P.S, am I supposed to create a method for character creation like this? Is this the only way?

    Code (CSharp):
    1. public Character CreateCharacter(string name) {
    2.         Character character = new Character();
    3.         character.name = name;
    4.         character.Character_Stats[0].stat = Stat_Base.Stat.HP;
    5.         character.Character_Stats[1].stat = Stat_Base.Stat.MP;
    6.         character.Character_Stats[2].stat = Stat_Base.Stat.AP;
    7.         return character;
    8.     }
    P.S - Sorry for the bother but since these are just ints I'm realizing that I am being stupid and I should just make it int hp, int mp and int ap! Forgive my stupidity! : p
     
    Last edited: Mar 27, 2018
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Use a constructor like @GroZZleR suggested. A constructor is a method with the same name as the object that is called when the object is created, and it can be used to set parameters while doing so. If you don't use a custom constructor, then the compiler will just create one by default with no parameters, which is basically this line at work:
    Code (CSharp):
    1. new Character_Stat();
    You can create a constructor like this, however:
    Code (CSharp):
    1.  
    2. public class Stat_Base {
    3.    public enum Stat { HP, MP, AP }
    4.    public Stat stat;
    5. }
    6.  
    7. public class Character_Stat : Stat_Base {
    8.    public int Power;
    9.  
    10.    //Constructor
    11.    public Character_Stat(Stat stat) {
    12.       this.stat = stat;
    13.    }
    14. }
    15.  
    And now you can create a new Character_Stat object while applying the value of Stat like so:
    Code (CSharp):
    1.  
    2.       new Character_Stat(Stat.HP),
    3.       new Character_Stat(Stat.MP),
    4.       new Character_Stat(Stat.AP)
    5.  
    You could also include your "Power" variable as another parameter if necessary to set it when creating a new Character_Stat object as well.
     
    Last edited: Mar 27, 2018
    astracat111 likes this.
  5. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    Oh, nice, thanks guys this helps out a ton!