![]() |
#1 |

Join Date: Feb 2008
Posts: 405
![]() ![]() |
![]() Use this to destroy a handle after X amount of seconds.
TimerUtils is optional. ![]() library TimedHandles uses optional TimerUtils /************************************************************** * * v1.0.4 by TriggerHappy * * Use this to destroy a handle after X amount seconds. * * It's very useful for things like effects where you may * want it to stay in the map for a little, but not worry * about the cleaning memory leak. By default it supports * effects, lightning, weathereffect, items, ubersplats, and units. * * If you want to add your own handle types copy a textmacro line * at the bottom and add whichever handle you want along with it's destructor. * * For example: * //! runtextmacro TIMEDHANDLES("handle", "DestroyHandle") * * Installation: * 1. Copy this script and over to your map inside a blank trigger. * 2. If you want more efficiency copy TimerUtils over as well. * * API: * call DestroyEffectTimed(AddSpecialEffect("effect.mdx", 0, 0), 5) * call DestroyLightningTimed(AddLightning("CLPB", true, 0, 0, 100, 100), 5) * * Credits to Vexorian for TimerUtils and his help on the script. * **************************************************************/ globals // Check if the handle is null before destroying it private constant boolean NULL_SAFETY = true // If you don't want a timer to be ran each instance // set this to true. private constant boolean SINGLE_TIMER = true // If you chose a single timer then this will be the speed // at which the timer will update private constant real UPDATE_PERIOD = 0.05 endglobals // here you may add or remove handle types //! runtextmacro TIMEDHANDLES("effect", "DestroyEffect") //! runtextmacro TIMEDHANDLES("lightning", "DestroyLightning") //! runtextmacro TIMEDHANDLES("weathereffect", "RemoveWeatherEffect") //! runtextmacro TIMEDHANDLES("item", "RemoveItem") //! runtextmacro TIMEDHANDLES("unit", "RemoveUnit") //! runtextmacro TIMEDHANDLES("ubersplat", "DestroyUbersplat") // Do not edit below this line //! textmacro TIMEDHANDLES takes HANDLE,DESTROY struct $HANDLE$Timed $HANDLE$ $HANDLE$_var static integer index = -1 static thistype array instance static if SINGLE_TIMER then static timer timer = CreateTimer() real duration real elapsed = 0 else static if not LIBRARY.TimerUtils then static hashtable table = InitHashtable() endif method destroy takes nothing returns nothing static if NULL_SAFETY then if (this.$HANDLE$_var != null) then call $DESTROY$(this.$HANDLE$_var) set this.$HANDLE$_var = null else endif else call $DESTROY$(this.$HANDLE$_var) endif static if SINGLE_TIMER then set this.elapsed = 0 endif call this.deallocate() endmethod private static method remove takes nothing returns nothing static if SINGLE_TIMER then local integer i = 0 local thistype this loop exitwhen i > thistype.index set this = instance[i] set this.elapsed = this.elapsed + UPDATE_PERIOD if (this.elapsed >= this.duration) then set instance[i] = instance[index] set i = i - 1 set index = index - 1 call this.destroy() if (index == -1) then call PauseTimer(thistype.timer) endif endif set i = i + 1 endloop else local timer t = GetExpiredTimer() static if LIBRARY.TimerUtils then local $HANDLE$Timed this = GetTimerData(t) call ReleaseTimer(t) call this.destroy() else local $HANDLE$Timed this = LoadInteger(table, 0, GetHandleId(t)) call DestroyTimer(t) set t = null call this.destroy() endif endif endmethod static method create takes $HANDLE$ h, real timeout returns $HANDLE$Timed local $HANDLE$Timed this = $HANDLE$Timed.allocate() static if SINGLE_TIMER then set index = index + 1 set instance[index] = this if (index == 0) then call TimerStart(thistype.timer, UPDATE_PERIOD, true, function thistype.remove) endif set this.duration = timeout else static if LIBRARY.TimerUtils then call TimerStart(NewTimerEx(this), timeout, false, function $HANDLE$timed.remove) else local timer t = CreateTimer() call SaveInteger(thistype.table, 0, GetHandleId(t), this) call TimerStart(t, timeout, false, function $HANDLE$Timed.remove) set t = null endif endif set this.$HANDLE$_var = h return this endmethod endstruct function $DESTROY$Timed takes $HANDLE$ h, real duration returns $HANDLE$Timed return $HANDLE$Timed.create(h, duration) endfunction //! endtextmacro endlibrary ![]() call DestroyEffectTimed(AddSpecialEffect("effect.mdx", 0, 0), 5) call DestroyLightningTimed(AddLightning("CLPB", true, 0, 0, 100, 100), 5) call RemoveUnitTimed(CreateUnit(Player(0), 'hfoo', 0, 0 ,0), 5) call RemoveItemTimed(CreateItem('ratf', 0, 0), 60) Last edited by TriggerHappy : 02-02-2014 at 08:41 AM. |
![]() |
![]() |
Sponsored Links - Login to hide this ad! |
|
![]() |
#3 |

Join Date: Feb 2008
Posts: 405
![]() ![]() |
![]() thanks for that dusk, for those who don't know, I never needed to store the timer, since I use GetExpiredTimer anyways.
|
![]() |
![]() |
![]() |
#4 | |
Obscurity, the Art
|
![]() ![]() Quote:
|
|
![]() |
![]() |
![]() |
#5 | |

Join Date: Feb 2008
Posts: 405
![]() ![]() |
![]() Quote:
Not entirely :P You try to destroy the struct instance, not the handle. You also allocate the struct in teh create function, instead of creating it. Other than that it's fine. |
|
![]() |
![]() |
![]() |
#6 |
obey
|
![]() Totaly useless.
"Timed leaks episode 2" ExecuteFunc() + TriggerSleepAction() == profit. profit + textmacro == hurge profit without any other system timers and anything from "advanced vJASS coding" |
![]() |
![]() |
![]() |
#7 | |
requires vJass
Code Moderator
|
![]() Quote:
If you are fine with imprecise timing and the inability to wait less than ~0.25 sec and you hate vJASS, the scripts section is probably not a good place for you to be visiting or posting in. |
|
![]() |
![]() |
![]() |
#8 |
User
Join Date: Oct 2006
Posts: 1,490
![]() |
![]() And also if you want a wait still running when the game is paused ...
It does what it say and could be useful, i would say "why not ?" Last edited by Troll-Brain : 04-13-2009 at 12:30 PM. |
![]() |
![]() |
![]() |
#9 |
Obscurity, the Art
|
![]() Vex, Ani, and I were all discussing the usefulness of this in IRC and apparently it wasn't useless for us, so. Oh, Vex also said last night that if he added more handles to this that he would approve it. Well he added more handles, but Vex probably went to sleep or something. This is ready for approval, I just have one request based on the documentation:
__________________![]() //* All this script does is start a timer attached to a handle //* and destroys the handl after the timer expires. ![]() call StartTimedEffect(AddSpecialEffect("SomeFile.mdx", x, y), 2.) call StartTimedEffect(AddSpecialEffectTarget("SomeFile.mdx", SomeUnit, "chest"), 2.) Last edited by Rising_Dusk : 04-13-2009 at 03:48 PM. |
![]() |
![]() |
![]() |
#10 |
Obscurity, the Art
|
![]() Also, I was thinking (and chatting with Ani about it), you should change your function name from StartTimed$NAME$ since it really isn't fluid with standard WC3 functions for destroying handles.
__________________Consider what WC3 does: ![]() ![]() ![]() Like this: ![]() Last edited by Rising_Dusk : 04-13-2009 at 04:50 PM. |
![]() |
![]() |
![]() |
#11 |
User
Join Date: Jan 2007
Posts: 542
![]() ![]() ![]() |
![]() Dusk, how about this one:
__________________![]() Last edited by Deaod : 04-13-2009 at 05:32 PM. |
![]() |
![]() |
![]() |
#13 |

Join Date: Feb 2008
Posts: 405
![]() ![]() |
![]() I bring updates!
|
![]() |
![]() |
![]() |
#14 |
User
Join Date: Jan 2007
Posts: 542
![]() ![]() ![]() |
![]() that NAME parameter is solely for internal naming, why dont you delete it?
__________________ |
![]() |
![]() |
![]() |
#15 | |

Join Date: Feb 2008
Posts: 405
![]() ![]() |
![]() Quote:
Done, thanks for noticing that. |
|
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
|
|