[Log in / Register]
| News | Chat | Pastebin | Donations | Tutorials | Rules | Forums | Starcraft II |
| Maps | Skins | Icons | Models | Spells | Tools | Jass | Packs |
(Keeps Hive Alive)
Go Back   The Hive Workshop > Warcraft III Resources > JASS Functions

JASS Functions Approved JASS functions will be located here.
Remember to submit your own resources to the submission forum.

Reply
 
LinkBack (10) Thread Tools
Old 07-13-2009, 09:32 AM   10 links from elsewhere to this Post. Click to view. #1 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
IsUnitDead

Basically, using a unit's life to detect if it's dead is a bad idea because a dead unit's life can still be modified when it is dead.

The best way to detect dead units is to use this library (requires vJASS):
(Special thanks to azlier for showing me this)
library dead

//returns true if the unit is alive. requires JassHelper
// to declare the AI native "UnitAlive" as a JASS native, so that
// we can use it in the trigger editor. If you trigger in vJASS,
// you should DEFINITELY use this since it is the only way to be
// sure that a unit is alive.
native UnitAlive takes unit id returns boolean

//returns true if the unit is dead. inline friendly.
function UnitDead takes unit u returns boolean
    return not UnitAlive(u)
endfunction

//returns true if the unit exists in the game. inline friendly.
function DoesUnitExist takes unit u returns boolean
    return GetUnitTypeId(u) != 0
endfunction

endlibrary
I also included the DoesUnitExist function, since that seems to be a useful and popular check that not many people know about.

Plain JASS alternative (put this in your trigger editor map header):
//returns true if the unit exists in the game
function DoesUnitExist takes unit u returns boolean
    return GetUnitTypeId(u) != 0
endfunction

//returns true if the unit is dead (or does not exist)
function IsUnitDead takes unit u returns boolean
    return IsUnitType(u, UNIT_TYPE_DEAD) or not DoesUnitExist(u)
endfunction
Old Explanation (no longer applies)
Summary

How do you detect if a unit is dead? Check if the unit’s life is less than .405, right? Wrong. There is only one way to tell if a unit is actually dead (or not alive):
function IsUnitDead takes unit u returns boolean
      return IsUnitType(u, UNIT_TYPE_DEAD) or GetUnitTypeId(u) == 0
endfunction
Stuff like GetUnitState(yourUnit, UNIT_STATE_LIFE) < 0.405 or GetWidgetLife(yourUnit) < .405 can and will cause bugs.


Why?

When a live unit's health goes anywhere below 0.405, it becomes dead. Its health is set to 0.000 and its unittype becomes UNIT_TYPE_DEAD. Once the unit is dead, its life can still be changed by triggers. This serves no practical purpose that I know of, and it does not revive the unit if its life is set above 0.405. However, this can result in bugs if you rely on using the unit's health to tell if it is dead or not. For example, if you forget to have a triggered AOE heal spell filter out dead units, it can heal a bunch of corpses. This would result in all of your other triggered spells thinking that the corpses are "alive." Then imagine the corpses being accidentally knocked back, frozen, meat-hooked, etc. Regardless of whether or not one spell remembers to check for dead units, it should not screw up all of your other spells.

So, the obvious solution is to use the unit's unittype. This unittype is updated automatically whenever the unit is killed, exploded, revived, etc. Unfortunately, IsUnitType returns false when the unit does not exist, meaning we could think a non-existent unit is alive. To make my function work for removed/null units, I also check if the unit's type id is 0. Now units that are null or no longer exist are considered "dead." No matter what mistakes you make, this function will always tell you whether a unit is dead or not.

Test Map

Some of you may want to verify the above information. For that reason, I have provided the map that I used to test things. In it, you will find a peasant whose basic information is displayed. You can damage, heal, and revive the peasant to see how the information changes. To damage the peasant, type "-damage x" where x is a real number. To heal the peasant, type "-heal x" where x is a real number. I disabled the peasant's health regeneration for testing purposes. To revive the peasant, use the paladin's skill. A zeppelin and necromancer are provided for further testing.

Credits, misc.

Please do NOT give me credits for this function. I do not know who first used unittype to detect dead units. I first saw it somewhere on wc3c and am only posting it here for the benefit of the community.

I did, however, do all the research on this topic and create the String library used in the test map.

Despite the small size of this function, I found it important enough to be in its own thread rather than in Small Code Snippets. After all, nearly every spell and map needs to detect dead units.
Attached Files
File Type: w3x dead test.w3x (26.2 KB, 49 views)

Last edited by maskedpoptart; 01-20-2010 at 12:46 AM.
maskedpoptart is offline   Reply With Quote
Old 07-13-2009, 10:41 AM   #2 (permalink)
Registered User Ciebron
I'm The Supervisor
 
Ciebron's Avatar
 
