- Home /
How to stack items into your inventory? unsolved
100+ views, unsolved.
inventory is a list of items being populated from a chest loot. all items i am putting into it would be stackable, but how would one increase the number of an item than keep putting another of same type into that inventory? i know it would need a for loop. and a method to state if same item. like the item name or item id. it deals with increasing a number in the stack. I am not sure how to begin to do that in C#.
Dictionary doesn't fit what i, cause that only creates one instance not more than one. if i am wrong can someone tell me differently. When i tried that all it did for me. Wanted stacking like quantity 1 to 99. 2 or more stacks in the inventory. how do i tell if one list to another that item exists to stack it, than if that item has reached max quantity to begin another.
List<Item> loot = new List<Item>();
List<Item> inventory = new List<Item>()
void Start()
{
loot.Add(ItemGenerator.AddMeleeWeapon(ItemsNames.Sword, 10));
loot.Add(ItemGenerator.AddShield(ItemsNames.LargeShield, 5));
}
public void LootWindow()
{
int cnt = 0;
for(int y = 0; y < 5; y++)
{
for(int x = 0; x < 1; x++)
{
if(cnt < loot.Count)
{
if(GUI.Button(new Rect(10 + ( x * 50 ), 20 + ( y * 50 + y * 5 ), 190, 50), loot[cnt].quantity)
{
if(!inventory.Contains(loot[cnt]))
inventory.Add(loot[cnt]);
else
loot[cnt].quantity += loot[cnt].quantity;
loot.RemoveAt(cnt);
}
}
{
GUI.Button(new Rect(10 + (x * 50), 25 + y * 50, 190, 50), (x + y * 1).ToString());
}
cnt++;
}
}
}
public void InventoryWindow()
{
for(int i = 0; i < inventory.Count; i++)
{
if(inventory[i].quantity > inventory[i].maxQuantity)
inventory[i].quamtity = inventory[i].maxQuantity;
GUI.Button(new Rect(10, 50, i * 50, 50, 50), inventory[i].quantity);
}
}
This works well. My item generator changes the quantity in the loot window, so that they are now showing stacked items. When i try a chest out with this and comment out the RemoveAt line in the loot window it stacks inventory fine. But when i add more than one chest, with RemoveAt commended out or not. When i get to next chest it creats a new slot in my inventory for the item. Why does it skip over the if(!inventory.Contains(loot[cnt])) when i get to the next chest? does the list create a new item when it repopulates the list from my chest script. the trimmed script below in comment doesn't show it fully, but an idea.
Does anyone know why it wouldn't see the item in next chest as the same sword as the first chest?
Beginner C#.
The code is like http://www.burgzergarcade.com/. Thanks to his tutorials i was able to start a game. I have been modifying and changing so it can become my own game. i would really like to be able to stack items, since my inventory will have infinity items. I am not sure how to start with the code to check if item is same vs quantity vs stack number.
public class Item
{
public int itemID;
public string name = "";
public int level;
public bool stackable = true;
public int quantity = 1;
public int maxQuantity = 99;
public int stack = 1;
public int maxStack = 5;
public int attack;
public int physicalDefense;
public int charges;
public int cost;
public string description = "";
public Texture2D icon;
}
public static class ItemGenerator
{
public static Weapon Add$$anonymous$$eleeWeapon(ItemsNames meleeWeaponName, int quantity)
{
Weapon meleeWeapon = new Weapon();
switch(meleeWeaponName)
{
case ItemsNames.Sword:
meleeWeapon.itemID = 1;
meleeWeapon.name = "Sword";
meleeWeapon.level = 1;
meleeWeapon.stackable = true;
meleeWeapon.quantity = quantity;
meleeWeapon.maxQuantity = 99;
meleeWeapon.stack = 1;
meleeWeapon.maxStack = 5;
meleeWeapon.attack = 10;
meleeWeapon.cost = 5;
meleeWeapon.description = "Iron Sword";
break;
default:
break;
}
meleeWeapon.icon = Resources.Load( "Item/Icon/Weapon/$$anonymous$$elee/" + "Sword" ) as Texture2D;
return meleeWeapon;
}
}
public class Chest : $$anonymous$$onoBehaviour
{
public List<Item> loot = new List<Item>();
void Start()
{
loot.Add(ItemGenerator.Add$$anonymous$$eleeWeapon( ItemsNames.Sword ));
loot.Add(ItemGenerator.Add$$anonymous$$eleeWeapon( ItemsNames.Sword ));
loot.Add(ItemGenerator.Add$$anonymous$$eleeWeapon( ItemsNames.Sword ));
loot.Add(ItemGenerator.Add$$anonymous$$eleeWeapon( ItemsNames.Sword ));
}
}
public class PC : BaseCharacter($$anonymous$$onoBehavior)
{
private List<Item> inventory = new List<Item>();
public List<Item> Inventory
{
get { return inventory; }
set { inventory = value; }
}
}
public class $$anonymous$$yGUI : $$anonymous$$onoBehaviour
{
private void LootWindow(int id)
{
for(int cnt = 0; cnt < chest.loot.Count; cnt++)
{
if(GUI.Button(new Rect(5 +(50 * cnt), 10, 50, 50), new GUIContent(chest.loot[cnt].icon)))
{
PC.Instance.Inventory.Add(chest.loot[cnt]);
chest.loot.RemoveAt(cnt);
}
}
}
public void InventoryWindow(int id)
{
int cnt = 0;
for(int y = 0; y < 15; y++)
{
for(int x = 0; x < 10; x++)
{
if(cnt < PC.Instance.Inventory.Count)
{
if(GUI.Button(new Rect(5 + ( x * buttonWidth ), 20 + (y * 50), 50, 50), new GUIContent(PC.Instance.Inventory[cnt].icon)))
{
}
}
else
{
GUI.Button(new Rect(10 + x * 50, 25 + y * 50, 50, 50), "");
}
cnt++;
}
}
}
}
This is Trimmed down to explain what i have.
Hey, here is a tutorial that show you how to create an inventory with the most common operations including stackable items: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Answer by Kajos · May 27, 2013 at 02:09 PM
In C# a Dictionary is would what you'd probably want. Add an item to the dictionary and increase it if it already exists. See http://www.dotnetperls.com/dictionary
If i use the for loop a lot in my project for these. Would it be more beneficial to add the dictionary to a list. On that page said lists for loop faster. OR is there not a notable difference when accessing to sort the dictionary a lot? Would i still be able to access the dictionary to sort and also increase the quantity if it is inside of a list?
Beginner C# programmer.
I wouldn't really worry about it if you're a beginner. Speed difference is insignificant most of the time for games. Here's an example for sorting a dictionary: http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value There's no real difference to using a foreach and a for, other than for gives you the position (i) in the list as well, sometimes it can be handy (you can have that with foreach also, but things can get messy).
Dictionary will only leave me have 1 stack, but i wanted an inventory that could have 2 or more stacks with quantity 99. Dictionary will not work, since it will only allow one instance.Way to check if loot list item id/name exists in inventory list? these are Two different lists, not same list. So that if the item already exists in 2nd list i can check if the quantity is less than max, also if stacks is less than max stacks in the inventory. The problem i need help is to tell if the item already exist in the second list. Which i do not understand.
Beginner C# programmer.
You can make a dictionary with a list as value if that helps. With a dictionary you could just use dictionary["item"].size() > 0 for example, where dictionary consists of a list as value. $$anonymous$$aybe you can sketch how you want it, because this is how I think how it is best, but it always depends on your situation.