Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Check Player Quantities

Discussion in 'Scripting' started by Langdelliooo, Dec 21, 2019.

  1. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    Player Resources and Required Resources follow a similar pattern;
    Code (CSharp):
    1. public struct RequiredResources
    2. {
    3.     public Resource resource;
    4.     public int quantityRequired;
    5. }
    Code (CSharp):
    1. public class Resource : ScriptableObject
    2. {
    3.     [SerializeField]
    4.     private string resourceName;
    5.     public string GetName() { return resourceName; }
    6.  
    7.     [SerializeField]
    8.     public Sprite icon;
    9.     public Sprite GetIcon() { return icon; }
    10. }
    11.  
    The resources available to the player are as follows;
    Food, Gold, Wood, Stone

    I wish to use a system like this so I can easily add more resources as needed. When a building is placed it has a list of resources required to build it, for example a house may cost;
    Gold = 5
    Wood = 3

    With the code below this will loop through the resources and reduce the amount as required. Basically the amount the player has - the cost, then update the UI.

    Code (CSharp):
    1. public void UseResources()
    2.     {
    3.         foreach (RequiredResources requiredResources in building.GetResources())
    4.         {
    5.             foreach (ResourceManager.PlayersResources resource in player.playersResources)
    6.             {
    7.                 if (requiredResources.resource.Equals(resource.GetResourceType()))
    8.                 {
    9.                     resource.RemoveQuantity(requiredResources.quantityRequired);
    10.                 }
    11.             }
    12.         }
    13.     }
    However, before running this I want to check if the player even has enough resources. When I use a similar logic to return true and false, and the player has enough wood (true) but not enough gold (false), it will remove the amount for both. I am at such a loss as to a way to do this correctly.

    Please help!
     
    Last edited: Dec 21, 2019
  2. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    Code (CSharp):
    1.     public bool HasRequiredResources(RequiredResources[] requiredResources)
    2.     {
    3.         foreach (RequiredResources r in requiredResources) //go through all required resources
    4.         {
    5.             foreach (PlayersResources p in playersResources)
    6.             {
    7.                 if (r.quantityRequired > p.GetCurrAmount())
    8.                     Debug.Log("Has enough " + r.resource.name);
    9.                     return false;
    10.             }
    11.         }
    12.         return true; //reaching this point means that the faction has all required resources
    13.     }
    14.  
    This is what I have but it always return false, where am I going wrong? I believe this is due to the foreach inside of the foreach. But how can I run the search of the second (PlayersResources) list to return the same Resource?