Inventory Add Item error
So I wrote an inventory script, based on a list to store my items in. I've made a function to add the item, where I pass in an id, and an amount. In the function I check multiple things. First I check if I even have anything in my inventory, if I don't, it's easy. I just add the given item as the very first item in my list. Second I check if I already have the item in my inventory and if it's stackable. I do that with a bool function:
bool doesExist(int id)
{
for(int i = 0; i < inventoryCount; i++)
{
if(inventory[i].itemID == id)
{
existingSlot = i;
return true;
}
}
//existingSlot = inventoryCount -= 1;
//Reset inventoryCount
return false;
}
If I already have one of these items in my inventory and it's stackable, I just add the given amt to the total amount for that inventory slot. If however, I do not have this item in my inventory, or it's just not stackable, I have an else statement. In the else statement I add a new item with the given id and amount. It all works great.
But... I made a crafting system, where I find a recipe (premade), and then I loop through my inventory to check if I have all the item. If I do, I call the addItem function. This all works great the first time, but the second time I try to craft, it adds the item with the id one below (so say if I craft an axe with the id of 4, second time I craft the axe, it adds item with id of 3)
Crafting System: public void SelectRecipe(string name) { for(int i = 0; i < recipes.recipes.Length; i++) { if(recipes.recipes[i].name == name) { selectedRecipe = recipes.recipes[i].recipe; index = i; //print(selectedRecipe[0]); CheckforItems(); break; } } }
void CheckforItems()
{
//print("Checking for items");
//print(inv.inventory[inv.inventoryCount-=1].itemName);
string[] first = selectedRecipe[0].Split(';');
string[] second = selectedRecipe[1].Split(';');
string[] third = selectedRecipe[2].Split(';');
for (int i = 0;i < inv.inventoryCount; i++)
{
if (inv.inventory[i].itemName == first[0] && inv.inventory[i].itemAmt >= int.Parse(first[1]))
{
firstSlot = i;
requiredFirst = true;
}
if (requiredFirst && inv.inventory[i].itemName == second[0] && inv.inventory[i].itemAmt >= int.Parse(second[1]))
{
secondSlot = i;
requiredSecond = true;
}
if(requiredSecond && inv.inventory[i].itemName == third[0] && inv.inventory[i].itemAmt >= int.Parse(third[1]))
{
//Remove first item(s)
inv.inventory[firstSlot].itemAmt -= int.Parse(first[1]);
//inv.slots[firstSlot].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[firstSlot].itemAmt.ToString();
//Remove second item(s)
inv.inventory[secondSlot].itemAmt -= int.Parse(second[1]);
//inv.slots[secondSlot].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[secondSlot].itemAmt.ToString();
//Remove third item(s)
inv.inventory[i].itemAmt -= int.Parse(third[1]);
//inv.slots[i].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[i].itemAmt.ToString();
//Add Item to inventory
AddItemToInv(recipes.recipes[index].id);
//print(recipes.recipes[index].id);
}
}
}
void AddItemToInv(int id)
{
//print(id);
inv.AddItem(id, 1);
requiredFirst = false;
requiredSecond = false;
firstSlot = 0;
secondSlot = 0;
thirdSlot = 0;
}
Inventory addItem function: public void AddItem(int id, int amt) {
//If we don't have anything in our inventory
if(inventory.Count == 0)
{
inventory.Add(database.items[id -= 1]);
inventory[0].itemAmt = amt;
//Visual
slots.Add(Instantiate(slotPrefab));
slots[0].transform.SetParent(inventorySlots.transform);
GameObject itemObj = Instantiate(itemPrefab);
itemObj.transform.SetParent(slots[0].transform);
itemObj.GetComponent<Image>().sprite = database.items[id].itemIcon;
//Reset id
id = 0;
//print(id);
itemObj.transform.GetChild(0).GetComponent<Text>().text = inventory[0].itemAmt.ToString();
return;
}
//If we already have one of these items in our inventory
if (doesExist(id) && database.items[id-=1].stackAble)
{
print(id);
inventory[existingSlot].itemAmt += amt;
//Visual
slots[existingSlot].transform.GetChild(0).GetChild(0).GetComponent<Text>().text = inventory[existingSlot].itemAmt.ToString();
id = 0;
return;
}
else
{
//print(id);
inventory.Add(database.items[id -= 1]);
//Reset id
//id += 1;
inventory[inventoryCount].itemAmt = amt;
//print(database.items[id].itemName);
//Visual
slots.Add(Instantiate(slotPrefab));
slots[inventoryCount].transform.SetParent(inventorySlots.transform);
GameObject itemObj = Instantiate(itemPrefab);
itemObj.transform.SetParent(slots[inventoryCount].transform);
itemObj.GetComponent<Image>().sprite = database.items[id].itemIcon;
//print(id);
//Reset id
id = 0;
itemObj.transform.GetChild(0).GetComponent<Text>().text = inventory[inventoryCount].itemAmt.ToString();
return;
}
}
The error seems to be located in the last part of the addItem function. The id changes, and I have no idea why. Can anyone help me pls? :3
In your AddItem()
function, you decrement id
in several places (i.e. id -= 1
). Are you sure that this is what you want? Shouldn't it be id - 1
?
This worked... omg thank you, I did not think of that at all. Haha I had a derp moment :P
Answer by Bokaii · Jan 17, 2016 at 01:28 PM
So like you said in the first comment, I used id -= 1, where I was supposed to use id - 1. This fixed it.
Answer by Danixos · Jan 17, 2016 at 01:11 PM
Line 24: you're decrementing id
in if
header. So even if the condition is false and else
section is executed, id
will be off by one.
Your answer
Follow this Question
Related Questions
Need help with 3d inventory system 0 Answers
List instance is always the same as the blueprint ? 0 Answers
c# foreach does not work 0 Answers
Removing objects with children from lists 0 Answers
Removing item from list makes it appear at the bottom ? 1 Answer