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

Handles and variables

Status
Not open for further replies.
Level 7
Joined
May 13, 2011
Messages
310
OK, well, I'm making an MUI spell with hashtables, and I need to get a unit in a variable, and I also need to get its handle. The problem here is that the unit I am getting will be a random unit from a unit group, and if I use "Random Unit From Unit Group" for both of them, I could get different units.
So I figured I could make a unit variable, and then a handle variable which gets a unit handle from the unit variable. However, I couldn't manage to find a way to do this. I tried to do it the other way round (get unit from unit handle) but this wasn't possible either. The best I could find was loading a unit handle from a hashtable, but I don't want to make a hashtable just for this.

If someone could tell me if I've missed something, or an alternative method altogether, that'd be cool. Also if you need any elaboration then please say so.
 
Level 23
Joined
Jan 1, 2009
Messages
1,617
But if he wants a random unit?

Also to your other questions:
A unit doesnt have a handle, it "is" a handle. Handle is the highest class of things in the game. (can be found in the common.j)
So I dont understand what you are trying to do, you wanna save some unit related stuff with the unit's HandleId as parentkey?
Whats your problem with that. And if you also want to have the unit then save it also.
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
To get a unit's handle ID:
  • Custom script: set udg_handleID = GetHandleId(udg_tempUnit)
(handleID is an integer variable, tempUnit a unit variable).
Then you can use the variable handleID in the hashtable.
  • Hashtable - Save integer as (Key int) of handleID in hashtable
It's best to store a handle ID into a variable either way, because the GUI way of doing things is rather... slow.

frotty said:
But if he wants a random unit?
"FirstOfGroup" basically picks a random unit, unless you know which unit is the first in the group of course (which you don't).

You could either use that native, or, you know, use the GUI "Random unit from group":
JASS:
function GroupPickRandomUnitEnum takes nothing returns nothing
    set bj_groupRandomConsidered = bj_groupRandomConsidered + 1
    if (GetRandomInt(1,bj_groupRandomConsidered) == 1) then
        set bj_groupRandomCurrentPick = GetEnumUnit()
    endif
endfunction

function GroupPickRandomUnit takes group whichGroup returns unit
    // If the user wants the group destroyed, remember that fact and clear
    // the flag, in case it is used again in the callback.
    local boolean wantDestroy = bj_wantDestroyGroup
    set bj_wantDestroyGroup = false

    set bj_groupRandomConsidered = 0
    set bj_groupRandomCurrentPick = null
    call ForGroup(whichGroup, function GroupPickRandomUnitEnum)

    // If the user wants the group destroyed, do so now.
    if (wantDestroy) then
        call DestroyGroup(whichGroup)
    endif
    return bj_groupRandomCurrentPick
endfunction
I'll take "FirstOfGroup(...)" any time.
 
I really dont trust FirstOfGroup as it picks the "First" of group, not random...
Somebody taught me this style to get random units compared to FirstOfGroup but this is rather better...
JASS:
function RANDOM takes nothing returns boolean
   local unit u = GetFilterUnit()
   if udg_INTEGER <=0 then
      set udg_INTEGER = udg_INTEGER + 1
      call KillUnit(u) //this is the random unit
   endif
   set u = null
   return false
endfunction
 
function PICK takes nothing returns nothing

   set udg_INTEGER = 0
   call GroupEnumUnitsInRange(g, X, Y, AREA, Filter(function RANDOM))
endfunction
 
Level 7
Joined
May 13, 2011
Messages
310
To get a unit's handle ID:
  • Custom script: set udg_handleID = GetHandleId(udg_tempUnit)
(handleID is an integer variable, tempUnit a unit variable).
Then you can use the variable handleID in the hashtable.

That is what I want, thank you ap0calypse. I should have seen it before actually, it's really simple.

To the rest of the people who replied: when I said handle I meant a handle ID, sorry for the confusion. I'm still getting the hang of hashtables and handle IDs so I might be mixing up a few terms.
What I was trying to do was get both a random unit in a unit variable, and it's handle ID in a handle variable. What I was trying to do was this:

  • Set HBRandUnit = (Random unit from (Units within 300.00 of (Position of (Target unit of ability being cast)) matching (((Matching unit) is A Hero) Equal to True)))
  • Set HBRandHandle = HBRandUnit
So that later on I could do stuff like this:

  • Hashtable - Save 20 as 0 of (Key HBRandHandle) in healBiteHash
  • Hashtable - Save 8 as 1 of (Key HBRandHandle) in healBiteHash
  • Hashtable - Save Handle Of(Last created unit) as 2 of (Key HBRandHandle) in healBiteHash
However, HBRandHandle was a handle variable, and I couldn't manage to specify what to get the handle ID from. All I could choose was a massive list of stuff like "Event Response - Attacked Unit", "Picked Player", and so on. Basically, what I wanted was the handle ID of a value within a variable, which I couldn't find. Now I understand that I should simply get the handle ID as an integer and use it as the key.

Still, thanks to all of you. I think I've got a better understanding of what a handle is and what a handle ID is. :thumbs_up:
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
I really dont trust FirstOfGroup as it picks the "First" of group, not random...
Somebody taught me this style to get random units compared to FirstOfGroup but this is rather better...
JASS:
function RANDOM takes nothing returns boolean
   local unit u = GetFilterUnit()
   if udg_INTEGER <=0 then
      set udg_INTEGER = udg_INTEGER + 1
      call KillUnit(u) //this is the random unit
   endif
   set u = null
   return false
endfunction
 
function PICK takes nothing returns nothing

   set udg_INTEGER = 0
   call GroupEnumUnitsInRange(g, X, Y, AREA, Filter(function RANDOM))
endfunction
FirstOfGroup picks the unit with the lowest Y-coordinates.
Your snippet picks the unit with the lowest Y-coordinates.

There is no difference, aside from the fact that "FirstOfGroup(g)" is faster...
 
Level 7
Joined
May 13, 2011
Messages
310
Ah see, in my case FirstOfGroup() wouldn't be desirable. The unit group I am getting my random unit from is "Units In Range Matching Condition", so if your claim is correct, using FirstOfGroup (or mckill's snippet) guarantees to get the southernmost unit. For most unit groups this would be OK, but not for this.

I'm not sure how GUI's "Random Unit From Unit Group" works, but as long as it does it in a more random way, then it's better to use that.
 
FirstOfGroup picks the unit with the lowest Y-coordinates.
Your snippet picks the unit with the lowest Y-coordinates.

While FirstOfGroup is faster...I dont think that there is no difference coz the snippet also provides to get random N units dynamically by changing the "udg_INTEGER <=N"
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,286
FirstOfGroup is not random as it depends on the underlying mechanics of groups.

For example, FirstOfGroup used on a group built from the same enumerator with units in about the same position will likly return the same results which is not truely random.

Random Unit means that a unit has an equal chance of being selected out of a group. Using a random index from an array list of units is truely random for example. Using FirstOfGroup chooses the unit that is stored first in the underlying implimentation of a group which will mean that all other units are not even considered for selection. Depending on how enumerations work and how the underlying implimentation of group stores units, it could potantially result in some units being impossible to be choosen in certain situations which is anything but random.
 
Status
Not open for further replies.
Top