- Home /
Custom class and hashtable
I've been trying to set up my own class, GeneralItem, and store these in a Hashtable. The scripts are as follows:
GeneralItem class
using UnityEngine; using System.Collections;
public class GeneralItem : Object {
public string itemName;
public int sellPrice;
public Texture icon;
public bool isStackable;
public string description;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
ItemList script
using UnityEngine; using System.Collections;
public class ItemList : MonoBehaviour {
public GeneralItem Turnip, Potato;
private int amountOfItems;
public GeneralItem[] itemArray;
public Hashtable itemList = new Hashtable();
//Hashtable: key is what you search, value is what is returned
// Use this for initialization
void Awake () {
createItemList();
}
// Update is called once per frame
void Update () {
}
private void createItemList() {
Turnip = new GeneralItem();
Turnip.itemName = "Turnip";
Turnip.sellPrice = 50;
Turnip.icon = Resources.Load("icons/ItemIcon") as Texture;
Turnip.isStackable = true;
Turnip.description = "Just a turnip.";
itemList.Add(Turnip.itemName, Turnip);
Potato = new GeneralItem();
Potato.itemName = "Potato";
Potato.sellPrice = 60;
Potato.icon = Resources.Load("icons/PotatoIcon") as Texture;
Potato.isStackable = true;
Potato.description = "Only a potato.";
itemList.Add(Potato.itemName, Potato);
}
public GeneralItem FindItemByName(string itemName) {
if (itemList.ContainsKey(itemName)) {
GeneralItem newItem;
newItem = itemList["Turnip"] as GeneralItem;
return(newItem);
} else {
return null;
}
}
}
However, when trying to see what value comes with "Turnip" in the hashtable, I get 'GeneralItem' instead of 'Turnip'. Also, when I try to assign a GeneralItem via the interface of Unity, neither Turnip nor Potato shows up in the list of usable GeneralItems.
My question is, how can I make sure 'Turnip' is retrieved in the hashtable instead of 'GeneralItem'?
Answer by yoyo · Jan 12, 2011 at 05:49 PM
A few suggestions and observations ...
- when you say you "get 'GeneralItem'", do you mean that's what shows up in the Inspector? or are you printing the value? I suspect this is simply the default ToString method showing you the name of the type
- if you want your items to show in the Inspector, you need to add the System.Serializable attribute to your class
- you still won't see a drop down menu with Potato and Turnip, but you could add a custom editor that uses your hash table to do so
- you might also add a ToString override method to GeneralItem to give you more information about the particular item
- I suggest you use System.Collections.Generic, and then you can use Dictionary instead of simply Hashtable -- it gives you more type safety and ease of use, no need to typecast values retrieved from the dictionary
- you probably don't need to derive from Object, just declare your GeneralItem class with no base class
- GeneralItem.Start and GeneralItem.Update will never be called, since GeneralItem is not a component (it doesn't derive from MonoBehaviour) (I wish the Unity editor had right-click > Create Script and Create Behaviour as different menu items, with different starting code templates :-)
If anything, you shouldn't ever derive from Object at all, as UnityEngine.Object is more for the base engine to use
Thanks for the tips! I'll see em through script-wise. Just to get back to your initial question in the first point. I meant that Turnip and Potato do not show when I print itemList["Turnip"] as GeneralItem.
Debug.Log(itemList["Turnip"]) is equivalent to Debug.Log(itemList["Turnip"].ToString()), and the default System.Object.ToString method just returns the name of the class. You could override it to return itemName, or more info about all the properties of your class.
@$$anonymous$$ike, good idea -- I almost wrote that you can't do that in Unity3, but of course that's not one of the newly sealed classes. When you create a new script in the editor it gives you a template that derives from $$anonymous$$onoBehaviour, and if you're new to inheritance I can see how you would want to either leave $$anonymous$$onoBehaviour or replace it with another class. But yes, in this case, no base class is needed.
And what I meant by not showing up in the Inspector: If I were to create a Public Texture , it would show up in the Inspector and I could visually choose a texture. I wanted the same for my GeneralItems. Also, can I school classes that inherit from GeneralItem still under GeneralItem (as in, when using that Dictionary)?
Your answer
Follow this Question
Related Questions
What am I doing wrong with hashtable.CopyTo? 1 Answer
Convert returning object to Hashtable 1 Answer
creating and manipulating a grid efficiently 1 Answer
Problems with Hashtables and creation order? 2 Answers
Converting String to Variable Name 3 Answers