Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check what items do the player has

Discussion in 'Scripting' started by MikeyJY, May 21, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I want to create a building system based on blueprints. I created everything but I need some logic.
    I have an small leaf shelter made of 9 sticks and 19 leaves.
    I implemented it like this:
    Code (CSharp):
    1. public GameObject[] ConstuctionObjects;
    2. public int[] PlacedObject;
    3. public int[] MaxItems;
    4. public bool[] finished;
    5. public int index;
    6. public GameObject[] TotalObjects;
    7. public Vector3 positions;
    8. public Quaternion rotations;
    9. public Vector3[] scales;
    10.  
    11. public GameObject currentConstructionObject;
    12.  
    13. public GameObject currentConstructionObject;
    14.  
    15. void Update(){
    16.    if(nearBlueprint){
    17.       foreach (GameObject obj in ConstructionObjects)
    18.             {
    19.                 if (PlacedObjects[Array.IndexOf(ConstructionObjects, obj)] == maxObjects[Array.IndexOf(ConstructionObjects, obj)])
    20.                 {
    21.                     finished[Array.IndexOf(ConstructionObjects, obj)] = true;
    22.                
    23.                 }
    24.             }
    25.    
    26.       foreach (bool boolean in finished)
    27.             {
    28.                 if (boolean == true)
    29.                 {
    30.                     currentConstructionObject = ConstructionObjects[Array.IndexOf(finished, boolean) + 1];
    31.                     break;
    32.                 }
    33.             }
    34.       if(Input.GetKeyDown(KeyCode.C)){
    35.         if(Inventory.ContainsitemWithTag(currentConstructionObject.tag)){
    36.             GameObject PlacedObject = Instantiate(currentConstructionObject, positions[index], rotations[index]);
    37.             PlacedObject.transform.parent = transform;
    38.             PlacedObject.transform.localScale = scales[index];
    39.         }
    40.       }
    41.       if(index == TotalObjects.Length){
    42.          //finished
    43.          Instantiate(FinalBuliding, transform.position, transfrom.rotation);
    44.          gameObject.SetActive(false); //disable the bliueprint ghost
    45.       }
    46.    }
    47. }
    Let me explain this because the way of coding is a little bit weird.(Maybe I missed some brackets because I selected only the important parts)
    ConstructionObjects[] array stores the gameobjects that are used for a building. In this case:
    ConstructionObjects[0] is Stick
    ConstructionObjects[1] is Leaf


    PlacedObjects[] array stores the count of placed objects of a specific construction object(the indexes match). in this case:
    PlacedObjects[0] is how many sticks you placed
    PlacedObjects[1] is how many leaves you placed

    MaxItems[] array stores the items required of a specific construction objects
    MaxItems[0] is how many sticks you need to place until finished[0] will be true
    MaxItems[1] is how many leaves you need to place until finished[1] will be true

    TotalObjects[] array stores the objects in the hierarchy order

    currentConstructionObject gameObject keeps track of the current construction objects so first it is Stick and when you finished placing all sticks currentConstructionObject is leaf.

    It works but with one problem.
    If you have both sticks and leaves in inventory it works nice consuming first sticks and leaves after. but if you have only leaves without having sticks, the currentConstructionObject is sticks because that's the priority order and leaves are not placed until you place only sticks.

    I can do some if statements like this:
    if(Inventory.ContainsitemWithTag("leaf") && !Inventory.ContainsitemWithTag("sticks")){
    currentConstructionObject = Leaf;
    }
    if(Inventory.ContainsitemWithTag("stick") && !Inventory.ContainsitemWithTag("leaf")){
    currentConstructionObject = Leaf;
    }
    if(Inventory.ContainsitemWithTag("stick") && Inventory.ContainsitemWithTag("leaf")){
    currentConstructionObject = Stick;
    }

    But If I have 3 or more ConstructionObjects I need to check a lot of things and also I can't use constants as Inventory.ContainsitemWithTag("stick") or Inventory.ContainsitemWithTag("leaf") because not all buildings are made of sticks and leaves.

    P.S: If you ever played TheForest or GreenHell I want to recreate their building system where you can place blueprint even if you don't have the required materials. you can place them later. not like in Rust where you need to have the items in inventory.
     
    Last edited: May 21, 2020
  2. qbflack2

    qbflack2

    Joined:
    Feb 12, 2020
    Posts:
    7
    If it is for building, simply go
    if (PlacedObjects[0] <= 1)
    {
    //Script Here
    }
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I don't undestand what do you mean could you explain?