HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Get last trained unit?

07-28-2006, 09:30 PM#1
StRoNgFoE_2000
After hours apon hours of study, I finally understand JASS. Ok, first off here is what I'm trying to do. WEU adds a function to the map script when enabling advanced triggers for getting the gold cost of units. Unfortunately, it was buggy, (plus I don't want all that useless script in my map if all I'm doing is getting goldcost) so I'm trying to rewrite it a bit. All is good except removing a "last trained unit" within a function. Since there is no native or function to call, I am a little stumped on an appropriate method of finding the last trained unit, or at least a good alternative to removing it.

Here is what I have so far..
Collapse JASS:
//Of course this is to call the function.
call GetUnitGoldCost(someunit)

//Here is the function itself. The problem lies with "call IssueTrainOrderByIdBJ(R,B)" because there is no way to remove it directly.
function GetUnitGoldCost takes unit U returns integer
    local integer Diff
    local unit R
    local location TempPoint = GetRectCenter(gg_rct_Path)
    local integer B = GetUnitTypeId(U)
    set R = CreateUnitAtLoc(Player(15), 'nshf', TempPoint, 270)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_LUMBER)
    call SetPlayerState(Player(15),PLAYER_STATE_RESOURCE_FOOD_CAP,200)  
    set Diff = GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call UnitAddAbility(R,'Asud')
    call AddUnitToStock(R,B,1,1)    
    call IssueTrainOrderByIdBJ(R,B)    
    set Diff = Diff - GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call RemoveUnit(R)
    call RemoveLocation(TempPoint)
    set R = null
    set TempPoint = null
    return Diff
endfunction

The trigger works, only the trained unit is never removed. Anybody have any ideas?
07-28-2006, 09:40 PM#2
Alevice
If you just want Gold Cost, why not try this?
07-28-2006, 09:49 PM#3
The)TideHunter(
Just add a local trigger, add event a unit is sold, and action remove sold unit.
I added it to your code:

Collapse JASS:
//Of course this is to call the function.
call GetUnitGoldCost(someunit)

//Here is the function itself. The problem lies with "call IssueTrainOrderByIdBJ(R,B)" because there is no way to remove it directly.
function RemoveSoldUnit takes nothing returns nothing
    call RemoveUnit(GetSoldUnit())
    call DestroyTrigger(GetTriggeringTrigger))
endfunction

function GetUnitGoldCost takes unit U returns integer
    local integer Diff
    local unit R
    local location TempPoint = GetRectCenter(gg_rct_Path)
    local integer B = GetUnitTypeId(U)
    local trigger t = CreateTrigger()
    set R = CreateUnitAtLoc(Player(15), 'nshf', TempPoint, 270)
    call TriggerRegisterUnitEvent(t, R, EVENT_UNIT_SELL)
    call TriggerAddAction(t, function RemoveSoldUnit)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_LUMBER)
    call SetPlayerState(Player(15),PLAYER_STATE_RESOURCE_FOOD_CAP,200)  
    set Diff = GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call UnitAddAbility(R,'Asud')
    call AddUnitToStock(R,B,1,1)    
    call IssueTrainOrderByIdBJ(R,B)    
    set Diff = Diff - GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call RemoveUnit(R)
    call RemoveLocation(TempPoint)
    set R = null
    set TempPoint = null
    return Diff
endfunction
07-28-2006, 11:02 PM#4
StRoNgFoE_2000
Alevice: That is similar to the set of functions I modifed this script from. The reason I modified it is because I am using a guaranteed pathable location so the function to check if pathable isn't necessary, that and it created witchdoctors and flying sheep around the map and didn't remove them correctly. Probably an error on my part, but I didn't feel like troubleshooting it.

Tidehunter: There were a few syntax errors and I changed a few things around but the idea worked. I just added an if/then to make sure it only affects the two types of buildings used.

Collapse JASS:
function RemoveSoldUnit takes nothing returns nothing
    if (IsUnitType(GetSoldUnit(), UNIT_TYPE_MECHANICAL)) or (IsUnitType(GetSoldUnit(), UNIT_TYPE_ANCIENT)) then
        call RemoveUnit(GetSoldUnit())
    endif
endfunction

function GetUnitGoldCost takes unit U returns integer
    local integer Diff
    local unit R
    local location TempPoint = GetRectCenter(gg_rct_Path)
    local integer B = GetUnitTypeId(U)
    local trigger t = CreateTrigger()
    set R = CreateUnitAtLoc(Player(15), 'nshf', TempPoint, 270)
    call TriggerRegisterUnitEvent(t, R, EVENT_UNIT_SELL)
    call TriggerAddAction(t, function RemoveSoldUnit)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call AdjustPlayerStateBJ(50000,Player(15),PLAYER_STATE_RESOURCE_LUMBER)
    call SetPlayerState(Player(15),PLAYER_STATE_RESOURCE_FOOD_CAP,200)  
    set Diff = GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call UnitAddAbility(R,'Asud')
    call AddUnitToStock(R,B,1,1)    
    call IssueTrainOrderByIdBJ(R,B)    
    set Diff = Diff - GetPlayerState(Player(15),PLAYER_STATE_RESOURCE_GOLD)
    call RemoveUnit(R)
    call RemoveLocation(TempPoint)
    call DestroyTrigger(t)
    set R = null
    set TempPoint = null
    set t = null
    return Diff
endfunction

Also a quick noob question.. does everything here appear to look correct, Memory leak free and optimized?