How do I find the index of the first empty cell in a specific column of an array?

0 favourites
  • 4 posts
From the Asset Store
Use this game pack to create your own game, modify the existing game or simply take a look and see how it was made.
  • I want to determine the index of the first empty cell in a particular column of an array (The column has multiple rows (cell) filled with data). Once I find the first empty cell, I'd like to perform an action based on its position. Basically, I want to fetch its position.

    Tagged:

  • If I understood correctly, something like this will do (probably there's a simpler way)

    + Button: On clicked

    | Local boolean emptyFound‎ = false

    | Local number targetColumn‎ = 2

    ----+ Array: For each XY element

    ----+ Array: Current value = ""

    ----+ System: Array.CurX = targetColumn

    --------+ System: [X] Is emptyFound

    ---------> Functions: Call DoSomething (xIndex: Array.CurX , yIndex: Array.CurY)

    ---------> System: Set emptyFound to True

    * On function 'DoSomething'

    * Parameter 'xIndex' (Number)

    * Parameter 'yIndex' (Number)

    -> Text: Set text to xIndex & "," & yIndex

  • array.IndexOf(0), this works as empty array cells return null

    edit: sorry I just read you are looking for a specific collumn, my bad

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • thanks, cesisco, also I managed to do it with JS so posting the answer for future users.

    const arrayInstance = runtime.objects.C3_Array.getFirstInstance();
    const columnIndex = 4; // Target column
    
    let indexOfEmptyCell = -1; // Initialize with a default value in case no empty cell is found
    
    for (let rowIndex = 0; rowIndex < arrayInstance.height; rowIndex++) {
     const cellValue = arrayInstance.getAt(columnIndex, rowIndex);
     if (cellValue === '') {
     indexOfEmptyCell = rowIndex;
     break;
     }
    }
    
    runtime.globalVars.IndexOf_Empty_Cell_GV = indexOfEmptyCell;
    
    // IndexOf_Empty_Cell_GV is a Global Variable of C3 event sheet where I put the output.
    
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)