- Joined
- Dec 12, 2008
- Messages
- 7,385
This is useful for recycling dummies efficiently. You can have up to 8191 unit recyclers.
I'd suggest using Bribe's MissileRecycler if you have a projectile system because this system only deals with giving a dummy unit fresh off a stack.
Bribe's system actually takes into account facing.
If you need simple unit recycling, just use this.
Demo Code:
Feel free to comment..
I'd suggest using Bribe's MissileRecycler if you have a projectile system because this system only deals with giving a dummy unit fresh off a stack.
Bribe's system actually takes into account facing.
If you need simple unit recycling, just use this.
JASS:
/**********************************************
*
* DummyRecycler
* v2.0.0.0
* By Magtheridon96
*
* - Allows the creation of a unit recycler given
* a unit id and a player.
*
* Optionally Requires:
* --------------------
*
* - Table by Bribe:
* - hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
*
* API:
* ----
*
* - struct DummyRecycler
*
* - static method register takes player whichPlayer, integer unitId returns thistype
* - Creates a new recycler given a player, a unit Id and a facing angle.
* - method get takes nothing returns unit
* - Takes a unit from the stack.
* - method release takes unit whichUnit returns nothing
* - Throws a unit into the stack.
* - method preload takes integer amount returns nothing
* - Preloads a number of units into the stack.
*
**********************************************/
library DummyRecycler requires optional Table
struct DummyRecycler extends array
private static thistype current = 0
static if LIBRARY_Table then
private Table array stack
else
private static hashtable ht = InitHashtable()
endif
private static player array owner
private static integer array id
private static integer array index
method preload takes integer amount returns nothing
local integer i = 0
local unit u
loop
set u = CreateUnit(owner[this], id[this], 0, 0, 0)
static if LIBRARY_Table then
set stack[this].unit[index[this]] = u
else
call SaveUnitHandle(ht, this, index[this], u)
endif
call ShowUnit(u, false)
set index[this] = index[this] + 1
set i = i + 1
exitwhen i == amount
endloop
set u = null
endmethod
static method create takes player p, integer i returns thistype
set current = current + 1
set owner[current] = p
set id[current] = i
set stack[current] = Table.create()
set index[current] = 0
return current
endmethod
method get takes nothing returns unit
static if LIBRARY_Table then
if index[this] == 0 then
set stack[this].unit[0] = CreateUnit(owner[this], id[this], 0, 0, 0)
else
set index[this] = index[this] - 1
call ShowUnit(stack[this].unit[index[this]], true)
endif
return stack[this].unit[index[this]]
else
if index[this] == 0 then
call SaveUnitHandle(ht, this, 0, CreateUnit(owner[this], id[this], 0, 0, 0))
else
set index[this] = index[this] - 1
call ShowUnit(LoadUnitHandle(ht, this, index[this]), true)
endif
return LoadUnitHandle(ht, this, index[this])
endif
endmethod
method release takes unit u returns nothing
call ShowUnit(u, false)
static if LIBRARY_Table then
set stack[this].unit[index[this]] = u
else
call SaveUnitHandle(ht, this, index[this], u)
endif
set index[this] = index[this] + 1
endmethod
endstruct
endlibrary
Demo Code:
JASS:
struct Demo extends array
private static DummyRecycler stack
private static method lp takes nothing returns nothing
local unit u = stack.get()
call SetUnitX(u, 256)
call SetUnitY(u, 0)
// creating an effect to confirm the unit's presence :P
call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Human\\HolyBolt\\HolyBoltSpecialArt.mdl", GetUnitX(u), GetUnitY(u)))
call stack.release(u)
set u = null
endmethod
private static method onInit takes nothing returns nothing
// create a new recycler type
set stack = DummyRecycler.create(Player(15), 'Hpal')
// preload units to increase performance later.
call stack.preload(10)
// just for the vision ;D
call CreateUnit(Player(0), 'Hpal', 0, 0, 0)
call TimerStart(CreateTimer(), 1, true, function thistype.lp)
endmethod
endstruct
Feel free to comment..
Last edited: