Im making the dying light inventory/blueprint system in order to learn more about programming which I tend to do a lot. However I cant figure out how to check my players inventory to make sure they have the correct amount of materials
public void CraftItem(CraftingRecipe _recipe) {
foreach(ItemIngredient _ingredient in _recipe.ingredients) {
foreach(InventorySlot _slot in partSlots) {
if(_ingredient.item == _slot.item.GetComponent<Item>() && _slot.amount >= _ingredient.amount) {
Debug.Log("Can craft");
}
}
}
}
This is what I have so far and it works but the player but it still works if the player only has the correct amount of one of the items, they dont need to have all the items for it to work
The best way I’ve found to do these kinds of checks is to assume they can until you can prove they can’t, as that lets you bail out as early as possible in the checks.
public void HasIngredients(CraftingRecipe _recipe) {
foreach(ItemIngredient _ingredient in _recipe.ingredients) {
foreach(InventorySlot _slot in partSlots) {
if(_ingredient.item == _slot.item.GetComponent<Item>() && _slot.amount < _ingredient.amount) {
return false; // I changed the check from >= to <
}
}
}
return true;
}
public void CraftItem(CraftingRecipe _recipe) {
if(HasIngredients(_recipe)) {
// good to go
}
}
This may not be the exact correct solution as I’m not sure how your inventory/slot system works, but hopefully you get the idea.
Cheers mate, ill give it a try n let ya know
Same problem occurs. My recipe says 1x battery and 5x aerosol, you can craft the item if you have 1 battery, you dont need any of the aerosol. Its the same issue, the recipe is only checking if one item fits the criteria
Right, I missed that you’re not actually checking if the item is there, so the current checks would only fail if the item was present but the quantity was wrong. This should work:
public void HasIngredients(CraftingRecipe _recipe) {
foreach(ItemIngredient _ingredient in _recipe.ingredients) {
// tally the quantity from all slots
int quantity = 0;
foreach(InventorySlot _slot in partSlots) {
if(_ingredient.item == _slot.item.GetComponent<Item>())
quantity += _slot.amount;
}
// do we have enough?
if(quantity < _ingredient.amount)
return false;
}
return true;
}
public void CraftItem(CraftingRecipe _recipe) {
if(HasIngredients(_recipe)) {
// good to go
}
}
2 Likes
Your an absolute legend, that worked perfectly, thanks so fucking much!
2 Likes
Just another question on a similar topic, does anyone know how to check an a variable inside all arrays for the smallest number. Im trying to get the amount of items you can craft, by checking the ingredients to see which number has the smallest amount.
public bool HasIngredients(CraftingRecipe _recipe) {
foreach(ItemIngredient _ingredient in _recipe.ingredients) {
int quantity = 0;
foreach(InventorySlot _slot in partSlots) {
if(_ingredient.item == _slot.item.GetComponent<Item>()) {
quantity += _slot.amount;
// THIS IS WHERE I GET THE INGREDIENT AMOUNT
_ingredient.craftableAmount = (int)Mathf.Floor(_slot.amount / _ingredient.amount);
// I NEED TO CHECK THE ALL THE INGREDIENTS FOR THE SMALLEST NUMBER
}
}
if(quantity < _ingredient.amount) {
return false;
}
}
return true;
}[
/code]