Search Unity

C# Equipment/Inventory Accessing Children Classes

Discussion in 'Scripting' started by pirateTrace, Jan 30, 2015.

  1. pirateTrace

    pirateTrace

    Joined:
    Jan 30, 2015
    Posts:
    3
    Hey Everyone!

    I wanted to create an equipment class that holds all types of equipment (e.g. weapons, shields, helms, foot gear... ) and I also wanted the Inventory to hold all types of items and equipment. I'm new to C# and I've had a hard time figuring out how to get the child of a parent class. The following is the items, equipment and weapon classes:

    Code (CSharp):
    1. namespace itemCreator{
    2.  
    3.     public class items
    4.     {
    5.  
    6.         //name of item
    7.         string name;
    8.  
    9.         //The weight of the item.
    10.         int weight;
    11.  
    12.         public void setName(string xName){
    13.            
    14.             name = xName;
    15.            
    16.         }
    17.  
    18.         public string getName(){
    19.            
    20.             return name;
    21.            
    22.         }
    23.  
    24.         public void setWeight(int xWeight){
    25.            
    26.             weight= xWeight;
    27.            
    28.         }
    29.  
    30.         public int getWeight(){
    31.            
    32.             return weight;
    33.            
    34.         }
    35.  
    36.     }//END OF ITEMS
    37.  
    38.     public class equiptment : items
    39.     {
    40.    
    41.         string equiptmentType;
    42.        
    43.         //where the equiptment can be equipted.
    44.         string equiptmentPlace;
    45.  
    46.  
    47.         public void setEquiptmentPlace(string xEquiptmentPlace){
    48.            
    49.             equiptmentPlace =xEquiptmentPlace;
    50.            
    51.         }
    52.        
    53.         public string getEquiptmentPlace(){
    54.            
    55.             return equiptmentPlace;
    56.            
    57.         }
    58.  
    59.  
    60.         public void setEquiptmentType(string xEquiptmentType){
    61.            
    62.             equiptmentType =xEquiptmentType;
    63.            
    64.         }
    65.  
    66.  
    67.         public string getEquiptmentType(){
    68.            
    69.             return equiptmentType;
    70.            
    71.         }
    72.  
    73.     }//END OF EQUIPTMENT
    74.  
    75.     public class weapon:equiptment
    76.     {
    77.        
    78.  
    79.         //Two handed or single.
    80.         string handed;
    81.        
    82.         int attack;
    83.                 int damage;
    84.  
    85.         public string getHanded(){
    86.  
    87.             return handed;
    88.  
    89.         }
    90.  
    91.  
    92.  
    93.        
    94.     }
    95.  
    96. }//NameSpace
    97.  
    So in another script I'm making a function that checks if a weapon is two handed so I have

    Code (CSharp):
    1. //head[0],body[1],left Arm[2],
    2. equiptment[] charaEquiptment = new equiptment[8];
    3.  
    4. if(charaEquiptment[2].getEquiptmentType() = "weapon"){
    5. //What I bassically want to do is
    6. //bool isDoubleHanded = charaEquiptment[3].weapon.getHanded();
    7.  
    8. }
    Is it possible to access the children or should I be looking at this from a different angle? I have a really convoluted solution which involves adding List<> variables to hold each type of equipment and add a number to them for the place on the body they go but that doesn't feel like the way to go xD

    Any help is appreciated! thank you!
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You can cast it explicitly to access those specific properties.

    Code (csharp):
    1.  
    2. equiptment[] charaEquiptment = new equiptment[8];
    3.  
    4. if(charaEquiptment[2] is weapon){
    5.      var Weap = charEquiptment[2] as weapon;
    6.      //Now i can use all the weapon specific methods
    7.      bool isDoubleHanded = Weap.getHanded();
    8. }
    As a side note, I'm NOT usually hugely strict on how you case your types and variables, but I strongly recommend at least capitalizing your classes.

    Edit: missed a fairly important bolded word up there.
     
    Last edited: Jan 30, 2015
    pirateTrace likes this.
  3. pirateTrace

    pirateTrace

    Joined:
    Jan 30, 2015
    Posts:
    3
    Thank you! I read something about using var and as but it didn't make sense to me at the moment but now reading your example it makes perfect sense.

    Thank you for letting me know about my class names, I'm new to coding and I keep having to redo that sort of thing to keep up with the standards xD
     
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    The var isn't explicitly important here. It would have worked as well with
    Code (csharp):
    1. weapon Weap = charEquiptment[2] as weapon;
    I just tend to use var when you are explicitly declaring the type in the same line (in this case, we KNOW its a weapon). Not everyone likes using it.
     
    pirateTrace likes this.
  5. pirateTrace

    pirateTrace

    Joined:
    Jan 30, 2015
    Posts:
    3
    I'm a sponge; I see what you mean by the var not being nesesary. I appreciate you help Zaladur.