Need some help to create a difficult algorithm

From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game          • You probably can use a dictionary. It’s just something you’d have to try. You can loop over the values much like the array to find the highest value.

Instead of that expression with max you could do

var highest=0

var index=0

Array: for each x

Compare: array.curvalue>highest

—- set highest to array.curvalue

—- set index to array.curx

• You probably can use a dictionary. It’s just something you’d have to try. You can loop over the values much like the array to find the highest value.

Instead of that expression with max you could do

var highest=0

var index=0

Array: for each x

Compare: array.curvalue>highest

—- set highest to array.curvalue

—- set index to array.curx

Thanks. I will give it a try.

I have a last question: is it possible to get the second highest (2nd place) value and index too? Can I use this same code for this?

• I suppose. I’ll try to give a possible example.

But it’s probably simpler to sort the array. If you want to remember the original indexes, you can add another column and set the values there to the indexes before sorting. If you want to leave the original array alone, you can copy it to another first with the asjson expression.

var index1=0

var index2=0

Array: For each x

Current value <= array.at(index1)

—- set index2 to index1

—- set index1 to curx

• Try Construct 3

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

Construct 3 users don't see these ads
• I suppose. I’ll try to give a possible example.

But it’s probably simpler to sort the array. If you want to remember the original indexes, you can add another column and set the values there to the indexes before sorting. If you want to leave the original array alone, you can copy it to another first with the asjson expression.

var index1=0

var index2=0

Array: For each x

Current value <= array.at(index1)

—- set index2 to index1

—- set index1 to curx

Thanks R0J0hound

But unfortunately I did not understand how I use this second code with the first one. Could you tell me how it would be if I have these two codes together? I just asking this because the 1st code worked perfectly with my scrip. Now I just need to also get the second highest value and the respective index.

Taking the opportunity, I've been thinking about the results and I am wondering what would happen if I have a tie. Just a curiosity.

• Just use it instead of the first code. I kind of paraphrased the expressions but it should be useable. At the end of it the highest value will be

Array.at(index1)

And the second will be

Array.at(index2)

You’ll want to reset both vars to 0 before running that event.

If there is a tie then then it will still work. Barring any flaw to the idea.

edit:

Actually that won't work right for finding the second highest value in some cases. Do this instead: loop over the array twice. Again you'll want to reset the variables to 0 before running the two loops again.

global number index1=0

global number index2=0

array: for each x

compare: array.curvalue >= array.at(index1)

--- set index1 to array.curX

array: for each x

compare: array.curx != index1

compare: array.curvalue >= array.at(index2)

--- set index2 to array.curX

• Just use it instead of the first code. I kind of paraphrased the expressions but it should be useable. At the end of it the highest value will be

Array.at(index1)

And the second will be

Array.at(index2)

You’ll want to reset both vars to 0 before running that event.

If there is a tie then then it will still work. Barring any flaw to the idea.

edit:

Actually that won't work right for finding the second highest value in some cases. Do this instead: loop over the array twice. Again you'll want to reset the variables to 0 before running the two loops again.

global number index1=0

global number index2=0

array: for each x

compare: array.curvalue >= array.at(index1)

--- set index1 to array.curX

array: for each x

compare: array.curx != index1

compare: array.curvalue >= array.at(index2)

--- set index2 to array.curX

Thanks. It's a nice solution I tried to do something before using your last code. It worked but I think this solution is much better.

• 21 posts