Cokemonkey11
Spell Reviewer
- Joined
- May 9, 2006
- Messages
- 3,570
RecycleTimers
Preface:
After much discussion in another submission thread of mine ( http://www.hiveworkshop.com/forums/submissions-414/snippet-temporaryheroattribute-vjass-216972/ ), I've decided that a very simple, efficient timer recycler is worth posting.
My argument was that using TimerUtils is an overly-targeted system which was designed to be way too configurable and fill way too many uses, when in reality it's just a bloated system. I created an example of a spell using this simple timer recycler ( http://www.hiveworkshop.com/forums/2161637-post62.html ), and with the help of another hive user, realized that my system was not useful because calling jass functions is simply too slow a process to make it worth while, and that at this point simply creating and destroying timers is a better choice.
I admitted that was probably true and moved on, but after a short delay I decided to test this myself ( http://www.hiveworkshop.com/forums/2161951-post64.html ), and found that recycling timers really is worthwhile (At least twice as fast, basically).
So here you are. It's only 20 lines. Any idiot with basic programming knowledge could write their own version that does the same thing and more, but this should be extremely useful for newer vJass/JASS users. Especially those making spells.
Pre-requisites:
The script requires no other libraries to function, but as it is written in vJass, it DOES require JassHelper (Though I recommend just compiling with JNGP).
So what's the difference?
This is probably what you're wondering now. If you're using TimerUtils you're going to wonder what the difference is.
The answer is that this does the "recycle" part of TimerUtils, and nothing else. It won't handle hashtables for you or preload timers. But you can write your own script to do that, and you can handle hashtables yourself! 9 times out of 10, more efficiently than TU does, too.
API:
This method tells the system to stop using it and store it in a list. The list has no limit to it's size; it's your job to be smart.
This method gets a timer. If there are none available in the list, it creates a new one. That simple.
The script:
Example Usage:
Change Log:
2012.06.05 #2 - Updated the script to "extends array" to make the struct syntax worth while. Also changed the name to RecycleTimers.
2012.06.05 - Initial submission to Hive: Jass Resources: Submissions
Special Thanks:
Q/A:
But all the systems use TU already, and tons of spells too! Are they supposed to update to use yours? No. I don't expect this snippet to be used by public resources, but by users. If a public resource does use this, then you have to make a choice of which libraries to use. Using this in parallel with TU is twice is as bad as using one or the other.
My map uses TU but I want to take advantage of the speed benefit of this! What can I do? Start a new map or keep using TU. It's most likely not worth your time to change spells/systems to use this.
But this is gonna use a bunch of handle id's! Why can't I just destroy my timers? :< If you're worried about your timers using too many handles, you're probably using way too many timers, seeing as how the handle limit is over 8,000, and timers shouldn't even reach 1/100 of that. Regardless, this timer system doesn't give a shit about the source of the timer. You can recycle ones you CreateTimer()'d yourself, and destroy timers you RecycleTimers.get() (got).
Pastebin Mirror (Newest First):
http://pastebin.com/AvuEJT3T
Preface:
After much discussion in another submission thread of mine ( http://www.hiveworkshop.com/forums/submissions-414/snippet-temporaryheroattribute-vjass-216972/ ), I've decided that a very simple, efficient timer recycler is worth posting.
My argument was that using TimerUtils is an overly-targeted system which was designed to be way too configurable and fill way too many uses, when in reality it's just a bloated system. I created an example of a spell using this simple timer recycler ( http://www.hiveworkshop.com/forums/2161637-post62.html ), and with the help of another hive user, realized that my system was not useful because calling jass functions is simply too slow a process to make it worth while, and that at this point simply creating and destroying timers is a better choice.
I admitted that was probably true and moved on, but after a short delay I decided to test this myself ( http://www.hiveworkshop.com/forums/2161951-post64.html ), and found that recycling timers really is worthwhile (At least twice as fast, basically).
So here you are. It's only 20 lines. Any idiot with basic programming knowledge could write their own version that does the same thing and more, but this should be extremely useful for newer vJass/JASS users. Especially those making spells.
Pre-requisites:
The script requires no other libraries to function, but as it is written in vJass, it DOES require JassHelper (Though I recommend just compiling with JNGP).
So what's the difference?
This is probably what you're wondering now. If you're using TimerUtils you're going to wonder what the difference is.
The answer is that this does the "recycle" part of TimerUtils, and nothing else. It won't handle hashtables for you or preload timers. But you can write your own script to do that, and you can handle hashtables yourself! 9 times out of 10, more efficiently than TU does, too.
API:
public static method release takes timer time returns nothing
RecycleTimers.release(<timer>)
This method tells the system to stop using it and store it in a list. The list has no limit to it's size; it's your job to be smart.
public static method get takes nothing returns timer
RecycleTimers.get()
This method gets a timer. If there are none available in the list, it creates a new one. That simple.
The script:
JASS:
library RecycleTimers
struct SimpleTimers extends array
private static timer array availableTimers
private static integer maxAvailable=-1
public static method release takes timer time returns nothing
call PauseTimer(time)
set maxAvailable=maxAvailable+1
set availableTimers[maxAvailable]=time
endmethod
public static method get takes nothing returns timer
if maxAvailable==-1 then
return CreateTimer()
endif
set maxAvailable=maxAvailable-1
return availableTimers[maxAvailable+1]
endmethod
endstruct
endlibrary
Example Usage:
JASS:
//uses Knockback3D and RecycleTimers
scope thunderClap initializer i
globals
private constant integer THUNDERCLAP_CODE='AHtc'
private constant real POWER=35.
private constant real RADIUS=300.
private constant real TRAJECTORY=bj_PI/16.
private constant real DELAY=1.
private group grp=CreateGroup()
private hashtable ht=InitHashtable()
endglobals
private function a takes nothing returns nothing
local timer time=GetExpiredTimer()
local integer id=GetHandleId(time)
local real direc
local unit u=LoadUnitHandle(ht,id,0)
local real x=GetUnitX(u)
local real y=GetUnitY(u)
local unit FoG
call GroupEnumUnitsInRange(grp,GetUnitX(u),GetUnitY(u),RADIUS,null)
loop
set FoG=FirstOfGroup(grp)
exitwhen FoG==null
if FoG!=u then
set direc=Atan2(GetUnitY(FoG)-y,GetUnitX(FoG)-x)
call Knockback3D_add(FoG,POWER,direc,TRAJECTORY)
endif
call GroupRemoveUnit(grp,FoG)
endloop
call FlushChildHashtable(ht,id)
call RecycleTimers.release(time)
set u=null
set time=null
endfunction
private function c takes nothing returns boolean
local timer time
if GetSpellAbilityId()==THUNDERCLAP_CODE then
set time=RecycleTimers.get()
call SaveUnitHandle(ht,GetHandleId(time),0,GetTriggerUnit())
call TimerStart(time,DELAY,false,function a)
endif
return false
endfunction
private function i takes nothing returns nothing
local trigger t=CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t,Condition(function c))
set t=null
endfunction
endscope
Change Log:
2012.06.05 #2 - Updated the script to "extends array" to make the struct syntax worth while. Also changed the name to RecycleTimers.
2012.06.05 - Initial submission to Hive: Jass Resources: Submissions
Special Thanks:
- -Kobas- for creating a map submission template, which turned out useful for system submissions as well.
- Vexorian for developing JassHelper. Without vJass, I almost certainly would not still be scripting for wc3.
- The various developers of JNGP including PitzerMike and MindworX. Integrating JassHelper and TESH is a godsend.
Q/A:
But all the systems use TU already, and tons of spells too! Are they supposed to update to use yours? No. I don't expect this snippet to be used by public resources, but by users. If a public resource does use this, then you have to make a choice of which libraries to use. Using this in parallel with TU is twice is as bad as using one or the other.
My map uses TU but I want to take advantage of the speed benefit of this! What can I do? Start a new map or keep using TU. It's most likely not worth your time to change spells/systems to use this.
But this is gonna use a bunch of handle id's! Why can't I just destroy my timers? :< If you're worried about your timers using too many handles, you're probably using way too many timers, seeing as how the handle limit is over 8,000, and timers shouldn't even reach 1/100 of that. Regardless, this timer system doesn't give a shit about the source of the timer. You can recycle ones you CreateTimer()'d yourself, and destroy timers you RecycleTimers.get() (got).
Pastebin Mirror (Newest First):
http://pastebin.com/AvuEJT3T
Last edited: