 |
WC3Jass.com "The Jass Vault"
|
Affiliates
|
 |
|
 |
|
|
| Message |
Posted:
Mon Apr 25, 2005 2:14 pm Post subject:
Using the Handle Vars |
|
|
I'll explain quickly how to use the Handle Var functions..
First, some people call them "Handlers", but the actual name is "Local Handle Variables" or "Handle Vars" for short.
What is a handle?
A handle is an object in the game.
An object can be a unit, group, player, trigger, timer, boolexpr, effect, lightning or location for example.
Integer, real, boolean, code, and string variables are the only variables in Jass that are NOT handles.
Let's take an example:
| Code: |
function MyFunction takes handle h returns nothing
/// ... Do something
endfunction
local unit u = // ...Some unit
local group g = // ...Some group
local integer i = 5
call MyFunction(u) // OK: u is a unit and a unit is a handle
call MyFunction(g) // OK: g is a group and a group is a handle
call MyFunction(i) // WRONG: i is an integer and an integer is NOT a handle
|
Using the handle vars
Their purpose is to store variables in a way that we can get them back later.
Try to think of it as ordinary locals, that are tied to objects instead of functions.
Let's take an example:
| Code: |
local unit myUnit = // ...Some unit
local unit myUnit2 = // ...Some other unit
local integer i = 250
local integer a
call SetHandleInt(myUnit, "abc", i) // Set "abc" on myUnit to 250
call SetHandleInt(myUnit2, "abc", i*2) // Set "abc" on myUnit2 to 500
call SetHandleInt(myUnit2, "asd", i/2) // Set "asd" on myUnit2 to 125
set a = GetHandleInt(myUnit, "abc")
// a = 250 because we set "abc" to 250 earlier
set a = GetHandleInt(myUnit, "asd")
// a = 0 because "asd" is not set on myUnit
set a = GetHandleInt(myUnit2, "abc")
// a = 500 since myUnit2 has "abc" set to 500
set a = GetHandleInt(myUnit2, "asd")
// a = 125 since myUnit2 has "asd" set to 125
|
Now this is useful because we can get back the stored values much later and in another function.
Cleaning up
Removing a single handle var can be done by setting it to null, 0 or "".
| Code: |
call SetHandleHandle(..., "abc", null)
call SetHandleInt(..., "abc", 0)
call SetHandleReal(..., "abc", 0.00)
call SetHandleString(..., "abc", "")
|
But what if we just want to set the value to 0?
No problem: GetHandleInt returns 0 if the value is not set. GetHandleHandle returns null if it's not set and GetHandleString returns "" if it's not set. So in the end it's just saving memory without affecting the variables.
Before destroying a handle with locals, you better clear all the locals first so they won't consume memory.
FlushHandleLocals(handle h) will remove all local variables set on a given handle.
| Code: |
local unit u = // ...Some unit
call FlushHandleLocals(u) // Clean up!!
call RemoveUnit(u)
|
Adding more GetHandle-type functions
Copy the GetHandleUnit function, give it a new name, change the return type to whatever type you need and you are done! It's that easy.
For example:
| Code: |
function GetHandleUnit takes handle subject, string name returns unit
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
// Only difference is the name and the 'boolexpr' instead of 'unit' at the end.
function GetHandleBoolexpr takes handle subject, string name returns boolexpr
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
|
Read on here to learn more about the use of the Handle Vars. |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Fri Jul 21, 2006 8:07 pm Post subject:
i dont understand!!! |
|
|
hi im new using jass so bare with me.
just wondering what functons are:
SetHandleInt(...)
GetHandleInt(...)
WE and the jass editor says they dont exist and u never defined them in the script so could you pls explain them to me thnx |
|
|
|
 |
|
|
|
| Message |
|
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 12:48 am Post subject:
still... |
|
|
Yea i saw that and im trying to understand the script but i cant understand what StoreInteger(...) and GetStoredInteger(...) do. i thought it saved the integer value in the game cache but when i test it in a simple trigger i recieve the value '0' instead of the value i assigned it. could you look at the script and see whats wrong:
function Trig_TRIGGER_NAME_Actions takes nothing returns nothing
call StoreInteger(udg_CACHE_NAME,"MainKey","Key",3)
call DisplayTextToForce(GetPlayersAll(),I2S(GetStoredInteger(udg_CACHE_NAME,"MainKey","Key")))
endfunction |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 1:43 am Post subject:
|
|
|
| Don't forget to initialize the cache... |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 2:08 am Post subject:
InitGameCache... |
|
|
| but the game cache im using is a global. do i still have to init it? if so how? i dont think InitGameCache(...) is ment for that after all it askes for a file address not a game cache variable. or does the global have a filename as well??? |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 2:23 am Post subject:
OOOOOOOO!!!! |
|
|
set udg_GAMECACHENAME=InitGameCacheBJ( "ANYNAME.w3v" )
i tried this and it worked guess that was what u meant Shvegait well thnx for helping.
if you can one more thing: how can you transfer the value from one local varible from one function to another without making the function "take" the value? my reason for asking this is that the TimerStart(...) function cannot queue a function that "takes" values neither can the action of a trigger. so is there a way to do that other than to resort to the use of game cache?? |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 3:34 am Post subject:
|
|
|
| Code: |
call SetHandleInt(myTimer, "abc", i)
//....
//....on the expired timer callback function
call GetHandleInt(GetExpiredTimer(), "abc")
|
_________________ _-|-_ |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 3:34 am Post subject:
|
|
|
| Code: |
call SetHandleInt(myTimer, "abc", i)
//....
//....on the expired timer callback function
call GetHandleInt(GetExpiredTimer(), "abc")
|
_________________ _-|-_ |
|
|
|
 |
|
|
|
| Message |
Posted:
Sat Jul 22, 2006 4:15 am Post subject:
??? |
|
|
| im not really familiar with the local handle variable script so give me a break here. if u want to take the value of a local unit variable into another function how do that? |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Sat Jul 22, 2006 5:07 am Post subject:
Re: ??? |
|
|
| NewGuy wrote: |
| im not really familiar with the local handle variable script so give me a break here. if u want to take the value of a local unit variable into another function how do that? |
"Attach" it into something that will certainly get passed between functions. In a timer callback, the timer as GetExpiredTimer() should work (my example should cover that, read below). To pass it to a trigger, you may have to either have a global trigger variable that points to the trigger you need, or cerate it within your function.
Example:
| Code: |
//This will run when the timer created in BaseFunction expires
function TimerCallback takes nothing returns nothing
local unit myUnit = GetHandleUnit(GetExpiredTimer(),"myUnit")
//etc...
endfunction
//This will run as the actions in the trigger created in BaseFunction
function TriggerAction takes nothing returns nothing
local unit myUnit = GetHandleUnit(GetTriggeringTrigger(),"myUnit")
//etc...
endfunction
function BaseFunction takes nothing returns nothing
local trigger myTrigger = CreateTrigger()
local timer myTimer = CreateTimer()
local unit myUnit = CreateUnitAtLoc(p,'o003',0.00,0.00)
call SetHandleHandle(myTrigger,"myUnit",myUnit)
call SetHandleHandle(myTimer,"myUnit",myUnit)
call TimerStart(myTimer, 5.00 ,false, function TimerCallback)
call TriggerRegisterAnyUnitEventBJ( myTrigger, EVENT_PLAYER_UNIT_DEATH )
call TriggerAddAction( myTrigger, function TriggerAction )
//etc...
endfunction
|
_________________ _-|-_ |
|
|
|
 |
|
 |
|
 |
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|