• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Trigger] Finding largest value

Status
Not open for further replies.
Hello

How would I go about finding the largest value out of X other integer/real variables? It should also need some way to detect if two or more values are equal.

I have a way of doing it, but it's convoluted and not very efficient at all. It involves a lot of "if X is greater than X and X and X and X" and so on....

I tried googling some formulas on how to find the correct values, but all I found was excel solutions.

Not really sure if this should even be posted under triggers & scripts since it's essentially a math question :D
 
Level 10
Joined
Mar 26, 2017
Messages
376
Here is some code. If you add all variables to a list 'l', then loop through the list.
With the value 'i' you retrieve the highest value, and list 'l2' you find the index(es) of the variable that hold that value.

Lua:
local i = 0
for x=1,#l do
    if l[x] > i then
        l2 = {x}
        i = l[x]
    elseif l[x] == i then
        l2[#l2+1] = x
    end
end

If you just want to know the highest value and don't care to know about which variables this value comes from, then you can use this function:

Lua:
math.max(var1, var2, var3...)
 
Last edited:
Here is some code. If you add all variables to a list 'l', then loop through the list.
With the value 'i' you retrieve the highest value, and list 'l2' you find the index(es) of the variable that hold that value.

Lua:
local l2 = {}
local i = 0
for x=1,#l do
    if l[x] > i then
        l2[1] = x
        i = l[x]
    elseif l[x] == i then
        l2[#l2+1] = x
    end
end

If you just want to know the highest value and don't care to know about which variables this value comes from, then you can use this function:

Lua:
math.max(var1, var2, var3...)
Thanks! Excuse my lack of coding knowledge, but could you put this into a GUI solution?
 
Level 10
Joined
Mar 26, 2017
Messages
376
Thanks! Excuse my lack of coding knowledge, but could you put this into a GUI solution?

Run your map in lua mode.
Add a global integer variable called 'i' (this will be converted to udg_i in the script language.

Then use this line to set the variable i to the highest value of variables.

  • Custom script: udg_i = math.max(var1, var2, var3...)
You replace 'var1' with the desired variable names.
If it is a global variable, you need to put 'udg_' in front of it.
 
Run your map in lua mode.
Add a global integer variable called 'i' (this will be converted to udg_i in the script language.

Then use this line to set the variable i to the highest value of variables.

  • Custom script: udg_i = math.max(var1, var2, var3...)
You replace 'var1' with the desired variable names.
If it is a global variable, you need to put 'udg_' in front of it.
Awesome, seems straight forward enough.

What if I want to pick a random number in the event two or more are equal? (a random number among the equal ones of course).
 
Or would you like to retrieve a random variable among those that hold the highest number?
This. Basically it's for a turn based combat system, and to determine what unit starts first it checks the "swiftness" stat of all units. If two or more units has got the same swiftness value then a random should be selected among those with equal swiftness.
 
Level 10
Joined
Mar 26, 2017
Messages
376
  • Custom script: local l = array_name local i = 0 for x=1,#l do if l[x] > i then l2 = {x} i = l[x] elseif l[x] == i then l2[#l2+1] = x end end udg_i = l2[GetRandomInt(1,#l2)]
You need to replace 'array_name' with the name of the global array that holds your variables, and add 'udg_' in front.
Then with this function above, you can retrieve a random index from variable(s) in that array which hold the highest value.
This random index will be bound to global variable 'i'.
 
Last edited:

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,871
Something like this in GUI:
  • Actions
    • -------- Your Integers/Reals --------
    • Set VariableSet Values[1] = 100
    • Set VariableSet Values[2] = 200
    • Set VariableSet Values[3] = 300
    • -------- --------
    • Set VariableSet Greatest = 0
    • For each (Integer A) from 1 to 3, do (Actions)
      • Loop - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Values[(Integer A)] Greater than Greatest
          • Then - Actions
            • Set VariableSet Greatest = Values[(Integer A)]
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • Values[(Integer A)] Equal to Greatest
              • Then - Actions
                • Game - Display to (All players) for 5.00 seconds the text: Tie!
              • Else - Actions
The variable Greatest will always be equal to the greatest value.

And if you wanted to pick one of the tied values at random, you could do something like this:
  • Untitled Trigger 001
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • -------- Your Integers/Reals --------
      • Set VariableSet Values[1] = 100
      • Set VariableSet Values[2] = 200
      • Set VariableSet Values[3] = 300
      • Set VariableSet Values[4] = 300
      • Set VariableSet Values[5] = 300
      • -------- --------
      • Set VariableSet Total_Values = 5
      • Set VariableSet Index = 0
      • Set VariableSet Greatest = 0
      • -------- --------
      • For each (Integer A) from 1 to Total_Values, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Values[(Integer A)] Greater than Greatest
            • Then - Actions
              • Set VariableSet Greatest = Values[(Integer A)]
            • Else - Actions
      • -------- --------
      • Game - Display to (All players) for 5.00 seconds the text: ------------------
      • Game - Display to (All players) for 5.00 seconds the text: (Greatest: + (String(Greatest)))
      • Game - Display to (All players) for 5.00 seconds the text: ------------------
      • -------- --------
      • For each (Integer A) from 1 to Total_Values, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Values[(Integer A)] Equal to Greatest
            • Then - Actions
              • Set VariableSet Index = (Index + 1)
              • Set VariableSet Ties[Index] = (Integer A)
            • Else - Actions
      • -------- --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Index Greater than 1
        • Then - Actions
          • -------- Choose a tied value at random --------
          • Set VariableSet Winner = (Random integer number between Ties[1] and Ties[Index])
          • Game - Display to (All players) for 5.00 seconds the text: Pick a random tied ...
          • Game - Display to (All players) for 5.00 seconds the text: ((Winner = Value: + (String(Winner))) + ( ( + ((String(Values[Winner])) + ))))
        • Else - Actions
This is designed with the idea that the Index for the Values would represent something like a Player Number. Values[1] = Player 1's Score, Values[2] = Player 2's Score, etc...
 

Attachments

  • Get Greatest Value.w3m
    16.8 KB · Views: 27
Last edited:
Status
Not open for further replies.
Top