• 🏆 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!

How can I set the life of unit to 1 when it dies ?

Status
Not open for further replies.
Level 37
Joined
Mar 6, 2006
Messages
9,240
What you want to do is to block the damage so the unit will not die, and then set the health to 1. Is this correct?

If it is, then use "DamagEvent equal to 1" event.

Add these kind of actions
if damage >= current life of target + 0.41 then
set damage = current life of target - 1
endif

Might not work in 100% of scenarios but it gives you an idea.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
When life is set to 0.405 with a trigger/code, the unit dies. So units dies when their life equals or goes below 0.405.
There was some conversation about this a while ago and the results were inconclusive as someone kept changing the threshold. In any case the cutoff constant seemed to be exactly 0.405 although some people denied it and others argued over if it was "less than" or "less than or equal to".

It is worth noting that setting unit current life with triggers applies a HP delta rather than absolutely setting the health. The HP delta is subject to floating point error as it uses a 32bit float. For example if you were to set a unit with a billion HP to 1 life it will die because the floating point error of the delta results in it being set to 0 life.
 
Is it possible to set to zero too ?
Nope, there is no proper way to visually show anything lower then 1 besides the number going red since they didn't add a way to show negatives on the health bars. The interface can also only show integers so if you bypass 2.14 billion I think it is then you'll only see a black bar since it can't show something that is higher then what was coded.


There was some conversation about this a while ago and the results were inconclusive as someone kept changing the threshold. In any case the cutoff constant seemed to be exactly 0.405 although some people denied it and others argued over if it was "less than" or "less than or equal to".

It is worth noting that setting unit current life with triggers applies a HP delta rather than absolutely setting the health. The HP delta is subject to floating point error as it uses a 32bit float. For example if you were to set a unit with a billion HP to 1 life it will die because the floating point error of the delta results in it being set to 0 life.

It is less then or equal to 0.405 for anyone wondering.

To avoid floating point error you can set it twice, it usually fixes itself.
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
There was some conversation about this a while ago and the results were inconclusive as someone kept changing the threshold. In any case the cutoff constant seemed to be exactly 0.405 although some people denied it and others argued over if it was "less than" or "less than or equal to".

It is worth noting that setting unit current life with triggers applies a HP delta rather than absolutely setting the health. The HP delta is subject to floating point error as it uses a 32bit float. For example if you were to set a unit with a billion HP to 1 life it will die because the floating point error of the delta results in it being set to 0 life.

This is good info for everyone to read. I was wondering about the issue for a long time at some point. I remember testing setting the health to 0.405 repeatedly and it would not kill the unit every time. But sometimes it did. So there had to be some kind of rounding error or other issue.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
To avoid floating point error you can set it twice, it usually fixes itself.
I think if the unit falls below 0.405 life at any time it instantly dies so setting it twice would still kill the unit. The first set would kill the unit, the second set would raise the life of the corpse.

Also if the life is something insane you will need to set it more than twice anyway.

0.406 (which should not kill the unit) should read 0 health on the UI because it likely rounds to nearest. I know it does this in StarCraft II but there the mathematics have no floating point error.
 
I think if the unit falls below 0.405 life at any time it instantly dies so setting it twice would still kill the unit. The first set would kill the unit, the second set would raise the life of the corpse.

Also if the life is something insane you will need to set it more than twice anyway.

0.406 (which should not kill the unit) should read 0 health on the UI because it likely rounds to nearest. I know it does this in StarCraft II but there the mathematics have no floating point error.

If it hits or goes below 0.405 then usually the unit dies however you just have to change that so the first set doesn't kill the unit and the second set will work. Nope it works fine with just twice all the way up to the max real number in WC3 which is 27 zero's or billion power 3 (billion billion billion). Sadly 0.406 still shows 1 in red on the UI, wc3 rounding = bad.

He probably meant to set the life to 10,000 first and then to 0.41 avoiding the floating point error for units up to 99,999,999,999 hp

Nope I meant this;
  • Melee Initialization
    • Events
    • Time - Elapsed game time is 1.00 seconds
    • Conditions
    • Actions
    • Custom script: call SetWidgetLife( gg_unit_hfoo_0001, 0.41 )
    • Custom script: call SetWidgetLife( gg_unit_hfoo_0001, 0.41 )
    • Game - Display to (All players) the text: (String((Life of Footman 0001 <gen>)))
    • Set r = (Life of Footman 0001 <gen>)
all the way up to 1,000,000,000,000,000,000,000,000,000.
 
Last edited:
Status
Not open for further replies.
Top