Snippet Custom Custom Value

Weep

Godspeed to the sound of the pounding
Reaction score
400
Custom Custom Value
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 --
  1. Be sure "Automatically create unknown variables while pasting trigger data" is enabled in the World Editor general preferences.
  2. 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.)
  3. Copy the code into your map header (click on the map's name at the top of the trigger list).
  4. (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.
 

Attachments

  • CCV Demo.w3x
    21.2 KB · Views: 189

Romek

Super Moderator
Reaction score
963
You could use H2I + Gamecache or GetHandleId + Hashtables.
The latter of which is probably the fastest - faster than this.

Trouble is, nobody actually needs things like this. What's the point in making a system that's an alternative to Custom Data, that does the exact same thing (with the exact same limitations)?

> This allows you to attach an integer value to a unit without using UnitUserData
Why would you want to do that? :p

It's be easy for indexing systems to use hashtables or gamecache, though they don't, as Custom Data is much faster and more convenient. There's also no reason as to why it shouldn't be used.
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
You could use H2I + Gamecache or GetHandleId + Hashtables.
The latter of which is probably the fastest - faster than this.
The Mac version of the World Editor can't use the new natives in 1.24, and there's a good chance <return h, DoNothing(), return 0> will be disabled in an upcoming patch - a better chance than Blizzard updating the World Editor for Mac.

Don't game caches start bugging out once you've played enough different maps that use them, requiring you to go delete some file?

Trouble is, nobody actually needs things like this. What's the point in making a system that's an alternative to Custom Data, that does the exact same thing (with the exact same limitations)?
The exact same limitations? This lets you make your own unique index set for your system or spell.

> This allows you to attach an integer value to a unit without using UnitUserData
Why would you want to do that? :p
To co-exist with UnitUserData. I've helped a few people on the main forum so far who did not want a solution to their problem that used UnitUserData because they had imported something else that used it.

It's be easy for indexing systems to use hashtables or gamecache, though they don't, as Custom Data is much faster and more convenient. There's also no reason as to why it shouldn't be used.
It really seems self-evident to me: hashtables can't be implemented on Mac (though downloaded maps that use them will work) nor pre-1.24 (which some people seem to still use - I keep reading people mention something called "Garena"?), gamecache has its own problems (or so I think I've read), and there's only one UnitUserData to be shared by everything in a map.
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
Bump. Can someone benchmark this, if only for curiosity's sake? :)
 

Jesus4Lyf

Good Idea™
Reaction score
397
Why? It's got to be one of the slowest attaching methods I've ever heard of.

I've thought about this before. I reached much the same conclusions as Romek.

When the unit dies, references persist in the groups also, don't they? Sounds bad. :p

As for those projects, they should use a universal indexing system of some sort (AIDS or PUI... or AutoIndex). Since you're on a mac, I'd say just custom write something, or rip off PUI and make it plain JASS.
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
While is is appropriate that this is graveyarded, because this version does exhibit a progressive, significant slowdown as units decay (though I may update it at some point), I just wanted to link a few posts supporting the reason for my original attempt:

http://www.thehelper.net/forums/showpost.php?p=1117714&postcount=4
http://www.thehelper.net/forums/showpost.php?p=1117832&postcount=7

People who'd need a system like this would be a small minority (Mac users who haven't managed to build their project on the principle that custom value should only be used for general unit indexing), but it could be useful, still. :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top