| 04-13-2009, 02:29 AM | #1 |
Use this to destroy a handle after X amount of seconds. TimerUtils is optional. JASS: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 JASS:
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)
|
| 04-13-2009, 03:57 AM | #2 |
JASS://*************************************************************************** //* //* TimedHandles - By TriggerHappy187 //* //*************************************************************************** //* //* Installation //* Simply copy this script into your map, as well as TimerUtils. //* //*************************************************************************** //* //* Documentation //* //* All this script does is creates a handle and destroys it after a certain //* amount of time, so you don't have to worry about it. //* //*************************************************************************** //* //* The Public Functions //* //* StopTimed$NAME$ - Stopes the timed handle //* //* StartTimed$NAME$ - Starts the handle and it's timer, once it expires //* the handle will be destroyed. //* //*************************************************************************** library TimedHandles requires TimerUtils //! textmacro handles takes HANDLE,NAME,DESTROY private struct $HANDLE$struct $HANDLE$ var static method create takes $HANDLE$ var returns $HANDLE$struct local thistype s = thistype.allocate() set s.var = var return s endmethod endstruct private function Callback$NAME$ takes nothing returns nothing call $DESTROY$(s.var) call ReleaseTimer(GetExpiredTimer()) call s.destroy() endfunction function StartTimed$NAME$ takes $HANDLE$ var, real duration returns nothing local $HANDLE$struct s = $HANDLE$struct.create(var) local timer t = NewTimer() call SetTimerData(t, s) call TimerStart(t, duration, false, function Callback$NAME$) set t = null endfunction //! endtextmacro //! runtextmacro handles("effect","Effect","DestroyEffect") //! runtextmacro handles("lightning","Lightning","DestroyLightning") //! runtextmacro handles("weathereffect","WeatherEffect","RemoveWeatherEffect") endlibrary |
| 04-13-2009, 04:00 AM | #3 |
thanks for that dusk, for those who don't know, I never needed to store the timer, since I use GetExpiredTimer anyways. |
| 04-13-2009, 04:07 AM | #4 | |
JASS://*************************************************************************** //* //* TimedHandles - By TriggerHappy187 //* //*************************************************************************** //* //* Installation //* Simply copy this script into your map, as well as TimerUtils. //* //*************************************************************************** //* //* Documentation //* //* All this script does is creates a handle and destroys it after a certain //* amount of time, so you don't have to worry about it. //* //*************************************************************************** //* //* The Public Functions //* //* StopTimed$NAME$ - Stopes the timed handle //* //* StartTimed$NAME$ - Starts the handle and it's timer, once it expires //* the handle will be destroyed. //* //*************************************************************************** library TimedHandles requires TimerUtils //! textmacro handles takes HANDLE,NAME,DESTROY private struct $HANDLE$struct $HANDLE$ var endstruct private function Callback$NAME$ takes nothing returns nothing local timer t = GetExpiredTimer() local $HANDLE$struct id = GetTimerData(t) call $DESTROY$(id) call ReleaseTimer(t) call id.destroy() set t = null endfunction function StartTimed$NAME$ takes $HANDLE$ var, real duration returns nothing local $HANDLE$struct s = $HANDLE$struct.allocate() local timer t = NewTimer() set s.var = var call SetTimerData(t, s) call TimerStart(t, duration, false, function Callback$NAME$) set t = null endfunction //! endtextmacro //! runtextmacro handles("effect","Effect","DestroyEffect") //! runtextmacro handles("lightning","Lightning","DestroyLightning") //! runtextmacro handles("weathereffect","WeatherEffect","RemoveWeatherEffect") endlibrary Quote:
|
| 04-13-2009, 04:26 AM | #5 | |
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. |
| 04-13-2009, 10:33 AM | #6 |
Totaly useless. "Timed leaks episode 2" ExecuteFunc() + TriggerSleepAction() == profit. profit + textmacro == hurge profit without any other system timers and anything from "advanced vJASS coding" |
| 04-13-2009, 11:02 AM | #7 | |
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. |
| 04-13-2009, 11:30 AM | #8 |
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 ?" |
| 04-13-2009, 02:39 PM | #9 |
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: JASS://* All this script does is start a timer attached to a handle //* and destroys the handl after the timer expires. JASS:call StartTimedEffect(AddSpecialEffect("SomeFile.mdx", x, y), 2.) call StartTimedEffect(AddSpecialEffectTarget("SomeFile.mdx", SomeUnit, "chest"), 2.) |
| 04-13-2009, 03:44 PM | #10 |
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: JASS:call DestroyEffect(...) JASS:call DestroyEffectDelayed(...) JASS:function Destroy$NAME$Delayed takes ... Like this: JASS:library TimedHandles requires TimerUtils //! textmacro handles takes PREFIX,HANDLE,NAME,DESTROY private struct $HANDLE$struct $HANDLE$ var endstruct private function DestroyTimed$NAME$ takes nothing returns nothing local timer t = GetExpiredTimer() local $HANDLE$struct s = GetTimerData(t) call $DESTROY$(s.var) call ReleaseTimer(t) call s.destroy() set t = null endfunction function $PREFIX$$NAME$Delayed takes $HANDLE$ var, real duration returns nothing local $HANDLE$struct s = $HANDLE$struct.create() local timer t = NewTimer() set s.var = var call SetTimerData(t, s) call TimerStart(t, duration, false, function DestroyTimed$NAME$) set t = null endfunction //! endtextmacro //! runtextmacro handles("Destroy","effect","Effect","DestroyEffect") //! runtextmacro handles("Destroy","lightning","Lightning","DestroyLightning") //! runtextmacro handles("Remove","weathereffect","WeatherEffect","RemoveWeatherEffect") //! runtextmacro handles("Remove","item","Item","RemoveItem") //! runtextmacro handles("Destroy","ubersplat","Ubersplat","DestroyUbersplat") endlibrary |
| 04-13-2009, 04:28 PM | #11 |
Dusk, how about this one: Code:library TimedHandles requires TimerUtils //! textmacro handles takes HANDLE,DESTROY private struct $HANDLE$struct $HANDLE$ var endstruct private function DestroyTimed$HANDLE$ takes nothing returns nothing local timer t = GetExpiredTimer() local $HANDLE$struct s = GetTimerData(t) call $DESTROY$(s.var) call ReleaseTimer(t) call s.destroy() set t = null endfunction function $DESTROY$Delayed takes $HANDLE$ var, real duration returns nothing local $HANDLE$struct s = $HANDLE$struct.create() local timer t = NewTimer() set s.var = var call SetTimerData(t, s) call TimerStart(t, duration, false, function DestroyTimed$HANDLE$) set t = null endfunction //! endtextmacro //! runtextmacro handles("effect","DestroyEffect") //! runtextmacro handles("lightning","DestroyLightning") //! runtextmacro handles("weathereffect","RemoveWeatherEffect") //! runtextmacro handles("item","RemoveItem") //! runtextmacro handles("ubersplat","DestroyUbersplat") endlibrary |
| 04-13-2009, 04:46 PM | #12 |
Ooooh, yes. I like that one even more. I had noticed there was some overlap, but I forgot that 2+2=4. Yeah, Trigger, do what Deaod suggests. |
| 04-13-2009, 10:52 PM | #13 |
I bring updates! |
| 04-14-2009, 02:12 AM | #14 |
that NAME parameter is solely for internal naming, why dont you delete it? |
| 04-14-2009, 02:58 AM | #15 | |
Quote:
Done, thanks for noticing that. |
