- Joined
- Apr 21, 2014
- Messages
- 21
I've been working with hashtables for quite a while now, but it occurred to me that I may not be using it properly. I may be missing or overlooking something. Just in case, can someone check my code if I'm doing it correctly?
Here's a sample of one of my abilities that uses hashtables, an item effect that restores HP over time to a unit that kills another unit.
Am I doing it right, the basics, at least?
Here's a sample of one of my abilities that uses hashtables, an item effect that restores HP over time to a unit that kills another unit.
JASS:
// Assuming that udg_hash is already initialized
function SoulCatcher_C takes nothing returns boolean
return (UnitHasItemOfTypeBJ(u, 'I000')) and (IsUnitEnemy(GetTriggerUnit(), GetOwningPlayer(GetKillingUnit()))) and (IsUnitType(GetTriggerUnit(), UNIT_TYPE_STRUCTURE) == false)
endfunction
function SoulCatcher takes nothing returns nothing
local trigger trg = GetTriggeringTrigger()
local integer id = GetHandleId(trg)
local real dur = LoadReal(udg_hash, id, 2)
local unit u
if (dur > 0) then
set u = LoadUnitHandle(udg_hash, id, 0)
call SetUnitState(u, UNIT_STATE_LIFE, GetUnitState(u, UNIT_STATE_LIFE) + (LoadReal(udg_hash, id, 1) * 0.10))
call SaveReal(udg_hash, id, 2, dur - 0.10)
else
call DestroyTrigger(trg)
call FlushChildHashtable(udg_hash, id)
endif
set trg = null
set u = null
endfunction
function SoulCatcher_A takes nothing returns nothing
local trigger trg = CreateTrigger()
local integer id = GetHandleId(trg)
call SaveUnitHandle(udg_hash, id, 0, GetKillingUnit())
call SaveReal(udg_hash, id, 1, GetUnitState(GetTriggerUnit(), UNIT_STATE_LIFE) * 0.25)
call SaveReal(udg_hash, id, 2, 1.00)
call TriggerRegisterTimerEvent(trg, 0.10, true)
call TriggerAddAction(trg, function SoulCatcher)
set trg = null
endfunction
function InitTrig_map takes nothing returns nothing
local trigger trg = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(trg, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddCondition(trg, Condition(function SoulCatcher_C))
call TriggerAddAction(trg, function SoulCatcher_A)
set trg = null
endfunction
Am I doing it right, the basics, at least?