Searching for minimum value in an array?

  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • just a thought .. example.capx

  • 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.

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