How do I check item occupancy in a grid inventory?

0 favourites
  • 4 posts
From the Asset Store
Grid - snap to grid
$1 USD
80% off
Snap to visible grid - perfect solution for any game genre
  • I am trying to develop a grid inventory system where items can occupy multiple slots and have various shapes (such as L, T, and I).

    Currently, I am attempting to implement a function called canPlaceItem, which checks if an item can be placed at a specific position in the grid. The function should:

    • Receive the coordinates (startX, startY) of where the item is being placed.
    • Check if the corresponding cells in the grid (arrayInventoryGrid) are occupied (i.e., not empty).

    * System: On start of layout -> itemL: Set value at (0, 0) to 1 -> itemL: Set value at (1, 0) to 1 -> itemL: Set value at (1, 1) to 1 -> arrayInventoryGrid: Set value at (0, 0) to 1

    * On function 'canPlaceItem' * Parameter 'startX' (Number) * Parameter 'startY' (Number) ----+ itemL: For each XY element --------+ System: itemL.At(itemL.CurX,itemL.CurY) = 1 --------+ System: arrayInventoryGrid.At(startX + itemL.CurX, startY + itemL.CurY) = 0 ---------> debug: Set text to 0 --------+ System: Else ---------> debug: Set text to 1

    + Mouse: On Left button Clicked on slotInventory -> Functions: Call canPlaceItem (startX: slotInventory.id % numColunas, startY: floor(slotInventory.id ÷ numColunas))

    The canPlaceItem function always returns that the slot is free, even when I click on a slot that is occupied. Debugging the value of arrayInventoryGrid, I notice that it is not returning the expected value (0 for free slots and a non-zero value for occupied slots).

    Can anyone help me understand why the check for occupied slots is not working correctly? Any suggestions on how to improve this logic or additional debugging steps I could add to identify the problem?

    here is the c3p file: drive.google.com/file/d/12WkZ9h_QeEKsr2BtISrKjGmkV8_jO2DY/view

  • Your For each XY element loop is iterating through each element of the itemL array, but the only value presented is the result of the final check. One way to fix this is you could add a local boolean (just above the loop) and have it switch on Else, then set your debug text to the value at the end.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you! The overlap issue is now resolved, and I didn't even notice it before.

    My problem now is making the function return 'can't place' when the grid area doesn't fit the itemL. For example, when clicking on the last slots or the sides, where the L shape wouldn't fit.

    | Global number numColumns‎ = 4
    | Global number numLines‎ = 4
    
    + System: On start of layout
    // item L
    -> itemL: Set value at (0, 0) to 1
    -> itemL: Set value at (1, 0) to 1
    -> itemL: Set value at (1, 1) to 1
    // -
    // debug
    -> arrayInventoryGrid: Set value at (0, 0) to 1
    
    * On function 'canPlaceItem'
    * Parameter 'startX' (Number)
    * Parameter 'startY' (Number)
     | Local boolean canPlace‎ = true
    ----+ itemL: For each XY element
    --------+ System: itemL.At(itemL.CurX,itemL.CurY) = 1
    ------------+ System: startX + itemL.CurX < numColumns
    ------------+ System: startY + itemL.CurY < numLines
    ------------+ System: startX + itemL.CurX ≥ 0
    ------------+ System: startY + itemL.CurY ≥ 0
    ------------+ System: arrayInventoryGrid.At(startX + itemL.CurX, startY + itemL.CurY) ≠ 0
    -------------> System: Set canPlace to False
    -------------> System: Stop loop
    
    ----+ System: Is canPlace
    -----> debug: Set text to "can place"
    
    ----+ System: Else
    -----> debug: Set text to "can't place"
    
    + Mouse: On Left button Clicked on slotInventory
    -> Functions: Call canPlaceItem (startX: slotInventory.id % numColumns, startY: floor(slotInventory.id ÷ numColumns))
    

    I'm open to suggestions, as I'm finding this logic quite complicated =(

  • It looks like lines 19-23 are checking that the spot being tested is within bounds (19-22) AND the spot is occupied (23) to set canPlace to false. You need to negate 19-22 and switch it to an OR block.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)