HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Creating a unique INTEGER for a specific unit.

02-03-2003, 06:17 PM#1
Guest
Since blizzard doesn't have a function for converting a Specific Unit to an Integer I went ahead and created a function that assigns a specific unit a specific integer. The below function GetUnitInteger(<unit>) returns a UNIQUE integer for a specific unit.

The most obvious use is in a global Array where you want to keep track of something about that unit. For example, you could keep track of the last unit this specific unit attacked, the last order it issued, etc.

Anyway...enjoy...(thanks to AIAndy for pointing me in the right direction on this)...



Code:
// set up the following variables
//      udg_unitnumber, value =0
//      udg_unitID = unit array

function CountUnit takes integer x returns integer
    set udg_unitnumber = x + 1
    return udg_unitnumber
endfunction

// Adds a unit to the unit ID array
function AddUnitToArray takes unit addunit returns nothing
       set udg_unitID[CountUnit(udg_unitnumber)] = addunit
endfunction

// The function you actually use to get the unique integer
function GetUnitInteger takes unit getunit returns integer
       local integer i = 1
       local integer id = udg_unitnumber + 1
       loop
         exitwhen i > udg_unitnumber
          if udg_unitID[i] == getunit then
             set id = i
             set i = udg_unitnumber + 1
             else
                set i = i + 1 
          endif
         endloop  
       if id == udg_unitnumber + 1 then
         call AddUnitToArray(getunit)
       endif
       return id
endfunction