Join Date: Apr 2008
Posts: 730
Ciebron will become famous soon enough (110)Ciebron will become famous soon enough (110)Ciebron will become famous soon enough (110)
just use GetWidgetLife(yourUnit) < .405 right and u have no problems.
Ciebron is offline   Reply With Quote
Old 07-13-2009, 11:08 AM   #3 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
No offense, but I do not understand you people. Why use a function that is guaranteed to screw up some of the time? Who cares if it is insignificantly faster... the function I have posted does EXACTLY what yours is meant to do, no strings attached. Yours has to come with a disclaimer: "Oh and be careful never to modify the life of a dead unit or this function will cause all of your spells to screw up".

My function is modular programming and is user friendly. No matter what experience level you are, you can count on it to do what IsUnitDeadBJ should have done in the first place.
__________________
My JASS resourcesMy Spells
ListFire Wall
IsUnitDead
String functions
maskedpoptart is offline   Reply With Quote
Old 07-13-2009, 02:50 PM   #4 (permalink)
Registered User hvo-busterkomo
'Cos I had some sense
 
Join Date: Jun 2007
Posts: 1,267
hvo-busterkomo is a jewel in the rough (238)hvo-busterkomo is a jewel in the rough (238)hvo-busterkomo is a jewel in the rough (238)
Former Staff Member: This user used to be on the Hive Workshop staff. 
If the unit is flagged as dead then wouldn't IsUnitType(u, UNIT_TYPE_DEAD) work?
hvo-busterkomo is offline   Reply With Quote
Old 07-13-2009, 07:34 PM   #5 (permalink)
Registered User Viikuna
User
 
Viikuna's Avatar
 
Join Date: Aug 2008
Posts: 338
Viikuna is on a distinguished road (61)Viikuna is on a distinguished road (61)
People usually make some custom HealUnit function, which checks healed units health before changing it, so I dont really see why GetWidgetLife as a death check is so bad.

o_O

My personal favorite is:
Jass:
IsUnitInGroup( unit, DeadGuys )

Its just perfect. ( Part of unit recycling system, which naturally handles all death thingies nice and smooth )
__________________
No Marlo, no game.
Viikuna is offline   Reply With Quote
Old 07-13-2009, 09:41 PM   #6 (permalink)
Registered User aznricepuff
OOP freak.
 
Join Date: Feb 2006
Posts: 751
aznricepuff is a jewel in the rough (181)aznricepuff is a jewel in the rough (181)
Former Staff Member: This user used to be on the Hive Workshop staff. 
Jass:
GetUnitTypeId(u) == 0

Does this always work? Cuz if it does, there's absolutely no point in having the other one. Same argument goes for the second evaluation.
aznricepuff is offline   Reply With Quote
Old 07-14-2009, 12:59 AM   #7 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
Okay. I would prefer having the function as just return IsUnitType(u, UNIT_TYPE_DEAD) but this would not return the correct value for null units or units that no longer exist. For example, IsUnitType(null, UNIT_TYPE_DEAD) would return false, making you believe a non-existent unit is actually alive.

So if the unit does not exist, we need to return true. Otherwise, check if the unittype is UNIT_TYPE_DEAD. I have only found one way to check if a unit does not exist, and that is if GetUnitTypeId(u) == 0 . Then our function comes out to return GetUnitTypeId(u) == 0 or IsUnitType(u, UNIT_TYPE_DEAD)

I am 99% sure this function will always work. If you find any cases where it does not work, please let me know. Otherwise, I believe there is no point in using GetWidgetLife or GetUnitState to check for dead units.
__________________
My JASS resourcesMy Spells
ListFire Wall
IsUnitDead
String functions
maskedpoptart is offline   Reply With Quote
Old 07-14-2009, 01:38 AM   #8 (permalink)
Registered User Nestharus
User
 
Nestharus's Avatar
 
Join Date: Jul 2007
Posts: 427
Nestharus has little to show at this moment (31)Nestharus has little to show at this moment (31)Nestharus has little to show at this moment (31)Nestharus has little to show at this moment (31)
Rather interesting here : ).
Nestharus is offline   Reply With Quote
Old 07-14-2009, 09:30 PM   #9 (permalink)
Registered User Ciebron
I'm The Supervisor
 
Ciebron's Avatar
 
Join Date: Apr 2008
Posts: 730
Ciebron will become famous soon enough (110)Ciebron will become famous soon enough (110)Ciebron will become famous soon enough (110)
Quote:
Originally Posted by maskedpoptart View Post
I would prefer having the function as just return IsUnitType(u, UNIT_TYPE_DEAD)
why dont just use ISUnitType(u,UNIT_TYPE_DEAD) then? why we need a function that just adds that? Totally worthless if you ask me

and if u check a null unit handle u seriously need to recode..
Ciebron is offline   Reply With Quote
Old 07-15-2009, 12:03 AM   #10 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
If any of you have a big enough ego to think your map will always work when you want it to, use GetWidgetLife. If you are more obsessed with speed than functionality, use GetWidgetLife. Otherwise, use my function and rest assured that when you think a unit is dead, IT IS FUCKING DEAD

