Home Game Development unity – How to find out whether or not a number of crafting necessities are met?

unity – How to find out whether or not a number of crafting necessities are met?

0
unity – How to find out whether or not a number of crafting necessities are met?

[ad_1]

The drawback is that you simply didn’t distinguish whether or not the internal loop terminated with or with out discovering the merchandise sort you had been searching for. You might 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 may test if requirementFound continues to be false. When it’s, then the loop did not discover any of that merchandise sort, which implies the recipe cannot be crafted. When it’s true, that useful resource is obtainable, so we are able to proceed with the following.

foreach (Item requirement in tradegoodRequirements)
{
    foreach (Item 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;
        }
    }
}

However, there’s a extra concise means of penning this if you wish to use Linq. If you add the utilizing System.Linq directive to your script, then Lists (and all different collections) achieve an entire bunch of helpful new strategies. Among them is the tactic .Contains(predicateFunction). Which in plain English tells you “Does this assortment include 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.Contains(
   (content material) => { 
       content material.scripableTradegood == requirement.scripableTradegood 
       && content material.quantity >= requirement.quantity ;
   })
) {
    // This merchandise is obtainable in enough amount
} else {
    // this merchandise is just not obtainable in enough amount
}

And then there may be one other helpful technique .All which tells you “Does this operate return true for all the things on this assortment?”. So you may substitute that entire code with this:

if( 
    tradegoodRequirements.All(
       (requirement) => {
           warehouse.warehouseContents.Contains(
               (content material) => { 
                  content material.scripableTradegood == requirement.scripableTradegood 
                   && content material.quantity >= requirement.quantity;
               }
           );
        }
    )
) {
      // recipe is craftable
}

By the way in which: I assume you do be sure that the content material of a warehouse cannot include a number of stacks of the identical merchandise sort, proper? Because should you permit that, then you can’t simply return; after you discovered a stack with an inadequate quantity. There may be a second stack which is massive sufficient. Or maybe all of the stacks collectively are massive sufficient. That would complicate issues so much.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here