# Array comparison across a range of cells

Get help using Construct 2

### » Thu Feb 21, 2013 6:31 am

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!

B
36
S
16
G
67
Posts: 1,038
Reputation: 38,080

### » Thu Feb 21, 2013 7:39 am

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
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
59
S
30
G
34
Posts: 1,194
Reputation: 23,767

### » Thu Feb 21, 2013 9:01 am

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!oosyrag2013-02-21 09:02:15
B
36
S
16
G
67
Posts: 1,038
Reputation: 38,080

### Who is online

Users browsing this forum: 99Instances2Go, db3344, makkancs, matriax, orugari, Saad Swad, Taweel and 12 guests