HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

detecting attack

03-15-2004, 02:26 AM#1
Pyrus
is there any way to detect when a unit lands a blow, not just ordered to attack?
03-15-2004, 03:54 AM#2
Narwanza
You could do the famous takes damage workaround. It is easier in JASS. Here take this bit of code.

Code:
function ActualStuff takes nothing returns nothing
    //Do whatever actions you want here, this funciton is triggered when unit is actually hit
endfunciton

function AttackInit takes nothing returns nothing
    local trigger damage = CreateTrigger()
    call TriggerRegisterUnitEvent( damage, GetAttackedUnitBJ(), EVENT_UNIT_DAMAGED )
    call TriggerAddAction(damage,function ActualStuff)
endfunction

//================================================
function InitTrig_[color=green]TriggerNameHere[/color] takes nothing returns nothing
    local trigger attack = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( attack,EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddAction(attack,function AttackInit)
endfunction

It is not fool-proof, because it puts a tag on the unit as soon as the attack order is issued. If he is attacked by another unit before the first one's attack is able to hit, then you may have some code issues.
03-15-2004, 03:56 AM#3
Albiino-I
Jass.. The only language you can understand besides english..
03-15-2004, 08:02 AM#4
Kamux
Or you can add a wait in the trigger after the unit is attacked event.
03-15-2004, 02:33 PM#5
Vexorian
But that wait would need to know the exact missile speed of the attacking unit.
03-15-2004, 03:43 PM#6
Anitarf
Well, that wouldn't be a problem if you only wanted to make the trigger go when a specific unit-type attacks.
There are other issues, though; when the "unit is attacked" thingy happens, it is just when the attacking unit begins the attack; that means there is some time, more exactly "attack 1(or 2) - damage point" of seconds before the actuall damage is dealt (melee attacks) or missle fired (ranged attacks). However, if the attacking unit dies or is issued another order or is stunned or put to sleep or hexed or banished or ensnared or entangled (or polymorphed or aerial shackled) (and I probably forgot a few more...) during this period, then the attack will not fully occur; no damage dealt. You would therefore have to wait for [attack 1 - damage point] seconds and then check for all these possible no-attack conditions and then wait some additional time for ranged assaults and then do what you want to do.
03-15-2004, 05:20 PM#7
Pyrus
Thanks for the advice. I am still working on my trigger skills so I'm not starting JASS as yet. If there's no way around this I guess I'll just have to wait.
03-15-2004, 10:50 PM#8
Narwanza
That has the exact same problem mine does, it will fire if another unit attacks him first and the first unit will be left high and dry.