I have four stats, all numeric, and I’ll be using their rankings fairly regularly. I need to know all four stats’ positions, so I was going to code a sequence that would set the best, second best, and worst (so then I’ve found the third-ranked stat by default). I’m planning to put this code in a subscene, since I’ll need it frequently.
The method I’m using now takes about a hundred lines, and it looks like this, only four times over in different iterations:
And so forth for stat_b and stat_c and stat_d, with a few minor variations to account for previous code.
I was just wondering if anyone had suggestions for how to code a simpler version? I feel like there must be a quicker solution, but after some tinkering, I’m still not sure what. I did have a hunt for a topic on this, but I didn’t find anything (apologies if I’ve just missed them).
I remember having to do this for 20 items back in 1999 for a football management game I was trying to make. Needless to say the code was very long!
For 4… let’s see. There are 4! possible combinations but you also have to take into account ties (two stats the same). 100 lines isn’t bad. I’ll get to a PC in a few hours and see if I can do any better.
I just decided which stats would take priority in the case of ties (mostly based on whimsy what might be most narratively interesting, or possibly less-often the highest), as I’m not using these for scores, like I have up in that wee sample.
Fabulous! I look forward to seeing the results of your coding wizardry.
Yes; I don’t think I’ll need it, actually, but if I do I’ve already found it by default.
I did see that, but you were just looking for the highest stat, correct? And not a system for ranking, which adds other steps too…
That looks brilliant, thank you.
I thought arrays would likely assist, but they’re still quite new to me so I’ve done very limited meddling. I should probably meddle more. Thank you though, much obliged!
I’m afraid I don’t have a lot of time, but this is what I’ve come up with.
Note: this will get you the four numbers in the correct order but it doesn’t tell you the name of what each stat represents… I’m assuming you will match them again afterwards or that doesn’t matter?
Summary
*create stat_a 0
*create stat_b 0
*create stat_c 0
*create stat_d 0
*create temp_stat 0
*create first stat_a
*create second stat_c
*create third stat_b
*create fourth stat_d
*comment compare 2 pairs to get highest (first and second) and lowest (third and fourth)
*if stat_b >= stat_a
*set first stat_b
*set third stat_a
*if stat_d >= stat_c
*set second stat_d
*set fourth stat_c
*comment cross check the lower numbers with the higher numbers
*if fourth >= first
*set temp_stat fourth
*set fourth first
*set first temp_stat
*if third >= second
*set temp_stat third
*set third second
*set second temp_stat
*comment compare top1 and top2 - for first and second place
*if second > first
*set temp_stat first
*set first second
*set second temp_stat
*comment compare third and fourth - swap if necessary.
*label third_fourth
*if fourth > third
*set temp_stat third
*set third fourth
*fourth temp_stat
*goto all_done
Edit: down to 33 lines if you remove the comments and spaces.
I actually mostly need to know which stat is in which position, not which number value is the highest…now to see what would be the quickest way to do that, and if I can sneak it in and still keep the total under the 42 lines of code @JimD’s algorithm compressed into. :D
@JimD and @andymwhy I’ll be using one or the other of your samples as the basis for my code…would you mind terribly if I credit you in the scene file via *comment as the originator of your code routine? And thanks so much!
Edit: @JimD I was playing about with the code and discovered that it doesn’t work properly if the stats happen to be in descending order. I tried (without changing anything save the names of the stats) and ended up like so:
@JimD is missing a check on the occasion that best_stat = temp_stat (which happens if stat_a is the highest). In which case, stat_a is found as the best in all 4 slots, I believe.
In this situation, I’d recommend using my code as you only have 4 numbers to order. My code will give you the four numbers in the correct order. Jim’s style, with a little tweaking, would be better for a general case (5, 10, X items to order).
There’s no need to place a credit but if you wish to in the code as a comment, that’s fine