• 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.
  • 💡 We're thrilled to announce that our upcoming texturing contest is in the works, and we're eager to hear your suggestions! Please take this opportunity to share your ideas in this theme discussion thread for the Texturing Contest #34!
  • 🏆 Hive's 7th HD Modeling Contest: Icecrown Creature is now open! The frozen wastes of Icecrown are home to some of Azeroth’s most terrifying and resilient creatures. For this contest, your challenge is to design and model a HD 3D monster that embodies the cold, undead, and sinister essence of Icecrown! 📅 Submissions close on April 13, 2025. Don't miss this opportunity to let your creativity shine! Enter now and show us your frozen masterpiece! 🔗 Click here to enter!

[JASS] Detecting train units queue

Status
Not open for further replies.
Level 6
Joined
May 7, 2009
Messages
228
Is there any way to determine if a building is currently training a unit or has units queued?

I tried the whole EVENT_PLAYER_UNIT_BEGIN/CANCEL/FINISH thing
but it didn't work because a CANCEL even is generated whenever you cancel a unit, even if there are others in the queue.

Any ideas?
 
Level 3
Joined
Aug 9, 2008
Messages
60
Is there any way to determine if a building is currently training a unit or has units queued?

I tried the whole EVENT_PLAYER_UNIT_BEGIN/CANCEL/FINISH thing
but it didn't work because a CANCEL even is generated whenever you cancel a unit, even if there are others in the queue.

Any ideas?

Why not store the amount of units being trained, so when you cancel you can lower the number by 1, so if it's still above 1 the building is training more units.
 
Level 6
Joined
May 7, 2009
Messages
228
Because it doesn't tell you when a unit is queued, only when the actual training begins.

So if the player, say queued 7 units, the count would still be at 1. Then he if canceled all of them, the count would be at -6. See the problem?
 
Level 6
Joined
May 7, 2009
Messages
228
I managed to solve it using a combination of orders and normal events
I'll post my code here in case anyone else has the same problem.


Code:
    // in main
    set TrigTrainOrder=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(TrigTrainOrder, EVENT_PLAYER_UNIT_ISSUED_ORDER) 
    call TriggerAddAction(TrigTrainOrder,function FuncTrainOrder)
    set TrigTrainDone=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(TrigTrainDone, EVENT_PLAYER_UNIT_TRAIN_CANCEL) 
    call TriggerRegisterAnyUnitEventBJ(TrigTrainDone, EVENT_PLAYER_UNIT_TRAIN_FINISH) 
    call TriggerAddAction(TrigTrainDone,function FuncTrainDone)

Code:
function FuncTrainOrder takes nothing returns nothing
    local integer p = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
    local integer t = GetUnitTypeId(GetTriggerUnit())
    local string s = UnitId2String(GetIssuedOrderId())
    
    if s==null then   //UnitId2String returns null if the given order was not a unit training order
        return
    endif

    if t=='h000' or t=='h01S' or t=='h02Z' or t=='h03Q' then  //check whether triggering unit was a capital
        set capitalunitqueue = capitalunitqueue + 1   //increment unit training count
        if capitalunitqueue == 1 then //if the count is greater then 1, it was already removed
            call removeFromGroup(GetTriggerUnit(), p, 0)
        endif
    endif
endfunction

function FuncTrainDone takes nothing returns nothing
    local integer p = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
    local integer t = GetUnitTypeId(GetTriggerUnit())

    if t=='h000' or t=='h01S' or t=='h02Z' or t=='h03Q' then  //check whether triggering unit was a capital
        set capitalunitqueue = capitalunitqueue - 1   //decrement unit training count
        if capitalunitqueue <= 0 then //if the count is greater 0, add it back to the group
            call add2group(GetTriggerUnit(), p, 0)
        endif
    endif
endfunction
 
Last edited:
Status
Not open for further replies.
Top