- Joined
- Jun 23, 2007
- Messages
- 4,066
Recycle
v0.1.4
v0.1.4
What is recycle?
Recycle keeps your handle count lower by re-using already created handles. Say you create two groups, then 'Recycle' them. Then somewhere else you want to create five more groups, your handle count will only increase 3 instead of 5 because it will re-use the two you made before.
How do I use it?
Normally when you create a new handle you do something like CreateGroup(). Instead you should use GetGroup(). That way the system will give you an already created/un-used group for use. If no free groups exists a new one will be created. This is the same for all the handles in this system.
Another important thing is to never destroy the handle, instead use RecycleHandle (replace Handle with the type). Below is an example explaining a little more.
Another important thing is to never destroy the handle, instead use RecycleHandle (replace Handle with the type). Below is an example explaining a little more.
JASS:
scope testt initializer onInit
globals
private group array g
endglobals
private function test takes nothing returns nothing
set g[1] = GetGroup()
set g[2] = GetGroup()
call BJDebugMsg(I2S(GetHandleId(g[1]))) // Creates new handle
call BJDebugMsg(I2S(GetHandleId(g[2]))) // Creates new handle
call RecycleGroup(g[1])
call RecycleGroup(g[2])
set g[3] = GetGroup()
set g[4] = GetGroup()
call BJDebugMsg(I2S(GetHandleId(g[3]))) // Gets g[2]'s old handle
call BJDebugMsg(I2S(GetHandleId(g[4]))) // Gets g[1]'s old handle
endfunction
private function onInit takes nothing returns nothing
call TimerStart(GetTimer(), 0.00, false, function test)
endfunction
endscope
Similar Systems?
Yes there are similar systems that recycle an individual handle type. But this systems allows you to specify any handle type you would like. If you are just going to recycle groups then use GroupUtils, same goes for other systems that recycle a handle. But if you plan on recycling multiple handle-types this system should suite you.
The Code
JASS:
library Recycle
//! textmacro RecycleHandle takes HANDLE, NAME, CREATE, CLEAR
globals
private $HANDLE$ array $NAME$
private integer $NAME$_COUNT = 0
endglobals
function Get$NAME$ takes nothing returns $HANDLE$
if $NAME$_COUNT == 0 or $NAME$[$NAME$_COUNT-1 ] == null then
debug if $NAME$[$NAME$_COUNT-1 ] == null then
debug call BJDebugMsg("Don't destroy recycled $NAME$'s!")
debug endif
return $CREATE$
endif
set $NAME$_COUNT = $NAME$_COUNT - 1
if $NAME$_COUNT > 8191 then
debug call BJDebugMsg("You have too many handles in your map, and recylcing still won't help you.")
return null
endif
return $NAME$[$NAME$_COUNT]
// returns the last free handle in the list.
endfunction
function Recycle$NAME$ takes $HANDLE$ h returns nothing
$CLEAR$ // Cleans up the handle
set $NAME$[$NAME$_COUNT] = h
set $NAME$_COUNT = $NAME$_COUNT + 1
// This function adds the recycled handle to the end of the list of free
// handles, thus allowing it to be used again.
endfunction
//! endtextmacro
//! runtextmacro RecycleHandle("timer", "Timer", "CreateTimer()", "call PauseTimer(h)")
//! runtextmacro RecycleHandle("group", "Group", "CreateGroup()", "call GroupClear(h)")
//! runtextmacro RecycleHandle("force", "Force", "CreateForce()", "call ForceClear(h)")
//! runtextmacro RecycleHandle("region", "Region", "CreateRegion()", "")
//! runtextmacro RecycleHandle("texttag", "TextTag", "CreateTextTag()", "call SetTextTagText(h, \"\", 0)")
//! runtextmacro RecycleHandle("dialog", "Dialog", "DialogCreate()", "call DialogClear(h)")
endlibrary
Last edited: