Hi =)
"Logic" is, quite sadly, my kryptonite and I am not exaggerating. Ask me a negated question and I will almost always answer incorrectly, and yes, that's outside of programming. I've taken courses on logic (from "this is what AND looks like" up to university-level courses, had an awesome private teacher for it for a couple of months, sadly with almost no effect. I struggle with learning any kind of language wich seems to love negated questions (I'm looking at you, spanish). It feels like my brain is coated in logic-repelling teflon and it is driving me nuts when I try to program stuff.
When it comes to programming, logic parts are almost always brute force try and error for me. I can build simple things like "if (x = 2 && y = 2)" (though sadly, nothing more complex), though "if (x = 2 || y =3)" still needs some time to think to understand what I am going to do. Usually, I use LEGO-stones on my desk to visualize what might happen and even then, I predict the wrong result quite regularly.
(Problem starts here) But now, I am at a part of my game, which I build in Unity, where I have to check if my player got the crafting materials needed for something either in his inventory or is storage area. To me, this is not a simple x=2 && y=2 thing, it is a bit more complex and I struggle a lot with how to make it work. Some help would be dearly appreciated.
This is how it looks like in my code right now:
for (int i = 0 ; i < neededmaterials ; i++)
{
if (!HasEnoughItems(item.craftmaterial_Menge[i], item.craftmaterial[i].currentlyIn_Inventory))
{
Debug.Log("not enough materials in Inventory");
return;
}
}
This part just checks if I have enough materials in my inventory and it works great, as long as I just want to check said inventory.
Now, I want to expand it to check for the materials in my inventory, as well as my storage area and added it like this right underneath the first if-statement inside the same for-loop seen above:
else if (!HasEnoughItems(item.craftmaterial_Menge[i], item.craftmaterial[i].currentlyIn_Lagerhaus))
{
Debug.Log("Not enough materials in storage");
return; //Exit from the funtion
}
The way I understand it, it should first check if I have the materials in my inventory. If there are none, check the material in my storage. If the second fails, too, the rest of the script won't be loaded. But if I have enough materials in either my storage area or my inventory, the rest of the script will run. If I would have to write it out in logic terms, it would look rougly something like this: "If inventory && storage == false, end function" and "if inventory || storage == true, run rest of script".
Interestingly enough, when I play the game with these two parts of script, it fails to craft no matter if I have enough materals either in my inventory or my storage area. For example: I have 2 wood and 3 iron in my inventory - enough to craft a sword. I click on the sword, starting the crafting-function. It looks for the materials in my inventory: Check, all that is needed is there. Superflously, it checks for materials in my storage next: Nope, none can be found, crafting won't happen - even though enough materials are at least in my inventory. Worse, if I want to craft with an empty inventory.
The rest of the function will only load if I have enough materials in my inventory as well as my storage area.
And I don't understand why.
I tried to comment out the first return where it looks for items in my inventory, thinking that it will probably just end the search for more materals and I think it might have helped a bit, but the end-result stays the same
Could somebody please explain to me, why this happens?