Array comparison across a range of cells

0 favourites
  • 3 posts
From the Asset Store
Finding the shortest path through the cells based on the A-star algorithm
  • I'm attempting to refactor my code. I'm pretty sure there is a more efficient way to do this, but I don't really have much of an idea for approaching it.

    I have functions to check for a variable amount of values in an array in sequence, and would like to combine them into one function.

    Currently my conditions look as follows if I want to check 3 cells:

    (if)

    array value at (startposition+1) < 0

    array value at (startposition+2) < 0

    array value at (startposition+3) < 0

    (then)

    array set value at startposition+1 to 1

    array set value at startposition+2 to 1

    array set value at startposition+3 to 1

    If I wanted to check and modify 10 cells, I would need to set up 10 conditions and 10 actions.

    I thought briefly of using loops, but the problem with that is I want all conditions met (check every cell) before modifying anything. When I tried a loop, the function modified each cell as it went, and just skipped over the cells that didn't meet the condition. If there are any cells in the range that don't meet the condition, I don't want any action to be made.

    Basically I wonder if there is a way to have a condition check each contiguous cell in an array from x to x+y for a value, and if true, set values for each cell from x to x+y, where x (starting point) and y (number of cells to check) are function parameters. Currently it looks like I have to check each array cell with a separate condition first.

    Any help would be appreciated.

    Thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • How about something like

    Make a variable called total =0

    For loop 1 to 10

    If the value at array (position +loop count ) is greater than 0 then add 1 to total

    At the end of loop

    If total is greater than ten

    Do you. Other loop

  • Excellent! That worked well, thanks much.

    The idea I had overlooked was how to run the other loop (set values) after the condition checking loop had finished. Before, I had the action on the same event, or in a sub event, so it would run at the same time as the condition checking. The solution was to have it on the same level, but following the check event. The action event just needed to check the condition that the previous event completed successfully. If not, it doesn't run.

    Managed to reduce the events used by that section by 80% :) Thanks again!

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