Dictionary
Hi, i am making Inventory system using Dictonaries. When i write my Dictionary definition it is allright, but when i write anything after it, even space, it says "The name "Dictionary" does not exist in current context". I have imported System.Collections.Generic and rest of script is fine. Any idea? Thanks
EDIT: CODE (it is on the very end of it)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryClass {
}
public class Inventory {
Slot[] inventory;
public Inventory () {
inventory = new Slot[20];
createInventory ();
}
public Inventory (int SlotCount) {
inventory = new Slot[SlotCount];
createInventory ();
}
public Slot[] Inventory2 {
get {
return inventory;
}
}
void createInventory () {
for (int slotI = 0; slotI < inventory.Length; slotI++) {
inventory[slotI] = new Slot(Item.ItemTypes.Stone);
}
}
}
public class Slot {
public enum ChangeItemCountMode {Add, Remove, Set};
int contentCount;
Item content;
public Slot () {
content = new Item(Item.ItemTypes.Empty);
contentCount = 0;
}
public Slot (Item.ItemTypes itemtype) {
content = new Item(itemtype);
contentCount = 1;
}
public Slot (Item.ItemTypes itemtype, int itemcount) {
content.ChangeItemType(itemtype);
contentCount = itemcount;
}
public void ChangeItemCount (ChangeItemCountMode mode, int number) {
int oldCount = contentCount;
if (mode == ChangeItemCountMode.Add) {
contentCount += number;
} else if (mode == ChangeItemCountMode.Remove) {
contentCount -= number;
} else if (mode == ChangeItemCountMode.Set) {
contentCount = number;
}
content.OnItemCountChanged (oldCount, contentCount);
}
public Item Content {
get {
return content;
}
}
}
public class Item {
public enum ItemTypes {Empty, Dirt, Stone};
ItemTypes itemtype;
public Item () {
itemtype = ItemTypes.Empty;
}
public Item (ItemTypes itemtype_) {
itemtype = itemtype_;
}
public void ChangeItemType (ItemTypes itemtype_) {
itemtype = itemtype_;
}
public ItemTypes Itemtype {
get {
return itemtype;
}
}
//Dummy method to override.
public virtual void OnItemCountChanged (int prevCount, int newCount) {
}
}
public class ItemList {
Dictionary<int, string, Sprite> itemDic = new Dictionary<int, string, Sprite>();
}
Lots of ideas, but none of them are useful unless we can see your code.
Answer by Dave-Carlile · Dec 14, 2015 at 01:59 PM
A Dictionary is effectively a key/value store. A key can be associated with a single value. When declaring the dictionary the key type and the value type are specified. You're trying to define 3 types.
Probably what you want is string or int for the key, and a class type for the values.
class MyDictionaryItem
{
public int SomeValue;
public string SomeOtherValue;
}
Dictionary<string, MyDictionaryItem> items = new Dictionary<string, MyDictionaryItem>();
You look the MyDictionaryItem items up by a string key (or int if you change it to that). Then you have access to the individual MyDictionaryItem class properties.