The issue is that you simply didn’t distinguish whether or not the internal loop terminated with or with out discovering the merchandise kind you had been searching for. You can repair that by introducing a brand new variable bool requirementFound
which you set to false earlier than that loop, after which set to true when discovering the useful resource.
Then, after that loop, you’ll be able to examine if requirementFound
remains to be false. When it’s, then the loop did not discover any of that merchandise kind, which suggests the recipe cannot be crafted. When it’s true, that useful resource is out there, so we are able to proceed with the following.
foreach (Merchandise requirement in tradegoodRequirements)
{
foreach (Merchandise content material in warehouse.warehouseContent)
{
var requirementFound = false;
if (content material.scripableTradegood != requirement.scripableTradegood) proceed;
// When proper content material was discovered
if (content material.scripableTradegood == requirement.scripableTradegood)
{
// If required content material quantity isnt current return
if (content material.quantity < requirement.quantity) return;
} else {
requirementFound = true;
break;
}
if(requirementFound == false) {
//useful resource is unvailable
return;
}
}
}
Nevertheless, there’s a extra concise manner of penning this if you wish to use Linq. If you happen to add the utilizing System.Linq
directive to your script, then Listing
s (and all different collections) acquire a complete bunch of helpful new strategies. Amongst them is the strategy .Incorporates(predicateFunction)
. Which in plain English tells you “Does this assortment comprise something the place this operate returns true?”. So as an alternative of the entire internal loop you are able to do this:
if (warehouse.warehouseContents.Incorporates(
(content material) => {
content material.scripableTradegood == requirement.scripableTradegood
&& content material.quantity >= requirement.quantity ;
})
) {
// This merchandise is out there in ample amount
} else {
// this merchandise shouldn't be obtainable in ample amount
}
After which there may be one other helpful technique .All
which tells you “Does this operate return true for every thing on this assortment?”. So you’ll be able to substitute that entire code with this:
if(
tradegoodRequirements.All(
(requirement) => {
warehouse.warehouseContents.Incorporates(
(content material) => {
content material.scripableTradegood == requirement.scripableTradegood
&& content material.quantity >= requirement.quantity;
}
);
}
)
) {
// recipe is craftable
}
By the best way: I assume you do make sure that the content material of a warehouse can’t comprise a number of stacks of the identical merchandise kind, proper? As a result of in case you enable that, then you cannot simply return;
after you discovered a stack with an inadequate quantity. There could be a second stack which is massive sufficient. Or maybe all of the stacks collectively are massive sufficient. That will complicate issues loads.