• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

String to Handle

With this simple yet powerful tool you can generate a handle from a string!

JASS:
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  //
//                                                                    //
//  -  -  -  -  -  -  S T R I N G  t o  H A N D L E  -  -  -  -  -  - //
//                                                                    //
//  -  -  -  -  -  -  C R E A T E D  b y  B R I B E  -  -  -  -  -  - //
//                                                                    //
//  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//     Simple code that retrieves a unique handle ID for any string.  //
//  The strings it checks are not case-sensitive, so don't worry it.  //
//  This code is very efficient as it references a constant function. //
//  String recognition is mostly alpha-numeric, but is very easy to   //
//  add your own symbols to the checker.  I use the most common chars //
//  for efficiency purposes.  Have fun =)                             //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  //
//                                                                    //
//                                                                    //
// How to Import  -  +  -                                             //
// Convert a trigger to custom script, place this code in the script. //
// Be sure that this trigger is above any trigger that references it. //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//                                                                    //
//                                                                    //
// Syntax  -  +  -                                                    //
// function GetStrHandle takes string s returns integer               //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//                                                                    //
//                                                                    //
// Example useage  -  +  -                                            //
// local integer HandleID = GetStrHandle("retrieve unit")             //
// local unit u = LoadUnitHandle(Hash,HandleID,0)                     //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //
//                                                                    //
//                                                                    //
//                                                                    //
    constant function Char2Int takes string s returns integer         //
        if s = "a" then                                               // -
            return 1                                                  // +
        elseif s = "b" then                                           // -
            return 2                                                  // +
        elseif s = "c" then                                           // -
            return 3                                                  // +
        elseif s = "d" then                                           // -
            return 4                                                  // +
        elseif s = "e" then                                           // -
            return 5                                                  // +
        elseif s = "f" then                                           // -
            return 6                                                  // +
        elseif s = "g" then                                           // -
            return 7                                                  // +
        elseif s = "h" then                                           // -
            return 8                                                  // +
        elseif s = "i" then                                           // -
            return 9                                                  // +
        elseif s = "j" then                                           // -
            return 10                                                 // +
        elseif s = "k" then                                           // -
            return 11                                                 // +
        elseif s = "l" then                                           // -
            return 12                                                 // +
        elseif s = "m" then                                           // -
            return 13                                                 // +
        elseif s = "n" then                                           // -
            return 14                                                 // +
        elseif s = "o" then                                           // -
            return 15                                                 // +
        elseif s = "p" then                                           // -
            return 16                                                 // +
        elseif s = "q" then                                           // -
            return 17                                                 // +
        elseif s = "r" then                                           // -
            return 18                                                 // +
        elseif s = "s" then                                           // -
            return 19                                                 // +
        elseif s = "t" then                                           // -
            return 20                                                 // +
        elseif s = "u" then                                           // -
            return 21                                                 // +
        elseif s = "v" then                                           // -
            return 22                                                 // +
        elseif s = "w" then                                           // -
            return 23                                                 // +
        elseif s = "x" then                                           // -
            return 24                                                 // +
        elseif s = "y" then                                           // -
            return 25                                                 // +
        elseif s = "z" then                                           // -
            return 26                                                 // +
        elseif s = " " then                                           // -
            return 27                                                 // +
        elseif s = "_" then                                           // -
            return 28                                                 // +
        elseif s = "-" then                                           // -
            return 29                                                 // +
        elseif s = "0" then                                           // -
            return 30                                                 // +
        elseif s = "1" then                                           // -
            return 31                                                 // +
        elseif s = "2" then                                           // -
            return 32                                                 // +
        elseif s = "3" then                                           // -
            return 33                                                 // +
        elseif s = "4" then                                           // -
            return 34                                                 // +
        elseif s = "5" then                                           // -
            return 35                                                 // +
        elseif s = "6" then                                           // -
            return 36                                                 // +
        elseif s = "7" then                                           // -
            return 37                                                 // +
        elseif s = "8" then                                           // -
            return 38                                                 // +
        elseif s = "9" then                                           // -
            return 39                                                 // +
        elseif s = "." then                                           // -
            return 40                                                 // +
        elseif s = "," then                                           // -
            return 41                                                 // +
        elseif s = "!" then                                           // -
            return 42                                                 // +
        elseif s = "?" then                                           // -
            return 43                                                 // +
        endif                                                         //
        return 44                                                     // defaults if char is not recognized
    endfunction                                                       //
//  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +  //
    function GetStrHandle takes string s returns integer              //
        local integer strIndex = 0                                    // current char-point in the string
        local integer strId                                           // current char value
        local integer lastId = 1                                      // last char value
        local integer genId = 0                                       // cumulative char values (turns into handle ID)
        local integer stop_point = StringLength(s)                    //
        loop                                                          //
            set strIndex = strIndex + 1                               // set to next char-point...
            set strId = Char2Int(SubString(strIndex,strIndex))        // retrieves char integer...
            set genId = genId + (strId - lastId) + (strId * lastId)   // Sets generated handle with scrambling math
            set lastId = strId                                        //     between strId and lastId
            exitwhen strIndex > stop_point                            //
        endloop                                                       //
        return genId * 1873                                           // returns string handle
    endfunction                                                       //
//                                                                    //
//  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  //

Here is an example function that can be put to good use:

JASS:
function HashVariables takes nothing returns nothing
    call SaveUnitHandle(Hash,GetStrHandle("unit 1"),0,CreateUnit(Player(0),'hfoo',0.,0.,0.))
    call SaveRectHandle(Hash,GetStrHandle("rect 1"),0,Rect(0,0,0,0))
endfunction
 
Last edited:
Handles are just automatically-generated integers that index an instance in the code, so since the game lacks and indexer for strings, I have created my own.

With another way to save/load from Hashtables, this opens the door for some more creative approaches to programming.

Note: I have updated the original post with a function that uses GetStrHandle to index variables.
 
Level 8
Joined
Oct 3, 2008
Messages
367
Constant functions are exactly the same speed as normal functions. So, this is inefficient, pointless (see StringHash), incorrectly labeled, not placed within a library...

I'm fairly sure that this should be graveyarded this instant.
 
Top