• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Comparing the several ways of detecting a dead unit.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi guys. What is the best way to detect whether a unit is dead or not?
JASS:
native UnitAlive takes unit returns boolean
JASS:
 GetUnitState(whichUnit, UNIT_STATE_LIFE) <= 0.405
JASS:
IsUnitType(u,UNIT_TYPE_DEAD)

What are the advantages and disadvantages of the above methods? Thanks.
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
JASS:
function IsUnitDead takes unit u returns boolean
    return GetUnitState(whichUnit, UNIT_STATE_LIFE) < 0.405 or GetUnitTypeId(u) == 0 or IsUnitType(u, UNIT_TYPE_DEAD)
endfunction
^this and UnitAlive are both fine. IsUnitAlive requires no implementation, you just declare it. IsUnitDead has to defined on the other hand.

Using each of comparisons from IsUnitDead alone might provide false results depending on circumstances.

In many cases, don't restrict yourself - you don't have to code like someone else does - "the best way to do it" usually doesn't exist, because there is not just one way.
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
JASS:
function IsUnitDead takes unit u returns boolean
    return GetUnitState(whichUnit, UNIT_STATE_LIFE) < 0.405 or GetUnitTypeId(u) == 0 or GetUnitType(UNIT_TYPE_DEAD)
endfunction
^this and IsUnitAlive are both fine. IsUnitAlive requires no implementation, you just declare it. IsUnitDead has to defined on the other hand.

Using each of comparisons from IsUnitDead alone might provide false results depending on circumstances.

In many cases, don't restrict yourself - you don't have to code like someone else does - "the best way to do it" usually doesn't exist, because there is not just one way.

Thanks. Bannar. Do you mean this?
JASS:
function IsUnitDead takes unit u returns boolean
    return GetUnitState(whichUnit, UNIT_STATE_LIFE) < 0.405 or GetUnitTypeId(u) == 0 or IsUnitType(u, UNIT_TYPE_DEAD)
endfunction

instead of this
JASS:
function IsUnitDead takes unit u returns boolean
    return GetUnitState(whichUnit, UNIT_STATE_LIFE) < 0.405 or GetUnitTypeId(u) == 0 or GetUnitType(UNIT_TYPE_DEAD)
endfunction

Also, by saying IsUnitAlive, you actually mean the native UnitAlive declaration, right?

Edit: as far as I can recall, each of them has its own bug, right? If so ,what are the bugs? For example, dead unit in War3 can be healed, that is one of them which causes the problem. Any others?
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
Fastest and the best -> native UnitAlive takes unit id returns boolean

Thanks, Malhorne. Do you know what "GetUnitType" is in Bannar's post? I don't think Jass has this function.
JASS:
function IsUnitDead takes unit u returns boolean
    return GetUnitState(whichUnit, UNIT_STATE_LIFE) < 0.405 or GetUnitTypeId(u) == 0 or GetUnitType(UNIT_TYPE_DEAD)
endfunction
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
It was a misstype since I was writing this manually second before I went with my dog to take a walk. IsUnitAlive -> UnitAlive; GetUnitType -> IsUnitType. Fixed.

Again, there is nothing wrong with using either of methods. Remember tho, that IsUnitDead required all of 3 conditions to work properly.
 
Level 11
Joined
Oct 11, 2012
Messages
711
IsUnitType
Got it. :)
It was a misstype since I was writing this manually second before I went with my dog to take a walk. IsUnitAlive -> UnitAlive; GetUnitType -> IsUnitType. Fixed.

Again, there is nothing wrong with using either of methods. Remember tho, that IsUnitDead required all of 3 conditions to work properly.

Ok, shouldn't GetUnitTypeId() and IsUnitType() works for the same purpose? I mean why do we need both of them? :)
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
Nope, Unit-Type is somewhat generic "template" of unit, i.e unit-kind or kin: unit-type footman :)
Given unit can have only one of such types. Ras Frostwhisper is always considered as Lich-type of unit, and by no means can be compared with Archmage type. Unit-type is constant and can not be changed.

The second, IsUnitType, is more of attribute than "type". It is taken into consideration when calculating various of things, e.g spell targets. Given unit can have more than one of such types e.g Barracks are considered as UNIT_TYPE_STRUCTURE and UNIT_TYPE_MECHANIC. Those types can be compared directly e.g Barrack-types (STRUCTURE) can be taken into consideration when comparing with Farm (also has type -STRUCTURE). During game, those types may vary.

Whatmore, GetUnitTypeId() returns constant integer coresponding to given unit-type whereas IsUnitType is used mostly as a filter, it returns boolean.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Nope, Unit-Type is somewhat generic "template" of unit, i.e unit-kind or kin: unit-type footman :)
Given unit can have only one of such types. Ras Frostwhisper is always considered as Lich-type of unit, and by no means can be compared with Archmage type. Unit-type is constant and can not be changed.

The second, IsUnitType, is more of attribute than "type". It is taken into consideration when calculating various of things, e.g spell targets. Given unit can have more than one of such types e.g Barracks are considered as UNIT_TYPE_STRUCTURE and UNIT_TYPE_MECHANIC. Those types can be compared directly e.g Barrack-types (STRUCTURE) can be taken into consideration when comparing with Farm (also has type -STRUCTURE). During game, those types may vary.

Whatmore, GetUnitTypeId() returns constant integer coresponding to given unit-type whereas IsUnitType is used mostly as a filter, it returns boolean.

Got it, +rep to you and Malhorne. :)
 
Status
Not open for further replies.
Top