Stack by quanity...? C#

[EDIT!]

Well I tryed using a for loop to add to the inventory like so …

for (int i = 0; i < playerInventory.playerI.Count; i++){
								
							if (playerInventory.playerI*.itemIndex == ItemType){*

_ playerInventory.playerI*.quanity++;_
_
}*_

* }*
Ok so this works … well I thought it did till I checked both list and checked them twice!
It keeps the same memory for both list still … playerI list at element 0 " or dirt " has quanity 2 after two pickups …
but for some reason the item list however does the same… it has quanity 2 the same as player inventory… im so confused m8’s please I love you forever!
HI thank you for viewing this topic!
I’ve made a inventory based off the berzerg grid inventory system…
The problem im running into is quantity stacking… I have the method for combining quantity everything fine! However for a quanity max and equipping and de equipping im running into some major problems that seem like they should be easy…
I’m making a mine craft kinda voxel engine game it works great…
However when I pick up dirt for example I pick up the dirt… adds it to the inventory… and adds to quantity… Well I figure out that I can use contains… to figure if there is a certain stored item the same as the item index of the dropped mineral… Sorry about the grammar its kinda hard to explain …
Basicly I have a public int inside my item class… and I have a item List class that is attached to the player that hold the index of items derived from Item Class in new list …
So I had the first idea of actually adding to the itemlist that holds the container for the premade “items” and add to that quanity which worked… However when equpping dirt for example … then after equipping it … I go and dig more dirt … it adds the same quanity stack…
So if I dug up 5 dirts… equipped 5 dirts then grab 1 other dirt … it adds the new dirt because I have dirt equipped but adds dirt quanity 5 not 1
Here is the method for adding the mineral object… the mineral object offcourse is generated from index … so if player hits dirt it passes a new int with something like mineralItem = 0 // if dirt and if sand mineralItem = 1 then it drops the new mineral prefab with a class attached called dropItems that sets its itemType int to = the mineralItem int …
here is the method for adding the item to the player inventory
public void addItem(){

* if (playerInventory.playerI.Contains(itemList.items[ItemType])){*

* itemList.items[ItemType].quanity ++;*

* }*
* else*
* {*
* playerInventory.playerI.Add(itemList.items[ItemType]);*

* }*

* Destroy(gameObject);*

* }*
And here is the item class it’s self…
using UnityEngine;
using System.Collections;

