• 🏆 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!

[JASS] Instead of H2I,I2H, Why not this?

Status
Not open for further replies.
Level 10
Joined
Sep 21, 2007
Messages
517
its pretty darn simple, but i didnt see it in jass section (maybe its just me ofc), anyways the only negative thing it has about it is that u have to create an id for a handle so that it can be used later. Here is the code:
JASS:
library HandleIds
   
    globals
        private hashtable ht = InitHashtable()
        private integer maxId = -1
        private handle array Handle[8191]
    endglobals
    
        function CreateId takes handle h returns integer
            set maxId = maxId + 1
            set Handle[maxId] = h
            call SaveInteger( ht, GetHandleId(h), 0, maxId )
            return maxId
        endfunction
    
        function GetId takes handle h returns integer
            return LoadInteger( ht, GetHandleId(h), 0 )
        endfunction
    
        function GetHandle takes integer i returns handle
            return Handle[ LoadInteger( ht, i, 0 ) ]
        endfunction
    
endlibrary

i did see one used in loops, and a hashtable one that doesnt support every type of handle, but the only limitation here is that it can only have a maximum of 8191 handles, its possible to do another, but il have to add if/then/else calls, anyways, this seems to work pretty well; should i submit in JASS functions section or is it just too dam simple? btw i will prob add a function to remove an id soon
 
Last edited:
Level 16
Joined
Oct 12, 2008
Messages
1,570
http://www.hiveworkshop.com/forums/submissions-414/i2h-without-typecasting-138267/
I made this some time ago, efficiency hasnt been checked for a long time (learned alot since then).
I would recommend the Hashtable version for basic use, for advanced dudes i would maybe say the slower Struct version.

The problem with yours is:
JASS:
local integer i = CreateId(CreateUnit(...))
local unit u = GetHandle(i)
// useless example, i know. But it shows what i mean
will not work, as a Handle cannot be downgraded to a unit automatically, a unit can, however, be seen as a handle.
(example: you got fruits (handle) on a bowl, and you want an apple(unit), then you cannot take any random fruit from the bowl, because it might be a banana (item), or a pineapple(destructable). However if you take an apple, you CAN say that it is fruit.)

BTW: That system of mine IS actually posted in JASS section :p
 
Level 10
Joined
Sep 21, 2007
Messages
517
nah man it worked, i used GetId in an example after creating the id, besides, why did u use the loop bro? its just for example, set id = GetId(GetTriggerUnit()) then set blabla[id] = bla bla XP

edit: i see where ur going, let me test it

Yep, it cant, i can create specific functions for units instead, or form an interface for it or something, pretty sure thatll work; any other problems u faced while making that function? so il cover it up with mine? >_>
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
see? ;]
Well i didnt really encounter any problems, except for the fact that when you use textmacro's, you make sure that the hashtable is private AND uses a parameter as (part of the) name, otherwise you have multiple hashtables with the same name = error.
 
Level 10
Joined
Sep 21, 2007
Messages
517
nah bro, i think one function for the units, destructables would fit, anyways man, why dont u integrate this technique of mine instead of using loops and structs? cus itll improve speed, good thing is u can remove ids easily with structs tho ^^

btw: works perfectly the new functions, thanks for the REALLY useful info bro, but in JASS Functions section the critics there seem scary lul, u think they would disapprove this even if i got everything working properly? lul :p
 
Level 10
Joined
Sep 21, 2007
Messages
517
speed is of the essence ;P anyways bro, i really dont feel like fully updating this today, but do it man, they will prob all use it, but not one for each type of handle, cus i dont really think, personally atlesat, that its needd, maybe units a seperate array and ddestructables, but handles, not like someone is gonna make 8191 trackables or something... anyways, thats my rant for the day :p thanks for tips and +rep >_>
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
no textmacro's are not inefficient, as far as i know, they just generate code, meaning this:
JASS:
//! textmacro TEST takes NAME,TYPE
function $NAME$ takes $TYPE$ h returns boolean
    if h == null then
        return true
    endif
    return false
endfunction
//! endtextmacro

//! runtextmacro TEST("Unit","unit")
//! runtextmacro TEST("Item","item")

//...
// will become:
//...

function Unit takes unit h returns boolean
     if h == null then
        return true
    endif
    return false
endfunction
function Item takes item h returns boolean
    if h == null then
        return true
    endif
    return false
endfunction
 
Level 8
Joined
Oct 3, 2008
Messages
367
>I2H is not useless at all and the return bug (probably) got fixed in the last version

I dare say that you spouted two incorrect things in one sentence.

Where is I2H useful? And we do, in fact, still have a return bug.
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,191
Do nothing is an empty function with no return.

I2H was only useful for recalling objects from integers, but is completly usless as there is no need for it anymore with hashtables. It is unsafe as well, and the wrong values can easilly cause crashes.

H2I is now a native which is even faster.
 
Level 19
Joined
Feb 4, 2009
Messages
1,313
>I2H is not useless at all and the return bug (probably) got fixed in the last version

I dare say that you spouted two incorrect things in one sentence.

Where is I2H useful? And we do, in fact, still have a return bug.

I said "probably" so it can't be wrong :p
however it does not work for me any longer and using it would be stupid because blizzard could fix it and we would have to find another way...
(or I am doing it wrong which might be the case)

and just because there are hashtables I2H does not become useless
hamburgers won't become useless cause there is pizza
it's just something else
(it has some disadvantages though)
 
Level 10
Joined
Sep 21, 2007
Messages
517
azlier, im sure a guy with ur expertise can recycle it with some RemoveID function, but it will take time, all those indexing shit, look, how about you guys just continue this work if u want, cus indexing really takes that much time for me and im 2 lazy to fully learn it, and im not planning to work on it further, here is current script:
JASS:
library HandleIds
    
    globals
        private hashtable ht = InitHashtable()
        private integer maxId = -1
        private integer maxId1 = -1
        private handle array Handle[8191]
        private destructable array Destructable[8191]
    endglobals
    
        function CreateId takes handle h returns integer
            set maxId = maxId + 1
            set Handle[maxId] = h
            call SaveInteger( ht, GetHandleId(h), 0, maxId )
            return maxId
        endfunction
    
        function GetHandlesId takes handle h returns integer
            return LoadInteger( ht, GetHandleId(h), 0 )
        endfunction
    
        function GetIdHandle takes integer i returns handle
            return Handle[ LoadInteger( ht, i, 0 ) ]
        endfunction
        
        // functions specifically for units, uses no custom Ids
        
        function CreateUnitId takes unit u returns integer
            call SaveUnitHandle(ht, GetHandleId(u), 1, u)
            return GetHandleId(u)
        endfunction
        
        function GetUnitId takes unit u returns integer
            return GetHandleId(u)
        endfunction
    
        function GetIdUnit takes integer i returns unit
            return LoadUnitHandle(ht, i, 1)
        endfunction
        
        // functions specifically for destructables, uses custom Ids

        function CreateDestructableId takes destructable d returns integer
            set maxId1 = maxId1 + 1
            set Destructable[maxId1] = d
            call SaveInteger( ht, GetHandleId(d), 2, maxId1 )
            return maxId1
        endfunction
    
        function GetDestructableId takes destructable d returns integer
            return LoadInteger( ht, GetHandleId(d), 2 )
        endfunction
    
        function GetIdDestructable takes integer i returns destructable
            return Destructable[ LoadInteger( ht, i, 2 ) ]
        endfunction
    
endlibrary

hf coding :p and im not so sure destructables can be used in HT, so did same thing as i did with handles
 
Status
Not open for further replies.
Top