• 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.

[JASS] Function GetUnitTypeByPrio

Status
Not open for further replies.
Level 3
Joined
Dec 7, 2012
Messages
52
Hi everyone!

I need help to create function.
------
The function must takes "Stats - priority" (name of the string in object editor) of each unit currently selected by player (selected UnitGroup), and returns unitType by unit with max "Stats - priority".
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
If there is a native to return the priority use that, otherwise you need to use a hashtable to map the priority to unit type.

Next to that its the simple maximum algorithm. Wikipedia explains how.

Instead of returning the unit, you return its type.

In case you cannot follow that link. The simplest for of search is a linear search. What you do is iterate through all units in the group and keep track of the one with the highest priority value. Once the final unit is checked you know you have the one with highest priority and so you can return its type. The operation has an O(n) complexity but that is of no concern in the case of WC3 selection groups as their size is finite and small.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
There is not and I have told you this before. You should make the assignments in an init trigger, in a hashtable for instance as Dr Super Good suggested. If you already have a lot of units, so manually copying everything would be a problem, you may consider taking the object data and transforming it programmatically. I can do this for you, only need the unit object data and know if you also use standard units.
 
Level 3
Joined
Dec 7, 2012
Messages
52
ok! thx!
I use 12 (custom+original) different UnitTypes of Naga race. Also, i created my custom SoundSets for my custom Units!
Now, about my problem: if player will select more than one Utnit (UnitGroup) must play SoundSet of Unit with max priority! Custom SoundSet are playing all together, because i can't use UnitPriority of custom Units in my triggers! So, if i select more than one Unit - i have more than one SoundSet are playing at the one time!
So, all i need - give the CustomPriority to all UnitTypes of race Naga and play SoundSet of Unit with max UnitPriority if i select more than one.
Please, tell me what i should do in unit object data, what the variables and trigger actions will be needed?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
JASS:
function GetUnitTypePriority takes integer id returns integer
    return LoadInteger(udg_hashtable, id, 0)
endfunction

function SaveUnitTypePriority takes integer id, integer val returns nothing
    call SaveInteger(udg_hashtable, id, 0, val)
endfunction

function init takes nothing returns nothing
    set udg_hashtable = InitHashtable()

    call SaveUnitTypePriority('hpea', 1)
    call SaveUnitTypePriority('hfoo', 2)
    call SaveUnitTypePriority('hkni', 3)
    ...
endfunction

Create variable udg_hashtable (hashtable in GUI), edit the parameters in the init function to match your unit type ids (which you can see in the object editor >View >Display as raw data), the 2nd argument is the priority you want to assign. Run the init function at startup. Use GetUnitTypePriority to load a unit type's priority later on.
 
Level 3
Joined
Dec 7, 2012
Messages
52
ok! thx! I will try!
But unfortunately, i don't understand how to get MaxUnitTypePriority! :(
I'm noob in jass... Can you write me this script or example on GUI?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
So what do you want to try if you do not get anything from the stuff we post?

GUI does not allow you to use unit types in integer representation, so there will still be jass lines. Furthermore you initially asked for a function but GUI does not feature proper custom functions, so deal with additional lines instead.

Variables are hashtable (hashtable), unitType (unit type), unitTypeId (integer), resultUnitType (unit type), resultPriority (integer).

  • Events
    • Map initialization
  • Conditions
  • Actions
    • Hashtable - Create a hashtable
    • Set hashtable = (Last created hashtable)
    • -------- --------
    • Set unitType = Peasant
    • Custom script: set udg_unitTypeId = udg_unitType
    • Hashtable - Save 1 as 0 of unitTypeId in hashtable
    • -------- --------
    • Set unitType = Footman
    • Custom script: set udg_unitTypeId = udg_unitType
    • Hashtable - Save 2 as 0 of unitTypeId in hashtable
    • -------- --------
    • Set unitType = Knight
    • Custom script: set udg_unitTypeId = udg_unitType
    • Hashtable - Save 3 as 0 of unitTypeId in hashtable
  • Set resultUnitType = No unit type
  • Unit Group - Pick every unit in (Units currently selected by (Triggering player)) and do (Actions)
    • Loop - Actions
      • Set unitType = (Unit-type of (Picked unit))
      • Custom script: set udg_unitTypeId = udg_unitType
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • 'IF'-Conditions
          • Or - Any (Conditions) are true
            • Conditions
              • resultUnitType Equal To No unit type
              • (Load 0 of unitTypeId from hashtable) Greater than resultPriority
        • 'THEN'-Actions
          • Set resultPriority = (Load 0 of unitTypeId from hashtable)
          • Set resultUnitType = unitType
        • 'ELSE'-Actions
  • ...//Do something with resultUnitType
Still wondering how you want to play the unit type-specific sound if you do not know how to assign the sound to the unit type in triggers. Do you want to create a new unit and kill it for instance to play the death sound?
 
Level 3
Joined
Dec 7, 2012
Messages
52
... Furthermore you initially asked for a function...
Yes, i was asked about function, which i'll want to call in the CustonScript action on GUI.

Still wondering how you want to play the unit type-specific sound if you do not know how to assign the sound to the unit type in triggers. Do you want to create a new unit and kill it for instance to play the death sound?
I know how to play the unit type-specific sound and how to assign it to the unit type in triggers too.
And i already did it before i created this thread.
--
My question was: how get the unitType with maxPriority in currently selected UnitGroup?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
If you want to call a proper jass function, guess what, you have to write the function in jass.

If you know how to assign the sound to the unit type, what deters you from using the same principle for attaching an integer? That is part of your question and you have asked for a "GetUnitTypePriority" function midway without the "determine max value of units in group" part. And that's good because the "GetUnitTypePriority" is a component you need to apply to realize your overall problem. Now I do not know why you specifically request a "GetMaxPriorityOfUnitTypesSelected" function (normally nobody would write such a compound for the simple reason it offers hardly any flexibility, too specific). Above I gave you the "GetUnitTypePriority" in jass and showed the remaining part in GUI.
 
Level 3
Joined
Dec 7, 2012
Messages
52
...If you know how to assign the sound to the unit type, what deters you from using the same principle for attaching an integer?..
Nothing! :)
I know how to assign sound, int and other variables to UnitType! But i don't know how to get max int from UnitGroupe! )
And you are right, this function should be named "GetMaxPriorityOfUnitTypesSelected" :)
 
Level 3
Joined
Dec 7, 2012
Messages
52
Hi!
I tried your method to get max Priority. It's working! Thx! :thumbs_up:

So, for the general development, answer one question, please!
"Hashtable - Save 1 as 0 of unitTypeId in hashtable"
why "as 0"? What really means this argument? :)

What means all of arguments of this action?

first argument - priority (in my case);
last argument - hashtable;
But what means second & third?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
In a hashtable in wc3, besides stating the hashtable, you save under two integer keys, like array indexes. Here unitTypeId forms one key because we want to later read the entry from the unitTypeId we are given, and I just had to pick any 2nd key. Other 2nd keys could be used to assign different attributes.
 
Level 3
Joined
Dec 7, 2012
Messages
52
So, if i understand you correctly, instead of "as 0" i can use "as variable(integer, array)"?
For example, "Hashtable - Save 1 as PriorityArray of unitTypeId in hashtable", isn't it?
 
Status
Not open for further replies.
Top