[System.Serializable]
public class Itemclass {

* public string _name;
public int _itemIndex;
public Texture2D _itemImage;*

* public bool _mineral;
public int _typeItem;*

* public int _quanity;*

* public int _quanityMax;*

* public Itemclass()*
* {*
* _name = “Unknown”;
_itemIndex = 0;
itemImage = null;
_
}*

* public int QuanityMax{*

* get {return _quanityMax;}
set {quanityMax = value;}
_
}*

* public int quanity{*

* get { return _quanity; }
set { quanity = value; }
_
}*

* public bool mineral{*

* get { return _mineral; }
set { mineral = value; }
_
}*

* public int typeItem*
* {*
* get { return _typeItem; }
set { typeItem = value; }
_
}*

* public string name{*
* get { return _name; }
set { name = value; }
_
}*

* public int itemIndex{*
* get { return _itemIndex; }
set { itemIndex = value; }
_
}*

* public Texture2D itemImage{*
* get { return _itemImage; }
set { itemImage = value; }
_
}*

* //0 - dirt*
* //1 - water*
* //2 - sand*
* //3 - ice*
* //4 - grassydirt*

* public virtual string ToolTip()*
* {*
* return name + "
" + " Type " + itemIndex;*

* }*

}
Finally here is the item list attached to the player … also the inventory list is stored in a seperate class inventory…
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Itemlist : MonoBehaviour {

* public List items = new List();*

* public Inventory inventory;*

* // Use this for initialization*
* void Start () {*

* for (int i=0; i < items.Count; i++){*
* //stackItems.Add(1);*
* }*

* inventory = gameObject.GetComponent() as Inventory;*
* }*

* // Update is called once per frame*
* void Update () {*

* }*

_ /_
_
void addItem(int itemindex){*_

* inventory.playerI.Add(items[itemindex]);*

* }*
_ */_

}
If you’re still reading here is the inventory its kinda messy…
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {

* public GUISkin mySkin;*

* #region misc*
* private int oldIndex = -1;*

* private Itemclass temp2;*
* public List playerI = new List();*

* public float resW = 0.0f;*
* public float resH = 0.0f;*

* private int thirdClick = 0;*

* private Itemclass weaponEQ;*

* private bool mineralEQ;*

* public int stackI = 0;*

* private Itemclass openWeapon ;*

* public bool MineralEQ*
* {*
* get {return mineralEQ;}*
* set {mineralEQ = value;}*
* }*

* public Itemclass WeaponEQ*
* {*
* get {return weaponEQ;}*
* set {weaponEQ = value;}*
* }*
* #endregion*

* #region inventory params*
* public float invX = 0.0f;*
* public float invY = 0.0f;*

* public float invWidth = 0.0f;*
* public float invHeight = 0.0f;*

* private Rect inventoryBox;*

* public float backgroundHeight = 0.0f;*

* public Vector3 scale;*

* public float btnX = 0.0f;*
* public float btnY = 0.0f;*

* public float buttonWidth = 0.0f;*
* public float buttonHeight = 0.0f;*

* private Rect gridBox;*

* public int inventorySize = 10;*
* private int currentInventorySize = 0;*

* private int invSX;*
* private int invSY;*

* private Matrix4x4 svMat;*
* #endregion // This block of code displays inventory paramaters that are needed to adjust the gui inventory*

* #region char params*
* public float charX = 0.0f;*
* public float charY = 0.0f;*

* public float charWidth = 0.0f;*
* public float charHeight = 0.0f;*

* private Rect charBox;*

* public float charbuttonX = 0.0f;*
* public float charbuttonY = 0.0f;*

* private int _characterPanel = 0;
private string _characterPanelNames = new string { “Equipment”, “Stats” , “Skills” };*

* private float _doubleClickTimer = 0;
private const float _DoubleClickTimer = 0.2f;
private Itemclass _selectedItem;*

* public int[] itemCAP;*
* #endregion // This block of code is the public parameter for the character GUI*

* #region display properties*
* private Texture2D[] itemIcon;*
* public int selGridInt = 0;*
private string[] inventoryNames;

* private Vector2 mousePos;*
* #endregion // This block of code contains image value arrays and assorted.*

* #region Scripts to grab*
* public PlayerIO playerinventory;*

* public MouseLook mainCam;*
* public MouseLook mainCam2;*

* public FPSInputController moveCon;*
* #endregion // This offcourse is self explained needs only to be grabbed.*

* #region inventory slots init*

* #endregion*

* private string _toolTip = “”;*

* //public int[] inventoryMax = new array();*

* //32*

* // Use this for initialization*
* void Start () {*

* //invSX = inventorySize / 2;*
* //invSY = inventorySize / 2;*

* //Debug.Log(invSX);*

* float resoX = Screen.currentResolution.width;*
* float resoY = Screen.currentResolution.height;*

* svMat = GUI.matrix; // save current matrix*
* scale = new Vector3(Screen.width/resoX, Screen.height/resoY,0);*

* #region main calls to the inventory*
* // This stores the windows resoultion …*
* inventoryBox = new Rect(Screen.width/invX, Screen.height/invY, Screen.width/invWidth, Screen.height/invHeight);*
* gridBox = new Rect(Screen.width/(invX+40), Screen.height/(invY+20), Screen.width/(invWidth + 0.5f ), Screen.height/(invHeight + 0.5f ));*
* charBox = new Rect(Screen.width/charX, Screen.height/charY, Screen.width/charWidth, Screen.height/charHeight);*
* //beltBox = new Rect(Screen.width/charX, Screen.height/charY, Screen.width/charWidth, Screen.height/charHeight);*

* //Debug.Log(gridBox);*

* //inventoryNames = new string[items.Count];*
* mainCam = GameObject.FindGameObjectWithTag(“Player”).GetComponent() as MouseLook;*

* mainCam2 = gameObject.GetComponent() as MouseLook;*
* playerinventory = gameObject.GetComponent() as PlayerIO;*
* #endregion*

* }*

* // Update is called once per frame*
* void Update () {*

* openWeapon = weaponEQ;*

* #region pause screen character inventory etc.*

* if (Screen.showCursor == true){*

* //mousePos = Event.current.mousePosition;*

* mainCam.enabled = false;*
* mainCam2.enabled = false;*
* moveCon.enabled = false;*
* }*
* else*
* {*
* mainCam.enabled = true;*
* mainCam2.enabled = true;*
* moveCon.enabled = true;*
* }*

* #endregion*

* }*

* void OnGUI (){*

* GUI.skin = mySkin;*

* //GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);*

* #region pause screen menue inventory etc …*
* GUI.skin.button.wordWrap = true;*
* if (Screen.showCursor == true){*

_ Vector2 scrollPos = new Vector2(Screen.width / 2 * invX , Screen.height/2 * invY);_

* //GUI.Box(inventoryBox,“”);*

* inventoryBox = GUI.Window(0,inventoryBox,inventoryGrid, " Inventory ");*

* charBox = GUI.Window(1,charBox,charWindow, " Character ");*

* DisplayToolTip();*

* }*

* #endregion*

* //GUI.matrix = svMat;*

* }*

* public void inventoryGrid(int windowID)*
* {*
* int cnt = 0;*

* #region Block cells for inventory*

* GUI.Box(gridBox,“bob”);*
* for (int x = 0 ; x < inventorySize; x ++ ){*
* for (int y = 0; y < inventorySize; y++){*

* if (cnt < playerI.Count){*

_ if (GUI.Button(new Rect(gridBox.x + x * gridBox.width /(inventorySize), gridBox.y + y * gridBox.height/(inventorySize),gridBox.width /(inventorySize),gridBox.height/(inventorySize)),new GUIContent(playerI[cnt].name,playerI[cnt].ToolTip()))){_

* if ((_doubleClickTimer != 0) && (_selectedItem != null)){*

* if (Time.time - _doubleClickTimer < DoubleClickTimer){
_
Debug.Log(“Double Clicked”);*

* _doubleClickTimer = 0;
selectedItem = null;
_
if (weaponEQ == null){*

* weaponEQ = playerI[cnt];*

* playerI.RemoveAt(cnt);*
* }*
* else*
* {*
* Itemclass temp = weaponEQ;*
* weaponEQ = playerI[cnt];*
* playerI[cnt] = temp;*
* }*
* }*
* else*
* {*
* Debug.Log(“reset timer”);*

* if (playerI[oldIndex].itemIndex != playerI[cnt].itemIndex){*
* temp2 = playerI[cnt];*
* playerI[oldIndex] = temp2;*
* playerI[cnt] = _selectedItem;
_selectedItem = null;
doubleClickTimer = 0;
_
//temp2 = playerI[oldIndex];*

* }*
* else*
* {*
* int storedQ = playerI[cnt].quanity + playerI[oldIndex].quanity;*

* if (storedQ < playerI[cnt].QuanityMax){ *
* playerI[cnt].quanity = Mathf.Clamp(playerI[cnt].quanity + playerI[oldIndex].quanity,0,100);*
* playerI.RemoveAt(oldIndex);*
* }*
* }*

* //Debug.Log(oldIndex);*

* }*
* }*
* else*
* {*
* _doubleClickTimer = Time.time;
if (_selectedItem == null){
selectedItem = playerI[cnt];
_
}*

* oldIndex = cnt;*

* }*
* }*

* if (cnt < playerI.Count){*
_ GUI.Box(new Rect(gridBox.x * 1.8f + x * gridBox.width /(inventorySize), gridBox.y * 1.5f + y * gridBox.height/(inventorySize),gridBox.width /(inventorySize * 2.1f) ,gridBox.height/(inventorySize * 2f)) ,new GUIContent(playerI[cnt].quanity.ToString()));
* }*
* }*
* else*
* {*
GUI.Label(new Rect(gridBox.x + x * gridBox.width /(inventorySize), gridBox.y + y * gridBox.height/(inventorySize),gridBox.width/(inventorySize),gridBox.height/(inventorySize)),“”,“box”);_

* }*

* cnt ++;*

* }*

* }*
* #endregion*

* SetToolTip();*
* GUI.DragWindow ();*

* }*

* void charWindow(int windowID){*
* _characterPanel = GUI.Toolbar(new Rect(5,25, charBox.width - 10, 25),_characterPanel,_characterPanelNames);*

* switch (characterPanel)
_
{*

* case 0:*
* equipScreen();*
* break;*
* case 1:*
* statsScreen();*
* break;*
* case 2:*
* skillsScreen();*
* break;*
* }*

* //GUI.DragWindow();*

* }*

* private void equipScreen(){*

* GUI.Button(new Rect(charBox.x/4f,charBox.y/0.5f,charBox.width / 4,charBox.height / 4.5f),“Head”);*

* if (weaponEQ == null){*
* GUI.Box(new Rect(charBox.x/15f,charBox.y/0.21f,charBox.width / 4,charBox.height / 4.5f),“Weapon”);*
* }*
* else*
* {*
* if (GUI.Button(new Rect(charBox.x/15f,charBox.y/0.21f,charBox.width / 4,charBox.height / 4.5f),new GUIContent(weaponEQ.name,weaponEQ.ToolTip()))){*

* playerI.Add(weaponEQ);*
* weaponEQ = null;*

* }*
* }*

* GUI.Button(new Rect(charBox.x/2.18f,charBox.y/0.21f,charBox.width / 4,charBox.height / 4.5f),“shield”);*
* GUI.Button(new Rect(charBox.x/4f,charBox.y/0.21f,charBox.width / 4,charBox.height / 4.5f),“Body”);*
* GUI.Button(new Rect(charBox.x/4f,charBox.y/0.13f,charBox.width / 4,charBox.height / 4.5f),“Legs”);*
* //GUI.DragWindow();*

* SetToolTip();*
* }*

* private void statsScreen(){*
* }*

* private void skillsScreen(){*
* }*

* private void SetToolTip(){*

* #region event function*
* if (Event.current.type == EventType.Repaint && GUI.tooltip != _toolTip){*

* if (_toolTip != “”){
toolTip = “”;
_
}*

* if (GUI.tooltip != “”){*
* toolTip = GUI.tooltip;
_
}*

* }*

* #endregion*
* }*

* private void DisplayToolTip(){*

* #region tool tip function*
* mousePos = Event.current.mousePosition;*

* if (_toolTip != “”){
//GUI.Box(new Rect(Screen.width /4f , Screen.height /7 , 400 , 200),_toolTip);
GUI.Box(new Rect(mousePos.x , mousePos.y, 250 , 200),toolTip);
_
}*

* #endregion*

* }*

}

