jwallstone
New Member
- Reaction score
- 33
Hashtable Handle Attachment System (Local Handle Vars with hashtables)
Here's a rewrite of KaTTaNa's Local Handle Vars using hashtable natives. The original can be found here:
http://www.wc3jass.com/viewtopic.php?p=3737
NOTE: I claim absolutely no credit for this. This involved very little work on my part, and definitely no creative work. All credit should go to KaTTaNa. I'm posting this only to help out other people who may be interested in an updated version for patch 1.24.
The system can be used to "attach" or "associate" any of the core data types (integers, reals, strings, booleans) as well as handles of the agent type with any other handle. (Agent type handles are the reference counted handle types.)
There are two ways to use this:
1. Easy way - create a Hashtable global var, and replace the return value in the HATable function with your variable
2. Less easy, but better way - create a Hashtable global var and replace all function calls to HATable() with your global variable. The function HATable can be removed.
Either way, the hashtable must be initialized with a call to InitHA() before use.
Notes:
1. The function for saving handles has been renamed to SetHandleAgent, to reflect the fact that only agent type handles can be saved.
2. To erase an entry, you do not simply save a 0, false, or null value. Instead call the appropriate "Remove_blank_" function. This is to allow these values to be saved as legitimate entries, and to ensure the proper operation of HaveSaved_blank_ when these values are saved. Furthermore, it enforces better code writing in terms of readability and clarity of function use.
3. The Remove functions have an extra step of storing a useless value (0,null,false) to prevent bugs that were discovered with hashtables as of the beta patch 1.24. If the bugs no longer exist, those lines will be removed.
Here's a rewrite of KaTTaNa's Local Handle Vars using hashtable natives. The original can be found here:
http://www.wc3jass.com/viewtopic.php?p=3737
NOTE: I claim absolutely no credit for this. This involved very little work on my part, and definitely no creative work. All credit should go to KaTTaNa. I'm posting this only to help out other people who may be interested in an updated version for patch 1.24.
The system can be used to "attach" or "associate" any of the core data types (integers, reals, strings, booleans) as well as handles of the agent type with any other handle. (Agent type handles are the reference counted handle types.)
There are two ways to use this:
1. Easy way - create a Hashtable global var, and replace the return value in the HATable function with your variable
2. Less easy, but better way - create a Hashtable global var and replace all function calls to HATable() with your global variable. The function HATable can be removed.
Either way, the hashtable must be initialized with a call to InitHA() before use.
Notes:
1. The function for saving handles has been renamed to SetHandleAgent, to reflect the fact that only agent type handles can be saved.
2. To erase an entry, you do not simply save a 0, false, or null value. Instead call the appropriate "Remove_blank_" function. This is to allow these values to be saved as legitimate entries, and to ensure the proper operation of HaveSaved_blank_ when these values are saved. Furthermore, it enforces better code writing in terms of readability and clarity of function use.
3. The Remove functions have an extra step of storing a useless value (0,null,false) to prevent bugs that were discovered with hashtables as of the beta patch 1.24. If the bugs no longer exist, those lines will be removed.
JASS:
function HATable takes nothing returns hashtable
return udg_HATable
endfunction
function InitHA takes nothing returns nothing
local hashtable ha = HATable()
set ha = InitHashtable()
set ha = null
endfunction
function SetHandleAgent takes handle subject, string name, agent value returns nothing
call SaveAgentHandle(HATable(), GetHandleId(subject), StringHash(name), value)
endfunction
function SetHandleInt takes handle subject, string name, integer value returns nothing
call SaveInteger(HATable(), GetHandleId(subject), StringHash(name), value)
endfunction
function SetHandleBoolean takes handle subject, string name, boolean value returns nothing
call SaveBoolean(HATable(), GetHandleId(subject), StringHash(name), value)
endfunction
function SetHandleReal takes handle subject, string name, real value returns nothing
call SaveReal(HATable(), GetHandleId(subject), StringHash(name), value)
endfunction
function SetHandleString takes handle subject, string name, string value returns nothing
call SaveStr(HATable(), GetHandleId(subject), StringHash(name), value)
endfunction
function GetHandleInt takes handle subject, string name returns integer
return LoadInteger(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleBoolean takes handle subject, string name returns boolean
return LoadBoolean(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleReal takes handle subject, string name returns real
return LoadReal(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleString takes handle subject, string name returns string
return LoadStr(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleUnit takes handle subject, string name returns unit
return LoadUnitHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleItem takes handle subject, string name returns item
return LoadItemHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleTimer takes handle subject, string name returns timer
return LoadTimerHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleTrigger takes handle subject, string name returns trigger
return LoadTriggerHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleEffect takes handle subject, string name returns effect
return LoadEffectHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleGroup takes handle subject, string name returns group
return LoadGroupHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleLightning takes handle subject, string name returns lightning
return LoadLightningHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleWidget takes handle subject, string name returns widget
return LoadWidgetHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleLocation takes handle subject, string name returns location
return LoadLocationHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandlePlayer takes handle subject, string name returns player
return LoadPlayerHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleRegion takes handle subject, string name returns region
return LoadRegionHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleRect takes handle subject, string name returns rect
return LoadRectHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleForce takes handle subject, string name returns force
return LoadForceHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleFogmodifier takes handle subject, string name returns fogmodifier
return LoadFogModifierHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function GetHandleTimerDialog takes handle subject, string name returns timerdialog
return LoadTimerDialogHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function HaveHandleHandle takes handle subject, string name returns boolean
return HaveSavedHandle(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function HaveHandleInt takes handle subject, string name returns boolean
return HaveSavedInteger(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function HaveHandleReal takes handle subject, string name returns boolean
return HaveSavedReal(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function HaveHandleString takes handle subject, string name returns boolean
return HaveSavedString(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function HaveHandleBoolean takes handle subject, string name returns boolean
return HaveSavedBoolean(HATable(), GetHandleId(subject), StringHash(name))
endfunction
function RemoveHandle takes handle subject, string name returns nothing
call SaveAgentHandle(HATable(),GetHandleId(subject),StringHash(name), null)
call RemoveSavedHandle(HATable(),GetHandleId(subject),StringHash(name))
endfunction
function RemoveInt takes handle subject, string name returns nothing
call SaveInteger(HATable(),GetHandleId(subject),StringHash(name), 0)
call RemoveSavedInteger(HATable(),GetHandleId(subject),StringHash(name))
endfunction
function RemoveReal takes handle subject, string name returns nothing
call SaveReal(HATable(),GetHandleId(subject),StringHash(name), 0.0)
call RemoveSavedReal(HATable(),GetHandleId(subject),StringHash(name))
endfunction
function RemoveBoolean takes handle subject, string name returns nothing
call SaveBoolean(HATable(),GetHandleId(subject),StringHash(name), false)
call RemoveSavedBoolean(HATable(),GetHandleId(subject),StringHash(name))
endfunction
function RemoveStr takes handle subject, string name returns nothing
call SaveStr(HATable(),GetHandleId(subject),StringHash(name), null)
call RemoveSavedString(HATable(),GetHandleId(subject),StringHash(name))
endfunction
function FlushHandle takes handle subject returns nothing
call FlushChildHashtable(HATable(), GetHandleId(subject) )
endfunction