| 01-31-2010, 05:43 PM | #1 |
Let we say I have 10 integer variables, each of them has random number between 0 and 100. Now, how can I find which one of them has lowest value? Min function World Editor has can compare only two variables (as far as I know) Or I'm doing this, because I'm trying to find closest unit to a certain point. If there's any clear way to find which one is it, well, I'm all ears :) |
| 01-31-2010, 05:49 PM | #2 |
For the first one, use a sort algorithm, or simply loop though the numbers and get the lowest one by multiple comparisons For the second one, pick all units and compare their distances like mentioned |
| 01-31-2010, 06:24 PM | #3 |
Thanks for the reply, could you be more specific please? The number of variables is unknown to me, might be more than 10. I can't imagine how to sucesfully compare them, without having it to be ridiculously long. What is this sorting algoritm you speak of, and how do I accomplish it? |
| 01-31-2010, 06:48 PM | #4 |
Should output the lowest #. Not tested (or syntax checked). JASS:local integer i = 1 local integer lowest = yourArray[0] local integer index = 0 loop exitwhen i > 10 if (yourArray[i] < lowest) then set lowest = yourArray[i] set index = i endif set i = i + 1 endloop call BJDebugMsg("yourArray["+I2S(index)+"] has the lowest value, " + I2S(lowest)) |
| 01-31-2010, 06:53 PM | #5 |
JASS:globals integer array A integer Size = 9 // for values in the array with index between 0 - 9 endglobals function Min10 takes nothing returns integer local integer i = 0 local integer r = A[0] local integer a = 0 loop exitwhen i == Size set a = Min(A[i], A[i+1]) if a < r then set r = a endif set i = i+1 endloop return r endfunction |
| 01-31-2010, 06:56 PM | #6 |
If you want to find the nearest unit to a point, PruneGroup can help. |
| 01-31-2010, 07:04 PM | #7 | |
Quote:
|
| 01-31-2010, 07:53 PM | #8 | |
Quote:
Here's an outline for how it's done. It's made to be simple, read comment on (meaningless? :P) optimizations below. Trigger: Once you understand this outline, you should probably optimize it by storing "Distance Between..." as a variable (so you won't have to check multiple times for the same unit). You should also take care of the point-leaks, and the unit group leak. |
| 01-31-2010, 10:06 PM | #9 |
Whoa, thanks for all the replies, I went with the moyack's one for finding lowest value, and it works perfectly (thanks man) After that, Themerion's trigger was clear as day to me. I'm glad you posted it :P So, yeah, thanks again ^^ |
| 02-01-2010, 01:06 PM | #10 | |
Quote:
|
| 02-01-2010, 01:37 PM | #11 | |
Quote:
|
| 02-01-2010, 05:05 PM | #12 | |
Quote:
Perhaps, I didn't exactly copy'n'paste it, just saw how it's done and then did it by memory. |
