List of different types in C# (Inventory system) -


so, i'm building inventory system in unity3d using c# scratch , i've faced following problem:

i have 3 types of inventory objects:

weapons, equipments , consumables.

so, example:

// weapon class class weapon : equipable {     public int damage }  // equipment class class equipment : equipable {      public equipmenttype equipmenttype;      public enum equipmenttype {         helmet,         armor     } }      // consumable class class consumable : item {     public consumablestat stat;     public int factor;      public enum consumablestat     {         life,         mana     } }  // equipable class class equipable : item {     public int durability }  // item class class item {     public string name; } 

now, player has list inventory.

this list, should store weapons, equipments , consumables

i've tried:

public list<t> inventory 

but receive following error on console:

the type or namespace name `t' not found. missing using directive or assembly reference?

have searched internet found solutions don't think it's correct way deal it.

someone step in issue?

what's best way create list of generic objects?

c# , unity3d new me. i'm experienced web developer migrating gaming development.

ps.: sorry poor english , in advance!

you need declare type when setting list on class, i.e. public list<t> inventory - in order store of these things in single list, need have common type or interface shared across them. suggest either creating shared interface or base type. in specific case, think base type makes sense. example:

public abstract item {     string id { get; set; }     bool isconsumable { get; }     /* etc. or whatever use in classes */ { 

this way can build inventory list way:

public list<item> inventory; 

whether or not meets needs, issue running can't define list isn't of type, , need shared base type across inventory items can create list of lowest common denominator. hope helps.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -