Damage event

substance

New Member
Reaction score
34
Im making a unit that when he attacks an ally he will damage them like normal but then instantly heal them for the same amount he damaged them x 2.

JASS:
function Trig_MedicAttack_Conditions takes nothing returns boolean
return GetUnitTypeId(GetAttacker()) == 'E00W'
endfunction

function Trig_MedicAttack_Actions takes nothing returns nothing
local unit attacker = GetAttacker()
local unit attackee = GetTriggerUnit()
local real damage = GetEventDamage()

   if IsUnitAlly(attackee,GetOwningPlayer(attacker)) then
      call SetUnitState(attackee, UNIT_STATE_LIFE, GetUnitState(attackee, UNIT_STATE_LIFE) + damage * 2)
   endif
endif 
endfunction

//===========================================================================
function InitTrig_MedicAttack takes nothing returns nothing
    set gg_trg_MedicAttack = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_MedicAttack, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_MedicAttack, Condition( function Trig_MedicAttack_Conditions ) )
    call TriggerAddAction( gg_trg_MedicAttack, function Trig_MedicAttack_Actions )
endfunction


My problem is 'GetEventDamage()' doesnt work with EVENT_PLAYER_UNIT_ATTACKED, not to mention 'on attack' isnt a good method anyway

So that leaves me with EVENT_UNIT_DAMAGED, but that only works for a specific unit. So what can i do?

**oh and what ability can i base a 'Heal over time' spell, I tied unholy frenzy with changing the 'damage' to a negative value but that doesnt work and 'rejuvenation' is funky.**
 

Steel

Software Engineer
Reaction score
109
1st - The healing an ally thing. You need to register each unit with the event of Unit Takes Damage. Usually people do this when a unit enters the map (when it is spawned, it is added). Then once they are added, you can track things from there.

JASS:
function Enter takes nothing returns nothing
    local unit enterer = GetTriggerUnit()
    local trigger damage = CreateTrigger()
    local unitevent damage_event = EVENT_UNIT_DAMAGED
    call TriggerRegisterUnitEvent(damage,enterer,damage_event)
    call TriggerAddAction(damage,function damage_detect) 

endfunction

//============ Init Trig Functions ===========
function InitTrig_DetectDamage takes nothing returns nothing
    set gg_trg_DetectDamage = CreateTrigger()
    call TriggerRegisterEnterRectSimple(gg_trg_DetectDamage,GetPlayableMapRect())
    call TriggerAddAction(gg_trg_DetectDamage, function Enter)
endfunction


Then in the damage_detect, you can use GetTriggerUnit as the damaged unit. You will have some fun fixing the bugs with this though ;) It can be very tricky.


As for a heal over time, here is a template:

JASS:
globals
unit udg_HoTTarget
real udg_HoTSteps
endglobals

function HoT_Timer takes nothing returns nothing
local timer t = GetExpiredTimer()
local unit target = GetHandleUnit(t, "target")//Global Version: local unit targetglobal = udg_HoTTarget
local real steps = GetHandleReal(t, "steps")//Global Version: local integer steps = udg_HoTSteps
local integer healvalue = GetRandomInt(50,100) //Or whatever you want your heal range to be

//Terminating Condition
if steps <=1 then
call FlushHandleLocals(t)//Not needed if using globals
call DestroyTimer(t)
set t= null
set target = null
return
endif

//Now we can do this many ways, we can set the unit state life to the value you want
//Or you can cast a dummy spell to heal the unit.  I'm going to do the unit state
call SetUnitState(target, UNIT_STATE_LIFE, GetUnitState(target, UNIT_STATE_LIFE)+healvalue)


call SetHandleReal(t, "steps", steps-1) //Globals Version: set udg_HoTSteps=steps-1
//Cleanup
set t = null
set target = null
endfunction

function HoT_Template takes nothing returns nothing
local unit target = GetSpellTargetUnit()
local timer t = CreateTimer()
local real time = 2 //This number means every 2 seconds, we will heal the target.
local integer duration = 60 //We want it to last 60 seconds

//IF YOU ARE USING HANDLES
call SetHandleHandle(t, "target", target)//Globals Version:set udg_HoTTarget = target
call SetHandleReal(t, "steps", (duration/time))//Globals Version: set udg_HoTSteps = (duration/time)

