Unit Recycling

Not open for further replies.
Level 16
Aug 20, 2009
Anyone mind explaining how Unit recycling works?
and how can it be applied?

i wanted to use this on projectile system,
anyone has it? is it a function? or is it a system?

Why do I want to recycle projectiles anyway?

Every time you create a new unit and destroy it, WC3's memory use goes up and doesn't seem to come back down. It causes performance degradation over time even if you don't leak.

Creating units is just about the slowest thing possible in WC3, too. If you try to make a machine gun that fires 20 bullets per second by creating dummy units, you will lag. With unit recycling, I have made a machine gun that can fire over 200 bullets per second before the framerate starts to dip (and I am sure it was because of the 200 bullets traveling, not because of creating 200 bullets). And if 200 bullets per second sounds excessive, just imagine that as 10 players each firing the 20 bullet per second machine guns.

Whether you notice any improvement in a particular map is going to depend on how many projectiles it creates over time and what interval they're created at. I highly recommend unit recycling for any shooter-type map that has guns firing at a high frequency. For other types of games, unit recycling should be optional.

GUI/JASS is fine by me.
Level 19
Feb 4, 2009
usually you create a dummy unit, move it from a source unit to some target and remove the dummy

but creating units is very painful for wc3 because the engine is not as good as it could be so it is better to hide the dummy unit instead of removing it and using it for the next shot which has the same model

of course you could use a dummy unit with no model at all but creating and destroying special effects leaks even more and is slow as well

so you should recycle units per unit type

but if you are shooting too many units at once you might get too many hidden units for wc3 to handle so you should remove some of them if they rise above a certain threshold
Level 16
Aug 20, 2009
i see, so i just need to hide them when they finish their projectile job,
and when they are needed, i reuse them,
when the units needed exceed units stored, i create them.

and when the units hidden is not used for a certain time, i remove them,
but isn't having many units on the map also lags the game?

thx anyways, +REP,

is there any example for a reference?
like a tutorial or a bullet system that uses recycling?. (around here or anywhere)
Level 31
Jul 10, 2007
Just use struct indexing and in the create new instance, make your dummy unit. In the recycle, unhide dummy unit. In destroy, hide dummy unit.

Here is very simple dummy unit recycling
struct Dummy extends array
    private static unit array r
    private static integer c=0
    static method make takes nothing returns unit
        if (0==c) then
            return CreateUnit(...)
        set c=c-1
        call ShowUnit(r[c],true)
        return r[c]
    static method recycle takes unit u returns nothing
        set r[c]=u
        call ShowUnit(u,false)
        set c=c+1

You can expand this to multiple unit types via a hashtable, but if you can get away with it, it's less overhead to just use 1 dummy recycler per unit type ; P.

Dr Super Good

Spell Reviewer
Level 63
Jan 18, 2005
The idea is basically to keep the unit and recycle it.

So what does recycling units mean? Instead of letting a unit be removed or die, you instead keep it alive and emulate its demise. You then put it into storage (usually off the playable field area and hidden). Next time you need a "new" unit of that type, you can instead bring back into play the same old unit you used previously (rehealing it etc). You can repeat the process as much as you want.

Advantages of this are numberous. For example, damage detection events are much easier to handle (as no units are getting removed). You do not need to null local units as the handle index will never get recycled. You also do not leak units (as a game engine bugs means that units do not get removed completly and cause a memory leak that can lead to an internal exasution of resources so a crash)

Disadvantages are also there. You can get a conceptual unit leak occuring where a unit type is made but only ever needed once. All on death behaviour has to be emulated via triggers.

For dummies and units which are not meant to die due to combat recycling however is easy. For a projectile system I encourage it due to new units causing a leak.
Not open for further replies.