• 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.

Limit Number of Units per Building

Status
Not open for further replies.
Is there a way (or a system) to limit the number of units a building can train? I don't just mean raise the limit for every X number if the building, but each building can train a fixed amount of units.

Eg: Barracks being limited to 5 Footmen. If I have 3 Barracks, I will be able to have a total of 15 Footmen, but one Barracks can ONLY train 5 of them.

One way I tried doing it is to use Charge Gold and Lumber and enable unit --> order train --> disable unit, but I couldn't queue them up because a building can't cast abilities while it's training a unit.

Another way I tried doing it was to just order the unit to cancel the unit if it counts beyond 5, but that doesn't work:
JASS:
function onTrainStart_Event takes nothing returns boolean
    
    local unit Source = GetTriggerUnit()
    local integer UnitType = GetTrainedUnitType()
    local integer id = 0
    
    if UnitType == 'vsd2' then
        set id = GetUnitUserData(Source)
        set MOD_AetherTendril_Count[id] = MOD_AetherTendril_Count[id] + 1
        if MOD_AetherTendril_Count[id] > 5 then
            call IssueImmediateOrderById(Source, 851976)
        endif
    endif
    
    return false
endfunction

//===========================================================================
function InitTrig_onTrainStart_Event takes nothing returns nothing
    set gg_trg_onTrainStart_Event = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( gg_trg_onTrainStart_Event, EVENT_PLAYER_UNIT_TRAIN_START )
    call TriggerAddCondition( gg_trg_onTrainStart_Event, function onTrainStart_Event )
endfunction

Any thoughts?
 

Ardenian

A

Ardenian

Idea 1: Create a dummy unit for the unit train, add it to all buildings that can train the
unittype and set the charges to remaining to the amount available.
Items without/with zero charges being added to a building are removed automaticallly.

Idea 2: Create a dummy ability spawning the unittype, checking upon cast if the
maximum train available is reached.

Idea 3: Create a dummy unit as requirement to train the unit. If no more unit should be trained, remove it.
This way, you would already have a tooltip message why a unit cannot be trained.

As for your solution, orders cannot be interrupted this way.
The event fires before the order is actually issued, or something.
In this thread is a general solution: http://www.hiveworkshop.com/forums/.../re-add-amov-ability-stop-point-order-279199/
 
Yeah GetTriggerUnit() should refer to the training building.
Also you pointing out triggering unit made me realise I forgot to null Source, so ty for that

EDIT: ok for some reason it's working now, but now the problem happens because EVENT_PLAYER_UNIT_TRAIN_START only fires when a unit is at the front of the queue, so queuing up units doesn't count up the integer variable that's supposed to order a cancel when the limit goes beyond 5. I tried with EVENT_PLAYER_UNIT_ISSUED_ORDER but I just hit the same problem as before with the queued up units refusing to get cancelled.
 
Last edited:
Status
Not open for further replies.
Top