c# how to retrive objects as values from array -
i trying create simple 'inventory' system stores items key being items name, , remaining information being stored value. however, having difficulty figuring out how read information. example, if have list of 10 items, , want select items 'type' information key 'television' outlined below, how this?
television {large, 5, 3, false, dynamic, 0.8, 20}
hashtable myitems = new hashtable(); protected virtual bool onattempt_additem(object args) { object[] arr = (object[])args; string itemtype = (string)arr[0]; string itemname = (string)arr[1]; int itemamount = (arr.length == 2) ? (int)arr[2] : 1; int itemacanhave = (arr.length == 3) ? (int)arr[3] : 1; bool itemclear = (bool)arr[4]; string itemeffect = (string)arr[5]; float itemmodifier = (float)arr[6]; int itemweight = (int)arr[7]; // enforce ability have atleast 1 item of each type itemacanhave = mathf.max(1, itemacanhave); myitems[itemname] = new object[] {itemtype, itemamount, itemacanhave, itemclear, itemeffect, itemmodifier, itemweight }; return true; }
create item class encapsulate properties:
public class inventoryitem { public string name; public string type; public int amount; public int canhave; // should consider renaming - it's unclear mean public bool clear; public string effect; public float modifier; public int weight; }
then can use dictionary store items:
dictionary<string, inventoryitem> inventory = new dictionary<string, inventoryitem>(); inventory["television"] = new inventoryitem { name = "television", type = "large", amount = 5, canhave = 3, clear = false, effect = "dynamic", modifier = 0.8, weight = 20 });
and can this:
console.writeline("type of television is: ", inventory["television"].type);
Comments
Post a Comment