How do I sort an Array with 2 "keys"

Get help using Construct 2

Post » Sun Aug 17, 2014 5:30 pm

Hi,

I've started to work on a turned-based combat game and I have my first problem. I want to create an Array for the initiative order of the fight.
I want to store 2 informations : Name of the current character and his initiative value. Something like :
12 Warrior
11 Mage
16 Ogre
9 Goblin

And i want to sort this array to have something like :
16 Ogre
12 Warrior
11 Mage
9 Goblin


I found this tutorial : https://www.scirra.com/tutorials/614/array-based-inventory but i'm not able to reproduce it. He didn't sort the array but the player can sort it with a visual interface.
I found this topic https://www.scirra.com/forum/viewtopic.php?t=67745&start=0 but the capx delivered by R0J0hound or Yann doesn't answered the initial question.

There is my actual .capx : https://www.dropbox.com/s/7f0gr0wmpa97os2/Human_Vs_Squirrel_V2%20-%20Insertion%20Sorting.capx

Thanks for the help and sorry for the bad english.

Edit : I edit my capx with my start about my initiative problem and what i've tried to do based on this algo : http://en.wikipedia.org/wiki/Insertion_sort
B
7
S
1
Posts: 30
Reputation: 787

Post » Sun Aug 17, 2014 10:13 pm

edit: ok this capx does an insertion sort based on your needs, hopefully

I based the array on your capx rather than what you've indicated above, as the order is different in the capx.
i.e. name, value rather than value, name. I'll let you plug it into your code.

insertion_sort.capx


Apart from the issues with your actual algorithm implementation, there's a basic way you're trying to loop that won't work. You're trying to use them like for loops in normal languages.
e.g.

System: For i from 1 to A_Initiative.Width-1


and then

System: Set current to A_Initiative.at(i, 1)


You need to use 'loopindex', not 'i'. 'i' is just a tag for the loop. e.g. you could refer to loopindex(i). Normally I use "X" or "Y" or similar, if I need to use it.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Aug 18, 2014 8:21 am

B
21
S
8
G
6
Posts: 177
Reputation: 8,134

Post » Mon Aug 18, 2014 3:23 pm

Thank you so much for the help and the explanation guys !
B
7
S
1
Posts: 30
Reputation: 787

Post » Mon Aug 18, 2014 9:49 pm

If you move the 4 lines that are unnecessarily executed in the while loop.. it's identical to mine :) Not surprising as it's the same algorithm :lol: I don't see the point in passing in the '3' to the function though.

B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 19, 2014 2:06 am

Won't it be easier if you just use action:sort array and then action:reverse array. This is quite straight forward if the initiative is at X-index=0.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Tue Aug 19, 2014 2:12 am

DuckfaceNinja wrote:Won't it be easier if you just use action:sort array and then action:reverse array. This is quite straight forward if the initiative is at X-index=0.


Won't work with 2d array (easily)..

edit: but I will stand corrected.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, dand, tinhofiel and 22 guests