| 09-18-2004, 04:57 AM | #1 |
Hopefully this isnt one of those questions that has been answered too many times, I dont usually check the jass forums. I'm trying to use local game cache to reduce variables, what I want to do is store integers or reals in game cache under the label of a unit, then get those values latter. This is so I can create custom stats and access them easily. I have been looking at some of weddaars maps in the repository, since they use a similar method as far as I can tell, but I am having troubles simplifying the basics of the code for my own uses. I know how to use jass (atleast the basics), just been awhile since I used wc3. Can someone help me out here? |
| 09-18-2004, 05:31 AM | #2 |
Okay I'll take a stab at that question. What I've come to use is a pretty simple little framework of code for every map: Code:
function H2I takes handle value returns integer
return value
return 0
endfunction
function I2U takes integer value returns unit
return value
return null
endfunction
function I2Timer takes integer value returns timer
return value
return null
endfunction
//Note: VarCache is a global game cache variable
function SetHandleInt takes handle subject, string name, integer value returns nothing
call StoreInteger(udg_VarCache, I2S(H2I(subject)), name, value)
endfunction
function GetHandleInt takes handle subject, string name returns integer
return GetStoredInteger(udg_VarCache, I2S(H2I(subject)), name)
endfunction
function SetHandleReal takes handle subject, string name, real value returns nothing
call StoreReal(udg_VarCache, I2S(H2I(subject)), name, value)
endfunction
function GetHandleReal takes handle subject, string name returns real
return GetStoredReal(udg_VarCache, I2S(H2I(subject)), name)
endfunction
function DoesHandleVarExist takes handle subject, string name returns boolean
return HaveStoredInteger(udg_VarCache, I2S(H2I(subject)), name)
endfunction
function FlushHandle takes handle subject returns nothing
call FlushStoredMission(udg_VarCache, I2S(H2I(subject)))
endfunctionH2I - Handle to Int. Because you can convert Int's to almost anything with handle bug, Int's are what I use most. I2U - Int to Unit, quite often, the information your storing is a [reference to a] unit. Other RB exploiters include ones like I2Timer, I2Trigger, I2Item. Of the next group of functions, SetHandleInt and GetHandleInt are basically generic custom values for any handle you please. Most often this will be a unit, but also commonly a trigger or timer. But again, you can use it to attach values to pratically anything you might need, like items, groups, quests. The good thing is because they take a handle you can use them with any handle and don't need lots of functions (like you don't need SetUnitInt) A simple example, lets say you want to record some piece of data for a unit: Code:
//set call SetHandleInt(tempUnit, "age", 1) //get local integer a = GetHandleInt(tempunit,"age") //modify call SetHandleInt(tempUnit,"age",a+1) Now, lets say you want to link two units together, say a summoned unit to the summoning unit, you could do it like this: Code:
call SetHandleInt(GetSummonedUnit(),"master",H2I(GetSummoningUnit()) Code:
local unit master
if DoesHandleVarExist(GetEnumUnit(),"master") then
//Unit has a master, so do stuff
set master = I2U(GetHandleInt(GetTriggerUnit())
call IssuePointOrder(GetEnumUnit(),"move",GetUnitX(master),GetUnitY(master))
endifFinally, you might want to clean up the variables when a unit dies. Code:
//generic event "unit dies" call FlushHandle(GetDyingUnit()) Now it seems that every advanced JASS coder has slightly different prefences on exactly how they use handle vars, like with function names and semantics and stuff. And I didn't invent mine either, I originally ripped it from Battledome but have since modified it some. As an example, some people prefer to have additional functions like this: Code:
function SetHandleUnit takes handle h, string name, unit u returns nothing
call SetHandleInt(h,name,H2I(u))
endfunction
function GetHandleUnit takes handle h, string name returns unit
return I2U(GetHandleInt(h,name))
endfunction |
| 09-18-2004, 05:58 AM | #3 |
I read through it, seems simple enough. Im going to be testing this out. Thanks for the help. Edit: Im having trouble getting it to work, after I set the value I try to read it and it comes up as 0. Heres how i put it. Code:
Create char
Events
Player - Player 1 (Red) skips a cinematic sequence
Conditions
Actions
Custom script: local unit tempunit=CreateUnit(Player(15),'Hpal',0,0,bj_UNIT_FACING )
Custom script: local string s
Custom script: call SetHandleInt(tempunit, "age", 10)
Custom script: set s = I2S(GetHandleInt(tempunit,"age"))
Custom script: call DisplayTextToForce( GetPlayersAll(), s )
|
| 09-18-2004, 08:00 AM | #4 |
Did you initialize the game cache? |
| 09-18-2004, 08:05 AM | #5 |
I should of guessed it was something simple like that, stupid me. It works now. Thanks again. |
| 09-18-2004, 02:20 PM | #6 |
I actually see no reason of using a global variable for that, I think it only acts as a placebo |
| 09-18-2004, 09:15 PM | #7 |
Vex, technically its slightly slower to call InitGameCache() as it has to do a comparision to check if the cache is already in memory before going on. However, its sucha minute slowdown that I don't bother. |
| 09-19-2004, 02:21 AM | #8 |
Code:
function H2I takes handle value returns integer return value return 0 endfunction |
| 09-25-2004, 11:39 AM | #9 |
Thanks Grater, I'd been wanting to do the same thing Though can anyone suggest a way to link a unit to a timer (as opposed to a timer to a unit) as I have a trigger which increases a 'rage' counter every time the unit is attacked which causes a unit to do bonus damage but I also want it to have a cooloff timer so that when he's not fighting it gradually reduces the bonus amount. So I need to detect when a timer expires and then access the unit that the timer is for. Thanks |
| 09-25-2004, 06:08 PM | #10 |
to link unit to timer do something like: call SetHandleInt(varcache, mytimer, "weeee", h2i(myunit)) and get with set tmpunit = i2u( GetHandleInt(varcache, mytimer, "weeee")) also you can make wrapper functions if you want to. PS very helpful post Grater; I haven't seen it explained so understandably before. Edit: I noticed Grater posted the wrapper functions for this if you want them. |
| 09-25-2004, 06:54 PM | #11 | |
Quote:
Thanks Edit: Errr can you describe it a bit more please as I'm getting compile errors... IE what are mytimer and "weeee"? |
| 09-25-2004, 07:52 PM | #12 |
mytimer is your timer, like udg_rage_timer or whatever it is, "weee" is a label, pick whatever you like, just put any string there. you can store multiple units on a timer by using different label strings. |
| 09-25-2004, 08:01 PM | #13 |
right, I think I may have had it the wrong way round. |
| 10-15-2004, 03:54 PM | #14 |
Code:
set master = I2U(GetHandleInt(GetTriggerUnit()) I'm getting an "invalid number of arguments" with this and I can't work out why :-/ |
| 10-15-2004, 05:08 PM | #15 | |
Quote:
It is delcared like this: Code:
function GetHandleInt takes handle subject, string name returns integer Also, another ")" is missing at the end. |
