- Home /
Need assistance with SubType in custom class (UnityScript or C#)
I have set up a simple item class that holds all the basic information of every item in my game(Name, Prefab path, Icon, Type, etc.) and I'm trying to work in subtypes. Currently the Type is a short enum {weapon, armor, material, item, misc}.
What I want to have is something in the class script that says "if Type.weapon, SubType enum {oneHandMelee, twoHandMelee, shield, oneHandRanged, etc.}, else if Type.armor, SubType enum {helm, cuirass, greaves, etc}.
I understand the basics, just finished my first semester of college Scripting in Unity, I'm just not sure doing this is as simple as I think it should be. Any assistance from Coding veterans would be greatly appreciated.
Answer by Maerig · May 13, 2014 at 08:51 AM
If you're talking about something like nested enums, this is not something you can do in C#. What you can do, however, is make a class for each of your enum entries (weapon, armor, etc), and make them inherit from the same class.
Interesting. I'm looking up nesting enums right now. I'm better at UnityScript than C# presently, so it doesn't need to be C#. (updated thread title)
Unfortunately enums weren't covered in class, so this is one of many new functions I'm learning on my lonesome. I'll have to test it out and see how it works. It doesn't need to be an enum so long as the functionality works. I just liked the drop-down menu in the inspector for Type and want a SubType with options that are dependent on what Type is selected.
Answer by snarf · May 13, 2014 at 10:26 AM
Instead of worrying too much about nest enums, why not make several distinct classes with different set of properties including type or socket type. Then you could make classes like Armor, Weapon, MiscItem, MagicItem, Potion etc.
What i'm trying to tell you: keep it simple :)
That's a good idea actually. Rather than "Item" class, have each item have a variable : class that is it's type, then I can have a SubType enum that's specific to that type!
Only problem is my inventory system uses and Array of the Item class I have already. Haven't had much luck using arrays with multiple item types... Guess this'll be a good chance to get that array bit working.
If you're using generic arrays (which i strongly encourage you to do) you'd have the ability to add elements of other classes as long as the class inherits the defined class.
Anyway this is probably how I would do it:
public class Item {
// your item class here
public string type;
public string category;
}
public class Weapon : Item {
// item is a weapon
// set type and category strings
}
public class Armor : Item {
// item is an armor
// set type and category strings
}
using System.Collections.Generic;
// example
void Start ()
{
// ...
List<Item> itemList = new List<Item>();
itemList.Add(new Weapon("some sword", "blah", /* various stats, etc */));
itemList.Add(new Armor("some cuirass", "blah", /* various stats, etc */));
// ...
}
again, hope this helps :)
Thanks again for the help. I'll definitely give that a shot. If it works out I'll update this as the answer. I've been looking up the issues I had with generic arrays before, and it seems my issue was not type casting correctly. Out-dated reference material in the course text.. :\
(btw, love the username. ^_^)
Your answer
Follow this Question
Related Questions
Create an array of classes, of different type. 1 Answer
Setting up an array of Items 1 Answer
Making an item class in Unity. 1 Answer
Implementing an Item system - Defining Items 1 Answer
calling a class named by a string -1 Answers