HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Handle Vars?

10-31-2006, 11:30 AM#1
Toink
What exactly are handle vars? How can I use them in trigger codes and what do they do? + rep to anyone who can explain this to me thoroughly
10-31-2006, 11:33 AM#2
karukef
Considering that there are several tutorials that explain EXACTLY what they are and how they can be used, anyone that spends time writing a thorough post to answer this question would be a dimwit.

Try the tutorials section.
10-31-2006, 11:36 AM#3
Rising_Dusk
Handle Variables

Quote:
Originally Posted by KaTTaNa; General How To
Stores and recieves local variables from handles.
Here is a short example:
Collapse JASS:
// Add spawn to a group stored on master
local unit master = GetSummoningUnit()
local unit spawn = GetSummonedUnit()
local group spawns = GetHandleGroup(master, "spawns")
// Initialize group if needed
if (spawns == null) then
    set spawns = CreateGroup()
    call SetHandleHandle(master, "spawns", spawns)
endif
// Add spawn to group
call GroupAddUnit(spawns, spawn)

// Store pointer to master on spawn
call SetHandleHandle(spawn, "master", master)
To add more GetHandle-type functions, simply copy GetHandleUnit (or another GetHandle-function) and replace the return type with another type and give it whatever name you like.

Note: In order to prevent this from leaking, replace InitGameCache with a global in the LocalVars function.
Also remember to initialize the game cache during map initialization, for example:
Collapse JASS:
call FlushGameCache(InitGameCache("cache.x"))
    set udg_GameCache=InitGameCache("cache.x")

Quote:
Originally Posted by Weaaddar; How H2I Works
Hidden information:
The quintessiential H2I, the crux that all return bugging code uses. This function was made popular by a bunch of people and me. The function is used to store a handle's address in an integer.

How this work:
All variable types in warcraft 3 are four bytes in length. The atomic types (ints, reals, and booleans) use thier four bytes as direct value. Handles use thier four bytes of space as a "pointer" to thier object they references "memory address". I use the terms "pointer" loosely. As they are not first class pointers. As well as "memory address", it is believed all objects are stored in a specific heap and these are thier keys to them. But that is irrelavant.
Anyway, the Jass parser only checks the last return value satiates the return type. Thus we "trick" it into taking a handle when it really was meant to take an integer.
In a similar fashion you can write your own function to typecast from integer to handle. Or to a specific handle. Note: that if you'd like to convert a handle to another type you only need to something like this
Collapse JASS:
function H2U takes handle h returns unit
return h
endfunction
As the return type checker only makes sure that the type being returned is a handle, if it checking for a child of handle.

Not all the non-atomic types are handles though. There are two other types of data. There is the code type. It is what is used to store an address to a function. There isalso the string. Jass has a redudancy check to make sure that all strings which have the same literal content are stored under the same address. Jass was made with poor coders in mind. Having to keep track of all usage of strings would have been maddening as there is no real time garbage collector.

Okay fine, you explained it but why now? Why submit H2I now, when there are a million and four snippets of code that use it?

Because, I think its time to no longer be that blacksheep function that most new jass scripters find as some evil deviance. When its fully explained how it works, and such people might not have thier irrational fear. What wasn't mention in the question was how thier are also about a thousand variations of a function to convert handles to integers. I think its already been determined that this is the most popular variant, and hopefully the willing will be inclined to modify thier old projects and that this shall become the gold standard for type casting handles to ints.

...Now if only we can figure out some resolve on how people use Gamecache!
10-31-2006, 10:42 PM#4
Toink
Thanks for the reply dusk :), yes I am a dimwit ;P
11-01-2006, 01:33 AM#5
PandaMine
Simplifieng what wealdarr said, H2I just converts any handle (i.e. any variable that isnt an integer,real,boolean,string) into a unique ID. You then store a value using the unique id as a label and then retrieve it the same way.

The reason why H2I is needed in JASS is before it was invented there was no way to make multi-instancable scripts. Multi-instancable scripts are scripts that aren't instant and because they aren't instant they can be run in multiple instance (i.e. while you have have one instance of the script going another one starts).

What is required when making multi-instancable scripts are also local variables. The reason why you need local variables is every time a function stores information in a local variable its is allocated in a DIFFERENT section in the memory, where as with a global it is stored in the same section of memory (over-riding anything that was there before). However since local variables are unique for each function, when you do something like use a timer which requires another function there was no way to transfer variables from the original function to the timer without using globals. This is where H2I came in, you could convert the timer into a unique code, store information under that code so when the function needs the information it retrieves the variables from the timer. This is also multi-instancable because everytime the script is a called a NEW timer is created so it makes a NEW unique id

Hopefully this will increase your understanding of the H2I bug
11-01-2006, 12:41 PM#6
MaD[Lion]
handle vars are integer variables that respresent a point to the memory block of something
11-02-2006, 11:04 PM#7
PandaMine
yes but that info i useless for a person trying to learn jass because they dont understand how to use it
11-04-2006, 03:22 AM#8
The)TideHunter(
Quote:
Originally Posted by PandaMine
yes but that info i useless for a person trying to learn jass because they dont understand how to use it

It is not useless, it explains what handle type casting is.