Hey guys,
Recently I’ve been trying to make an inventory system at the moment I have 5 categories of items inheriting from item class and the following way they have been initialized. First off I was wondering if these was a good way to how all items in the first place as I think it would take up a lot of RAM??
public List allItems = new List();
[SerializeField] Sprite[] weaponImagesInv;
[SerializeField] Sprite[] armorImagesInv;
[SerializeField] Sprite[] aidImagesInv;
[SerializeField] Sprite[] miscImagesInv;
[SerializeField] Sprite[] ammoImagesInv;
[SerializeField] Sprite[] weaponImages;
[SerializeField] Sprite[] armorImages;
[SerializeField] Sprite[] aidImages;
[SerializeField] Sprite[] miscImages;
[SerializeField] Sprite[] ammoImages;
[SerializeField] GameObject[] bulletPrefabs;
void Start () {
allItems.Add(new Weapon(0.000f, "Fists", weaponImagesInv[0], weaponImages[0], 1, 1, Weapon.WepType.Melee, Weapon.WepSubType.None, 1, 1));
allItems.Add(new Weapon(0.001f, "Swag Ray", weaponImagesInv[1], weaponImages[1], 1, 1, Weapon.WepType.Ranged, Weapon.WepSubType.AssaultRifle, 1, 0.1f, 1, 1, 10, 1, bulletPrefabs[0]));
allItems.Add(new Armor(1.000f, "Helmet for your Head", null, 1, 1, Armor.ArmorType.Head, 0, 1));
allItems.Add(new Aid(2.000f, "Name of Some Medicine", null, 1, 1, 1, 1));
allItems.Add(new Misc(3.000f, "Swagger", null, 1, 1500));
allItems.Add(new Ammo(4.000f, "Standard", null, 0, 1));
allItems.Add(new Ammo(4.001f, "MoreDamage!", null, 0, 5));
}
Secondly when I try and access the these items I get referred to the item created by the previous way instead of the item in the inventory, this is a result of instances I’m pretty sure but I’m not sure how to fix this issue.
public List<Item> itemInv = new List<Item>();
public InitialiseItems allInv;
[SerializeField] GameManager gm;
float weight = 0;
public void AddItem(Item i){
if(itemInv.Contains(i))
itemInv[itemInv.IndexOf(i)].quantity += i.quantity;
else{
itemInv.Add(i);
}
gm.DisplayNotification(i.itemName + " was added");
}
public void RemoveItem(Item i){
if(itemInv.Contains(i)){
if(i.quantity < itemInv[itemInv.IndexOf(i)].quantity){
itemInv[itemInv.IndexOf(i)].quantity -= i.quantity;
}
else{
itemInv.Remove(i);
}
gm.DisplayNotification(i.itemName + " was removed");
}
}