HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

player X owns a certain unit of unit-type?

09-14-2006, 02:47 AM#1
Razernok
This may sound noobish but I just can't figure out how to do this.

How can I can to see if player X owns a unit of unit-type X?

for example:
Collapse JASS:
if player owns unit-type Archer then
   //Do something
endif
09-14-2006, 03:00 AM#2
The_AwaKening
Collapse JASS:
if GetOwningPlayer(GetTriggerUnit())==Player(0) then
    call Your Actions
endif

or maybe to find the player owning the unit

Collapse JASS:
function OwningPlayer takes nothing returns integer
 local unit u                // set to the unit you are checking
 local integer i=0
    loop
        exitwhen i>15
        if GetOwningPlayer(u)==Player(i) then
            exitwhen true
        endif
        set i=i+1
    endloop
    return i
endfunction
This would return the player number owning the unit.

edit -- I just re-read the question and see now what you are asking for.

Collapse JASS:
function OwningPlayer takes nothing returns boolean
 local group g = CreateGroup()
 local player p                      //depending on how you're using it to setup the player
 local integer unitId = 'earc'    //unit id for archer
 local unit u
 local boolean b = false
    call GroupEnumUnitsOfPlayer(g,p,null)
    loop
        set u=FirstOfGroup(g)
        exitwhen u==null
        if GetUnitTypeId(u)==unitId then
            set b = true
            exitwhen true
        endif
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g=null
    set u=null
    set p=null
    return b
endfunction
or
Collapse JASS:
function OwningPlayerBool takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit())=='earc'
endfunction

function OwningPlayer takes nothing returns boolean
 local group g = CreateGroup()
 local player p = GetTriggerPlayer()                     //depending on how you're using it to setup the player, this will change
 local boolexpr bx = Condition(function OwningPlayerBool)
 local boolean b = false
    call GroupEnumUnitsOfPlayer(g,p,bx)
    if FirstOfGroup(g)!=null then
        set b=true
    endif
    call DestroyGroup(g)
    call DestroyBoolExpr(bx)
    set p=null
    set g=null
    set bx=null
    return b
endfunction

Of course there are other methods depending on how you are using it.
09-14-2006, 03:01 AM#3
Razernok
I want to just see if player x has at least 1 unit of unit-type not find owner of a unit.
09-14-2006, 03:31 AM#4
The_AwaKening
The last 2 scripts I gave to you would do that. Put this in the trigger or in your custom script and call it.

Collapse JASS:
function PlayerOwnsUnit takes player whichPlayer, integer unitId returns boolean
 local group g = CreateGroup()
 local unit u
 local boolean b = false
    call GroupEnumUnitsOfPlayer(g,whichPlayer,null)
    loop
        set u=FirstOfGroup(g)
        exitwhen u==null
        if GetUnitTypeId(u)==unitId then
            set b = true
            exitwhen true
        endif
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g=null
    set u=null
    return b
endfunction

Then you would in your trigger use something like:
Collapse JASS:
 local player p = Set to your player
    if PlayerOwnsUnit(p,'earc') then
        Your actions
    endif
With this method, you could have whatever trigger use it and check for any type of unit by just chaning 'earc'
09-14-2006, 03:58 AM#5
Razernok
thanks, works great.