• 🏆 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] Problem with EVENT_PLAYER_UNIT_ATTACKED

Status
Not open for further replies.
Level 3
Joined
Sep 14, 2007
Messages
43
my problem is that whenever the attacking unit is even starting to attack (without ever finishing the move) the trigger is launched.
Here is my trigger, if you find any bugs or know a way around my problem please help me:

JASS:
function EC_t_Actions takes nothing returns nothing
local unit u = GetHandleUnit(GetTriggeringTrigger(),"u")
local unit dummy
local unit a = GetAttacker() 
local unit attacked = GetTriggerUnit()
local integer level
if(a == u) then
set dummy = CreateUnit(GetOwningPlayer(u),'h014',GetUnitX(u),GetUnitY(u),1)
set level = GetUnitAbilityLevel(u,'A04C')
call UnitAddAbility(dummy,'A04E')
call SetUnitAbilityLevel(dummy,'A04E',level)
call IssueTargetOrder(dummy, "forkedlightning", attacked)
endif
set u = null
set a = null
call RemoveUnit(dummy)
set dummy = null
set attacked = null
endfunction

function Trig_Electric_Charge_Conditions takes nothing returns boolean
    if ( not ( GetSpellAbilityId() == 'A04C' ) ) then
        return false
    endif
    return true
endfunction

function Trig_Electric_Charge_Actions takes nothing returns nothing
local unit u = GetTriggerUnit()
local trigger EC = CreateTrigger()
local integer level = GetUnitAbilityLevel(u,'A04C')
if(GetUnitTypeId(u) == 'N02G') then
call TriggerRegisterAnyUnitEventBJ( EC, EVENT_PLAYER_UNIT_ATTACKED )
call TriggerAddAction(EC, function EC_t_Actions)
call SetHandleHandle(EC,"u",u)
call TriggerSleepAction(20.3)
endif
call DestroyTrigger(EC)
set u = null
endfunction

//===========================================================================
function InitTrig_Electric_Charge takes nothing returns nothing
    set gg_trg_Electric_Charge = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Electric_Charge, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Electric_Charge, Condition( function Trig_Electric_Charge_Conditions ) )
    call TriggerAddAction( gg_trg_Electric_Charge, function Trig_Electric_Charge_Actions )
endfunction
 
Level 11
Joined
Feb 18, 2004
Messages
394
Before reading your code: you've hit one of the biggest problems in attack detection. Its (nigh) impossible to accurately detect when a unit finishes an attack. That event fires when a unit starts an attack. There are many (not perfect) solutions to that problem, search around.
 
Level 10
Joined
Jul 14, 2004
Messages
463
It is possible to get the moment a unit gets actually hurt by another by using
JASS:
call TriggerRegisterUnitEvent( YourTrigger, UnitThatIsAttacked, EVENT_UNIT_DAMAGED )
Here you can already see the problem with this event: You'll have to add the event for every unit, that may be attacked, but we are lucky that this is not a big problem in JASS. :wink:

PS: indent your code, that makes it much easier to read, especially for others who do not yet know your script!
 
Level 10
Joined
Jul 14, 2004
Messages
463
Just add a condition and the problem is gone, isn't it?
At least it works, even if not very economic.

Another possibility would be to use the
JASS:
call TriggerRegisterAnyUnitEventBJ( EC, EVENT_PLAYER_UNIT_ATTACKED )
and use a timer that makes it wait the required time, but that's so difficult to script until it runs as it should, that I doubt that it is worth the effort.
By the way, this will fire every time unit gets attacked, what is not much less often than the event that a unit gets damage.
How would you combine these events?
 
Level 11
Joined
Aug 25, 2006
Messages
971
A quick question, is it possible to get the damaging unit with the EVENT_UNIT_DAMAGED event? If its possible then I would be very happy. (Also if it is possible, whats the function that returns the damaging unit?)

Edit: Changed it to the event I meant to say.
 
Last edited:
Level 10
Joined
Jul 14, 2004
Messages
463
Well, there are the two different events EVENT_PLAYER_UNIT_ATTACKED and EVENT_UNIT_DAMAGED. So you should keep careful using the words damage and attack here. :wink:
Anyways, as far as I know, the function GetAttacker() will return the unit you want for both events.
 
Level 11
Joined
Aug 25, 2006
Messages
971
Ok since I was so tired I wrote about the wrong event, I meant to say EVENT_UNIT_DAMAGED. Unfortunitly the GetAttacker() doesn't seem to work for EVENT_UNIT_DAMAGED. Though thats what I was looking for. Its a shame it doesn't work. Does anyone know anything that will return the damaging unit?
 
Status
Not open for further replies.
Top