• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical 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