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. Dismiss Notice

Question Trying to get Info about Slots in a Grid

Discussion in 'Scripting' started by RedCorpDE, Apr 4, 2021.

  1. RedCorpDE

    RedCorpDE

    Joined:
    Apr 11, 2017
    Posts:
    3
    Been trying to wrap my head around this for a couple of days now,
    And I can't, for the life of me, figure out, what I'm doing wrong.


    So here's the Set-Up:

    I've got a List of Slots, which are placed in a Grid,

    I then want to spawn an Item in, and set all the occupied slots to 'occupied'.


    Code (CSharp):
    1.             for (int sIndex = 0; sIndex < slots.Count; sIndex++)
    2.             {
    3.                 Debug.Log("sIndex: " + sIndex);
    4.                 if (!slots[sIndex].occupied)
    5.                 {
    6.                     Debug.Log("Found Empty Slot: " + sIndex);
    7.  
    8.                     Vector2Int currentSlotPos = slots[sIndex].gridPos;
    9.                     Debug.Log(currentSlotPos);
    10.  
    11.  
    12.                     for (int y = currentSlotPos.y; y < itemToAdd.itemSize.y; y++)
    13.                     {
    14.                         Debug.Log("Checking Row No. " + y);
    15.  
    16.                         for(int x = currentSlotPos.x; x < itemToAdd.itemSize.x + pouches[pIndex].gridSize.x * y; x++)
    17.                         {
    18.                             if ((x > (currentSlotPos.x + itemToAdd.itemSize.x * y) - 1))
    19.                                 continue;
    20.  
    21.                             Debug.Log("Setting Slot at Pos: " + x + ";" + y + " to occupied");
    22.  
    23.                             pouches[pIndex].slots[x].occupied = true;
    24.                         }
    25.  
    26.                      
    27.                     }
    28.  
    29.  
    30.                     Debug.Log("Spawning Item");
    31.  
    32.                     return;
    33.                 }
    34.             }

    Here's an example of the Inventory:
    upload_2021-4-4_2-31-24.png upload_2021-4-4_2-28-22.png

    In this example, the Slots 0,0; 1,0; 2,0; 3,0 0,1 and 1,1 get set to 'occupied'
    But only the Slots 0,0; 1,0; and 0,1; 1,1 should get set ti 'occupied'.

    How can I change my code, to not change the Slots 2,0 and 3,0 to occupied?

    Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    By using a one-dimensional array to store the slots, you're forcing yourself to combine the x and y into that single index. I think that's the part that is missing.

    Usually a one-dimensional index is calculated as:

    Code (csharp):
    1. int index = x + y * SpanWidth;
    Are you aware that you can straight-up use 2-dimensional arrays in C#? I see your slots above is likely a List, but if it can be a 2D array, they're pretty spiffy and would work well for this type of application and get you out of having to do the span multiplies.

    Code (csharp):
    1. MySlot[,] slots = new slots[ Across, Down];
    After that you just use it as
    slots[x,y]
    directly.

    If you want to know how big one of those dimensions are, instead of the
    .Length
    or
    .Count
    property, you use the
    .GetLength(n)
     
    SparrowGS and seejayjames like this.
  3. RedCorpDE

    RedCorpDE

    Joined:
    Apr 11, 2017
    Posts:
    3
    Using a List, because I'm more familiar with those,

    will try to use an 2d array.