Searching for minimum value in an array?

Get help using Construct 2

Post » Fri Jul 17, 2015 4:34 pm

The code I have looks something like:

Array row : [uid1, uid2, uid3, int]

Each of these rows with this data arrangement is set up when a laser line hits a "target". The three uids of the objects involved are stored (firer, laser, target), but the thing I'm really looking to do is compare the int at the end of the array.

Say I have an array data setup loaded like this with three rows.
[101, 201, 301, 500]
[101, 201, 302, 750]
[101, 201, 303, 1000]

In this setup I have the same firer for all three rows, the same laser beam, but three different targets at three different distances.

How can I search across the array to find the minimum distance in the fourth Y value, in a way that doesn't require knowledge of the number of rows I am searching across?
B
4
Posts: 7
Reputation: 214

Post » Fri Jul 17, 2015 5:05 pm

just a thought .. example.capx
Image
B
63
S
23
G
7
Posts: 1,480
Reputation: 10,796

Post » Fri Jul 17, 2015 5:05 pm

The number of rows is irrelevant as you will always have to search the entire array. The easiest method of doing it is a function with two local variables, that will return the index of the row with the minimum int. First, set the localVar1 to array.At(0,3) and localVar2 to 0, then Repeat array.width-1 -> if array.at(loopindex,3) < localVar1 then set localVar1 to array(loopindex,3) and set localVar2 to loopindex. After the loop, set return value to localVar2. Then you can access the array at the correct index and do whatever you want like Array.At(Function.Call("functionName"), [any y index]) to get a particular uid or the value.
B
67
S
27
G
24
Posts: 969
Reputation: 18,573


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests