- Home /
Best way of doing Item class?
Hello! I want to make a good base-class for items so i can easily add new items and itemTypes. I was wondering if there is a way to access different types of data from an item ID, i'm sorry if i'm bad at explaining, maybe if you look at my code you can understand what i mean and maybe point me in the right direction!
What i want to be able to do is write something like this to access the data:
readItemData(001).name;
Is what i'm trying to do possible? Am i doing item classes as good as possible or is there better ways to do it?
using UnityEngine;
using System.Collections;
public class itemInfo : MonoBehaviour {
public int universalStackLimit;
public int[] IDs;
public string[] Names;
public GameObject[] Models;
public string[] Types;
public GUIContent[] IconImages;
public bool[] stackables;
public void assignItemData(int ID, string Name, GameObject Model, string itemType, GUIContent IconImage, bool stackable)
{
IDs[ID] = ID;
Names[ID] = Name;
Models[ID] = Model;
Types[ID] = itemType;
IconImages[ID] = IconImage;
stackables[ID] = stackable;
}
public void readItemData(int ID)
{
}
}
Answer by Dave-Carlile · Jan 03, 2013 at 01:56 PM
You probably want to use a class or a struct for this instead.
e.g.
struct Item
{
public int ID,
public string Name,
public Model GameObject,
public string Type,
public GUIContent IconImage,
public bool Stackable
}
Store the structs in a list using generics...
List<Item> items = new List<Item>();
Add one to the list...
Item item = new Item {
ID = 10,
Name = "AnItem",
Model = someModelReference,
Type = "Test",
IconImage = someIconImageReference,
Stackable = true
}
items.Add(item);
And so on. Look up generic arrays and structs for more info. You could also use a class rather than a struct and things would look mostly the same. Depends on your needs.
Thank you! I tried something similar with enum, but struct seems much better, this looks much cleaner than my way of doing it!
List, is not available, IList is though. It seems to work the same way, the only thing i cant get to work is items.Add(item);
Never $$anonymous$$d, added using "System.Collections.Generic;" and it worked.
Your answer
Follow this Question
Related Questions
Equipting a Item/Weapon 1 Answer
Inventory and scriptable object items - please point me in the right direction 1 Answer
Parse different types of objects from Json 1 Answer
Creating a custom List or Collection 1 Answer
Access to slot Item 0 Answers