There’s a number of issues here. Firstly, contains uses the object’s defined qualifier for the Equals method, for that class. You have not overridden the Equals method, and thus they will use the default implementation that is developed in the System.Object class (from which every class you will ever make inherits, through whatever chain of inheritance it uses), which only checks to see if it is EXACTLY the same object, and not a “similar” object.

To explain in simpler terms, unless you change the equality comparer that the list will use (most simply accomplished, in my opinion, by overriding the Equals and the GetHashCode methods from System.Object), using “contains” will only return if the EXACT object you’re searching for is contained in that list.

Additionally, when you actually call the “Add” method, in the “else” block in the first piece of script you have pasted, up there, you call it like this:

playerInventory.playerI.Add(itemList.items[ItemType]);

The issue here is that the parameter you’re using is the object that is already contained in the itemlist, of that type. Additionally, when you changed the “quantity” variable for the item, the item in the “itemList” had its quantity changed, as well, because you were using a reference to the same object.

Honestly, your inventory system seems pretty over-complicated to fully debug, right now, but I would start by finding a better way of checking if the inventory already has a specific item in it.

Hint: All “contains” does for a standard list is to loop through it, object-by-object and check each one against the object being searched for. You could very easily create a similar loop, yourself, and just use a simple conditional based on some manner of id that is unique to the item, to check if that item type is contained in the list.

