Weep
Godspeed to the sound of the pounding
- Reaction score
- 400
Custom Custom Value
by Weep
Version 1.0
by Weep
Version 1.0
I'm surprised I haven't seen anything like this posted. Maybe I just missed it.
-- General Information --
This snippet will encode/retrieve an integer, attached to a unit, using the unit's inclusion and exclusion in each index of a unit group array as binary bits.This allows you to attach an integer value to a unit without using UnitUserData, which is used so often by spells and systems that it's hazardous to use it yourself due to risk of conflicts.
This snippet is probably obsoleted by hashtables in 1.24 (depending on speed), for those who can use them (read: not Mac users).
-- How To Implement --
- Be sure "Automatically create unknown variables while pasting trigger data" is enabled in the World Editor general preferences.
- Copy the trigger category "CustomCustomValue" from the demo map and paste it into your map. (Alternately: create an integer variable named Identifier_CustomCustomBits and a unit group array named Identifier_CustomCustomValue.)
- Copy the code into your map header (click on the map's name at the top of the trigger list).
- (optional) If you want to make the indexes unique to your system or spell, go into the variable editor and rename the variables Identifier_CustomCustomValue and Identifier_CustomCustomBits to have a unique name in place of "Identifier". Then change all instances of the text "Identifier" in the code you just pasted to what you wrote (a text editor with Find/Replace All makes this quite easy).
Now, write your code using Identifier_SetCustomUnitUserData(unit, integer) in place of SetUnitUserData(unit, integer), and Identifier_GetCustomUnitUserData(unit) in place of GetUnitUserData(unit).
- Step 4 can probably be made easy-breezy with private variables in vJASS, but of course I can't use vJASS myself, so you get this instead.
-- Code --
JASS:
// Globals: group array udg_Identifier_CustomCustomValue
// integer udg_Identifier_CustomCustomBits
function Identifier_GetCustomUnitUserData takes unit u returns integer
local integer l = 1
local integer r = 0
if(udg_Identifier_CustomCustomBits > 1) then
loop
exitwhen l == udg_Identifier_CustomCustomBits
if(udg_Identifier_CustomCustomValue[l] != null and IsUnitInGroup(u, udg_Identifier_CustomCustomValue[l])) then
set r = r + R2I(Pow(2, l-1))
endif
set l = l + 1
endloop
if(udg_Identifier_CustomCustomValue[0] != null and IsUnitInGroup(u, udg_Identifier_CustomCustomValue[0])) then
set r = (-1)*r
endif
endif
return r
endfunction
function Identifier_SetCustomUnitUserData takes unit u, integer i returns nothing
local integer l = 1
if(udg_Identifier_CustomCustomValue[0] == null) then
set udg_Identifier_CustomCustomValue[0] = CreateGroup()
endif
if(i < 0) then
call GroupAddUnit(udg_Identifier_CustomCustomValue[0], u)
set i = IAbsBJ(i)
else
call GroupRemoveUnit(udg_Identifier_CustomCustomValue[0], u)
endif
loop
exitwhen i < 1
if(udg_Identifier_CustomCustomValue[l] == null) then
set udg_Identifier_CustomCustomValue[l] = CreateGroup()
endif
if(ModuloInteger(i, 2) == 1) then
call GroupAddUnit(udg_Identifier_CustomCustomValue[l], u)
else
call GroupRemoveUnit(udg_Identifier_CustomCustomValue[l], u)
endif
set i = i/2
set l = l + 1
endloop
if(udg_Identifier_CustomCustomBits < l) then
set udg_Identifier_CustomCustomBits = l
else
loop
exitwhen l == udg_Identifier_CustomCustomBits
call GroupRemoveUnit(udg_Identifier_CustomCustomValue[l], u)
set l = l + 1
endloop
endif
endfunction
-- Notes --
Speed of this snippet is untested. Speed of GetCustomUnitUserData is proportional with the number of bits needed to store the largest integer yet saved in the map session.- I have no idea how to benchmark things in WC3, so if someone could do so, I'd appreciate the information.
-- Credits --
Some code based on Dec2Bin (http://wc3jass.com/viewtopic.php?t=76) by Aiursrage2k.Credit in your map not needed, but please include the README.
-- Version History --
1.0: First release.
--------------------------------------
Comments regarding problems, improvements, or redundancy (or praise ) welcome.