• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Made a simple builder recycle

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
Basically every time a building is to be done this unit was created, and then subsequently killed after it completed its building order. However for some reason I though it would be a good idea to make a recyler instead, is it worthwhile and how could I improve it?

Also I'm not sure if I cleaned up this hashtable properly:

JASS:
private function RecycleBlocker takes nothing returns nothing
        local timer t = GetExpiredTimer()
        call RecycleUnit(LoadUnitHandle(hash, GetHandleId(t), 0))
        call FlushChildHashtable(hash, GetHandleId(t))
        call DestroyTimer(t)
        set t = null
    endfunction

JASS:
library BuilderRecycle
    globals
        private group availible = CreateGroup()
        private unit builder
        constant integer CITY_BUILDER = 'uaco'
    endglobals
    
    function RecycleBuilder takes unit u returns nothing
        call GroupAddUnit(availible, u)
        call SetUnitX(u, 15400.)
        call SetUnitY(u, -15400.)
    endfunction
    
    function GetBuilder takes player p, real x, real y returns unit 
        set builder = FirstOfGroup(availible)
        if builder == null then 
            set builder = CreateUnit(p, CITY_BUILDER, x, y, 0)
            return builder
        endif
        call SetUnitOwner(builder, p, false)
        call SetUnitX(builder, x)
        call SetUnitY(builder, y)
        call GroupRemoveUnit(availible, builder)
        return builder 
    endfunction
endlibrary

Or this...

JASS:
library UnitRecycler initializer Init
    globals
        constant integer CITY_BUILDER = 'uaco'
        constant integer BUILD_BLOCKER = 'h005'
        private hashtable hash = InitHashtable()
        private group array availible
        private unit u
        private integer array uType
        private integer index = 1
    endglobals
    
    private function AddUnitType takes integer id returns nothing
        set availible[index] = CreateGroup()
        call SaveInteger(hash, id, 0, index)
        set index = index + 1
    endfunction
    
    private function Init takes nothing returns nothing
        call AddUnitType(CITY_BUILDER)
        call AddUnitType(BUILD_BLOCKER)
    endfunction
    
    function RecycleUnit takes unit u returns nothing
        call GroupAddUnit(availible[LoadInteger(hash, GetUnitTypeId(u), 0)], u)
        call SetUnitX(u, 15400.)
        call SetUnitY(u, -15400.)
    endfunction
    
    function GetRecyledUnit takes player p, integer uType, real x, real y returns unit 
        local integer i = LoadInteger(hash, uType, 0)
        if i > 0 then 
            set u = FirstOfGroup(availible[i])
            if u == null then
                set u = CreateUnit(p, uType, x, y, 0)
                return u
            endif
            call SetUnitOwner(u, p, false)
            call SetUnitX(u, x)
            call SetUnitY(u, y)
            call GroupRemoveUnit(availible[i], u)
            return u 
        endif
        return null
    endfunction
endlibrary
 
Last edited:
Status
Not open for further replies.
Top