[Edit] You’ll also probably want to ensure that when you add a new item stack to the inventory that you make a duplicate object of the one in the itemlist, and don’t actually edit the items in the itemlist, by editing the references that point back to their respective objects.

Anyone is interested still I wrote up a strange method in my add item method to check if an item contains the itemindex or " ID " if so it does an operator check then adds or subtracts here it is …

					int itemT = 0;
		
					for (int a = 0; a < playerInventory.playerI.Count; a ++){
						if (playerInventory.playerI[a].itemIndex == ItemType){
							itemT = 1;
						}
					}
		
					if (itemT == 0){
						playerInventory.playerI.Add(loot[0]);
					}
					else
					{
						for (int i=0; i < playerInventory.playerI.Count; i++){
								
								if (playerInventory.playerI*.itemIndex == ItemType){*

_ playerInventory.playerI*.quanity ++;_
_
break;_
_
}*_

* }*
* }*
I would suggest learning a different method but this a real simpleton if you will on how to loop out… with this one …
[EDIT!]
Actually excuse me I finally figured out how to use findIndex that should … work much better then the pervious solution …
int index = playerInventory.playerI.FindIndex(item => item.itemIndex == ItemType);

* if (index != 0){*

* #region This block set creates new mineral Items*
* Itemclass newItem = new Itemclass();*
* newItem.name = itemList.items[ItemType].name;*
* newItem.itemIndex = itemList.items[ItemType].itemIndex;*
* newItem.itemImage = itemList.items[ItemType].itemImage;*
* newItem.mineral = itemList.items[ItemType].mineral;*
* newItem.typeItem = itemList.items[ItemType].typeItem;*
* newItem.quanity = itemList.items[ItemType].quanity;*
* newItem.QuanityMax = itemList.items[ItemType].QuanityMax;*

* #endregion*

* playerInventory.playerI.Add(newItem);*

* }*
* else*
* {*
* for (int i=0; i < playerInventory.playerI.Count; i++){*

_ if (playerInventory.playerI*.itemIndex == ItemType){
playerInventory.playerI.quanity ++;
break;
}*_

* }*
* }*