List check argument of item
Hi, i'm need simple inventory to store few stuff, it's working, however i would like to use simpler method to check quantity of exaclty that item. Here is code:
public class Items
{
public string name;
public int itemID;
public int quantity;
}
public List<Items> inventory = new List<Items>();
//set up few items
//add quantity to existing item
public void AddItem(int idForItem)
{
if(inventory.Exists(x => x.itemID == idForItem))
{
foreach(Items i in inventory)
{
if(i.itemID == idForItem)
i.quantity ++;
}
}
}
But checking whole items is no needed, I know which item i wanna check and I know it's exist already in inventory so would like to use something simple like this:
inventory.itemID(0).quantity ++;
but it's not working like that.
Answer by Masterio · Mar 09, 2016 at 02:14 PM
You should use the Dictionary.
Keep item id as key and item object as value:
Dictionary<int, Item> inventory = new Dictionary<int, Item>();
then you can simply get the item by the id rally fast:
if(inventory.ContainsKey(item_id))
inventory[item_id].quantity ++;
Thanks, it works :) However I get hard error while using your "if" method and I could'n solve it with google. However it's working if I use this.
if(inventory.Contains$$anonymous$$ey(int item_id))
But I may use it inside inventory script. I would like ask you to help me if i want to check do I have item from other script, for example:
public Inventory my_inventory; // dropped from inspector
void $$anonymous$$yStuff()
{
if(my_inventory)...
{
// ok
}
You can add to the Inventory class a method like: [it search the items dictionary]
public bool IsItemExists(int itemID)
{
return items.Contains$$anonymous$$ey(itemID);
// you should check the quantity if you are not removing from the list items with quantity = 0.
}
You can add method like this if you need an item handler.
public Item GetItemById(int itemID)
{
if(items.Contains$$anonymous$$ey(itemID))
return items.[itemID];
return null;
}
And now you can check if item exists in inventory instance with:
if ( my_inventory.IsItemExists(item_id))
{
// item exists
}
else
{
// item not found
}
or:
Item item = my_inventory.GetItemById(item_id);
if (item != null )
{
// item exists, do something with item f.e.
item.quantity++;
}
else
{
// item not found
}
PS. Code is not compiled.
Thanks, I've checked first method and it's working fine. Interesting solution :)
Answer by RoofTurkey · Mar 09, 2016 at 02:15 PM
That is because you're accessing an integer (itemID) as if it is a list. However your list is called 'inventory'.
Therefore the correct syntax would be
inventory(0).quantity++;
However, then it will not be checking what the itemID is and therefore won't solve your problem.
Before going any further from here I would like to ask why the position in the list can't be the items ID? So for example, the first item would have the ID of '0'.
Answer by Kamil1064 · Mar 17, 2016 at 03:54 PM
Sorry but i want ask you for one more thing :) how to set my int in other script based on an item's quantity? Something like;
private int number;
//somewhere in code
number = my_Inventory(item_id).quantity;
number = my_inventory(0).quantity;
Your answer
Follow this Question
Related Questions
int variable from dictionary? 0 Answers
inventory and item system where every item need its own use function 0 Answers
How to add "Properties" to items? 1 Answer
Add Item to inventory - Shop UI intro and setup 0 Answers
Picking up various types of items? 0 Answers