Search Unity

Use first empty slot if current slot is full

Discussion in 'Scripting' started by Josiah_Ironclad, Apr 14, 2020.

  1. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    Running into a little logic problem.

    I have 4 hotbar slots, and when I pick up an item, a foreach loop runs through the slots and finds the one that's currently selected. If that slot is empty, the item is put into it.

    Code (CSharp):
    1. foreach(GameObject slot in slots) {    // Highlights the currently selected slot.
    2.  
    3.     if (slot.tag == "Selected") {
    4.  
    5.         if (slot.transform.childCount == 0) {
    6.  
    7.             GameObject newItem = Instantiate(itemPrefab, slot.transform);
    8.             newItem.GetComponent<RectTransform>().sizeDelta = new Vector2(120, 120);
    9.             newItem.GetComponent<Image>().sprite = itemToStore.GetComponent<ItemData>().itemIcon;
    10.  
    11.             newItem.name = itemToStore.name;
    12.         }
    13.  
    14.         else break;
    15.     }
    16. }
    But if the slot is full, i want the loop to run again and this time insert the item into the first empty slot. Currently I solved this by just copying the loop into itself, but I want to know if there's a better way.

    Code (CSharp):
    1.  
    2. foreach(GameObject slot in slots) {    // Highlights the currently selected slot.
    3.  
    4.     if (slot.tag == "Selected") {
    5.  
    6.         if (slot.transform.childCount == 0) {
    7.  
    8.             GameObject newItem = Instantiate(itemPrefab, slot.transform);
    9.             newItem.GetComponent<RectTransform>().sizeDelta = new Vector2(120, 120);
    10.             newItem.GetComponent<Image>().sprite = itemToStore.GetComponent<ItemData>().itemIcon;
    11.  
    12.             newItem.name = itemToStore.name;
    13.         }
    14.  
    15.         else {
    16.  
    17.             foreach(GameObject nextSlot in slots) {    // Highlights the currently selected slot.
    18.  
    19.                 if (nextSlot.transform.childCount == 0) {
    20.  
    21.                     GameObject newItem = Instantiate(itemPrefab, nextSlot.transform);
    22.                     newItem.GetComponent<RectTransform>().sizeDelta = new Vector2(120, 120);
    23.                     newItem.GetComponent<Image>().sprite = itemToStore.GetComponent<ItemData>().itemIcon;
    24.  
    25.                     newItem.name = itemToStore.name;
    26.                     break;
    27.                 }
    28.             }
    29.         }
    30.     }
    31. }
    32.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Why not search for both the currently selected slot and the first empty slot in the same loop? Then, after the loop is done, check if the selected slot is empty, and if not, drop the item in the open slot.
     
  3. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    I thought of that, but I couldn't (and still can't) come up with a proper way to syntax it.
     
  4. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    Bump.

    Still can't think of anything.