call TimerStart(t, time, true, function HoT_Timer)
set target=null
endfunction


That template is MUI only if you use handle vars. If you use the globals it will not be MUI. That template can be used on any spell you cast, it doesn't matter, you could even make it a Damage over time if you set the healvalue to a negative number.
 

substance

New Member
Reaction score
34
yeh i thought about this, but if there's alot of units then thats alot of triggers =p. Just seems unefficient.

If there's no other option then I guess I'll do it this way.

*heh, thanks for the code.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
I don't see what the problem with the code that Steel gave. It's the best thing you can do. If you want to add to the trigger pre-placed units as well trigger that as well, getting at elapsed time 1.00 all units in a group and then doing what Steel did. For example:

JASS:

function example takes nothing returns nothing.
    local group g = CreatedGroup()
    local unit s

    call GroupEnumUnitsInRect(g,GetWorldBounds(),null)

    loop
        set s = FirstOfGroup(g)
        exitwhen s == null
        call TriggerRegisterUnitEvent(gg_trg_MedicAttack,s,EVENT_UNIT_DAMAGED)
        call GroupRemoveUnit(g,s)
    endloop

    call DestroyGroup(g)
    set g = null
    set s = null
 

substance

New Member
Reaction score
34
There's nothing wrong with that method I guess, it just seems extremely inefficient.

You essentially create a trigger for every unit on the map. That trigger runs a function everytime any unit in the game recieves damage. The likely hood of that event being what i need it for is too small. In fact An entire game on my map could be played without that unit attacking (so it'd be a huge waste).

I might just remove that unit's attack ability all together and replace it with a spell that essentially does the same thing.

I havent really looked into it , but there are a few 'OnAttack' systems on wc3c that might be just what I need.

And as far as the HoT goes, I made a similar function (Dot) a while ago and forgot I had it =p. I changed it so that it works with both now.

JASS:
function Trig_MedicAttack_Conditions takes nothing returns boolean
return GetUnitTypeId(GetAttacker()) == 'E00W'
endfunction

struct oTdata
unit targetunit
player caster
integer currenttime
integer maxtime
endstruct

function oT takes nothing returns nothing
local timer t = GetExpiredTimer()
local oTdata data = GetData(t)

set data.currenttime = data.currenttime + 1

if data.currenttime >= data.maxtime then
   call PauseTimer(t)
   call DestroyTimer(t)
   set t = null
   call oTdata.destroy(data)
   call UnitRemoveAbility(data.targetunit, 'B00E')
   call UnitRemoveAbility(data.targetunit, 'B00F')
endif

if IsUnitAlly(data.targetunit,data.caster) then
   if GetUnitAbilityLevel(data.targetunit, 'B00E') == 0 then
      call UnitAddAbility(data.targetunit,'B00E')
   endif
   call SetUnitState(data.targetunit, UNIT_STATE_LIFE, GetUnitState(data.targetunit, UNIT_STATE_LIFE) + 0.01 * GetUnitState(data.targetunit, UNIT_STATE_MAX_LIFE))
else
   if GetUnitAbilityLevel(data.targetunit, 'B00F') == 0 then
      call UnitAddAbility(data.targetunit,'B00F')
   endif
   call SetUnitState(data.targetunit, UNIT_STATE_LIFE, GetUnitState(data.targetunit, UNIT_STATE_LIFE) - 0.01 * GetUnitState(data.targetunit, UNIT_STATE_MAX_LIFE))
   endif
endfunction

function Trig_MedicAttack_Actions takes nothing returns nothing
local timer t = CreateTimer()
local oTdata data = oTdata.create()

set data.caster = GetOwningPlayer(GetAttacker())
set data.targetunit = GetTriggerUnit()
set data.maxtime = 12

if IsUnitType(data.targetunit,UNIT_TYPE_MECHANICAL) == false and GetUnitAbilityLevel(data.targetunit, 'B00C') == 0 then
   call SetData(t, integer(data))
   call TimerStart(t,1.00,true,function oT)
endif 
endfunction

//===========================================================================
function InitTrig_MedicAttack takes nothing returns nothing
    set gg_trg_MedicAttack = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_MedicAttack, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_MedicAttack, Condition( function Trig_MedicAttack_Conditions ) )
    call TriggerAddAction( gg_trg_MedicAttack, function Trig_MedicAttack_Actions )
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top