Add Effect Time to Unit

Status
Not open for further replies.
Level 10
Joined
May 24, 2016
Messages
339
I want to know your opinion - are these functions okay? I only use AddEffectTimeToUnit when want to make effect when it cant be instant destroyed.
Code:
function DestroyEffectTimeToUnit takes nothing returns nothing
local integer id = GetHandleId(GetExpiredTimer())
local real time = LoadReal(Hash,id,2)
if time <= 0. or GetWidgetLife(LoadUnitHandle(Hash,id,3)) <= 0. then
    call DestroyEffect(LoadEffectHandle(Hash,id,1))
    call DestroyTimer(GetExpiredTimer())
    call FlushChildHashtable(Hash,id)
endif
    call SaveReal(Hash,id,2,time - .2)
endfunction

function AddEffectTimeToUnit takes string eff,unit u,string attach,real time returns nothing
local timer t = CreateTimer()
    call SaveEffectHandle(Hash,GetHandleId(t),1,AddSpecialEffectTarget(eff,u,attach))
    call SaveReal(Hash,GetHandleId(t),2,time)
    call SaveUnitHandle(Hash,GetHandleId(t),3,u)
    call TimerStart(t,.2,true, function DestroyEffectTimeToUnit)
set t = null
endfunction

function DestroyEffectTimeToPoint takes nothing returns nothing
    call DestroyEffect(LoadEffectHandle(Hash,GetHandleId(GetExpiredTimer()),1))
    call DestroyTimer(GetExpiredTimer())
    call FlushChildHashtable(Hash,GetHandleId(GetExpiredTimer()))
endfunction

function AddEffectTimeToPoint takes string eff, real x, real y,real time returns nothing
local timer t = CreateTimer()
    call SaveEffectHandle(Hash,GetHandleId(t),1,AddSpecialEffect(eff,x,y))
    call TimerStart(t,time,false, function DestroyEffectTimeToPoint)
set t = null
endfunction
 
Level 45
Joined
Feb 27, 2007
Messages
5,578
Nothing specifically wrong about doing this. You could use fewer timers (one for all effects or use one per group of effects created at the same time that should be destroyed at the same time) or a timer allocating system like TimerUtils but those aren't necessary.
call SaveReal(Hash,id,2,time - .2)
In general it's not recommended to increment countdowns as reals because sometimes repeated small errors in the arithmetic can cause a condition to unexpectedly not be true:

x = 100
loop
x = x-0.01
exitwhen x == 0.00
endloop

might never exit because instead x actually goes to 0.0000001 instead of 0 or something like that. However, you are checking with the <= operator so it should be fine. In general you should use integers for your time counters because they won't be weird. Here you would do IntCount = R2I(time/0.2), so for time=6 IntCount would = 30. Then count down IntCount by 1 each timer iteration and remove it when IntCount <= 0. This will automatically round down so any time from 6 to 6.199999999 would actually only last 6 seconds. If you want to round properly do R2I(time/0.2 + 0.5).
 
Status
Not open for further replies.
Top