…or has been removed.

If you only need to check for dead units in filters, you can use IsUnitType(yourUnit, UNIT_TYPE_DEAD) in place of GetWidgetLife(yourUnit) < .405 . Otherwise, call this function ( IsUnitDead(yourUnit) ). It is shorter to type, not noticeably slower, and guaranteed to work for every circumstance.


Okay I'm done ranting. Sorry for the bit of swearing, kiddies.
__________________
My JASS resourcesMy Spells
ListFire Wall
IsUnitDead
String functions
maskedpoptart is offline   Reply With Quote
Old 07-15-2009, 12:40 AM   #11 (permalink)
Registered User aznricepuff
OOP freak.
 
Join Date: Feb 2006
Posts: 751
aznricepuff is a jewel in the rough (181)aznricepuff is a jewel in the rough (181)
Former Staff Member: This user used to be on the Hive Workshop staff. 
I would suggest putting the IsUnitType() function as the first operand. It seems to me that checking a unit that doesn't exist anymore is the exception rather than the rule.
aznricepuff is offline   Reply With Quote
Old 07-15-2009, 01:24 AM   #12 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
Quote:
Originally Posted by aznricepuff View Post
I would suggest putting the IsUnitType() function as the first operand. It seems to me that checking a unit that doesn't exist anymore is the exception rather than the rule.
You are absolutely right. It is changed now. Don't know why I had it like that...
__________________
My JASS resourcesMy Spells
ListFire Wall
IsUnitDead
String functions
maskedpoptart is offline   Reply With Quote
Old 07-15-2009, 01:00 PM   #13 (permalink)
Registered User Viikuna
User
 
Viikuna's Avatar
 
Join Date: Aug 2008
Posts: 338
Viikuna is on a distinguished road (61)Viikuna is on a distinguished road (61)
Quote:
"Oh and be careful never to modify the life of a dead unit or this function will cause all of your spells to screw up".

There is difference between being careful and not being stupid.

This function is useful, but so is GetWidgetLife and there is nothing wrong with GetWidgetLife, if you know how to code your stuff properly.
__________________
No Marlo, no game.
Viikuna is offline   Reply With Quote
Old 07-15-2009, 01:31 PM   #14 (permalink)
Registered User maskedpoptart
aspiring programmer
 
maskedpoptart's Avatar
 
Join Date: Aug 2006
Posts: 318
maskedpoptart is on a distinguished road (70)maskedpoptart is on a distinguished road (70)
Meh, I would never purposefully modify the life of a dead unit. I meant in case I somehow accidentally did so, my function would still work.

I know how to not be stupid and how to code well, but I recognize that I am (like you) only human, and prone to make mistakes. When these mistakes inevitably occur, I won't have to worry about this function making matters worse.

Anyway, thank you for recognizing the usefulness of this function.
__________________
My JASS resourcesMy Spells
ListFire Wall
IsUnitDead
String functions
maskedpoptart is offline   Reply With Quote
Old 07-15-2009, 04:51 PM   #15 (permalink)
Registered User Viikuna
User
 
Viikuna's Avatar
 
Join Date: Aug 2008
Posts: 338
Viikuna is on a distinguished road (61)Viikuna is on a distinguished road (61)
Well, it can be used, but I still would rather use GetWidgetLife.
__________________
No Marlo, no game.
Viikuna is offline   Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.hiveworkshop.com/forums/jass-functions-413/isunitdead-135811/
Posted By For Type Date
Worldedit : Voir le sujet - condition This thread Refback 02-19-2010 04:27 PM
VOTE - Spell-Contest (September09) - Seite 2 - inWarcraft Forum This thread Refback 01-24-2010 11:21 AM
Critic my (bad) projectile system - Page 2 - The Helper Forums This thread Refback 11-06-2009 04:14 PM
Variable keeps reference to removed dummy - The Helper Forums This thread Refback 10-02-2009 10:29 AM
Variable keeps reference to removed dummy - The Helper Forums This thread Refback 10-01-2009 04:49 PM
inWarcraft.de Foren by ingame. - VOTE - Spell-Contest (September09) This thread Refback 10-01-2009 04:07 PM
inWarcraft.de Foren by ingame. - VOTE - Spell-Contest (September09) This thread Refback 09-27-2009 11:51 PM
inWarcraft.de Foren by ingame. - VOTE - Spell-Contest (September09) This thread Refback 09-27-2009 10:48 AM
inWarcraft.de Foren by ingame. - VOTE - Spell-Contest (September09) This thread Refback 09-27-2009 06:54 AM
A few questions - The Helper Forums This thread Refback 08-21-2009 03:44 PM

All times are GMT. The time now is 10:09 AM.






Hosting by SliceHost 
Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.1
Copyright©Ralle