spell behaves strangely

waaaks!

Zinctified
Reaction score
256
ive got a vjass spell, tried it and it works correctly, but sometimes it behaves strangely and different from what i want

spell description
Creates a shield with negative energy to the target unit, making it immune to all damage, after the duration has finished while the shield is still active, the shield will be absorbed, dealing damage to the target unit if it is an enemy
the spell works like this

cast spell, buff the target, run timer for 5 seconds, after 5 seconds if the target still has the buff, deal damage if it is an enemy, do nothing if it is an ally, if the shield is still active, the target will not be damaged, the buff will be automatically removed after 5 seconds, or if the shield is destroyed, the shield has only (40 * level of ability) hit points, meaning if the shield is level 1, it can only block 40 damage then it will be destroyed, if it is level 5 the shield will have 200 hp and can only block 200 damage

the spell didnt work well, sometimes when i casted the spell at level 5 (200 hp shield), and attack the target, it will still take damage even though i only deal 100 attack damage, and when i casted the spell in my self i get healed each time i take damage, instead of just blocking it

now the code

JASS:
scope ShieldofDeath initializer init

globals
    private constant integer spell = 'A024'
    private constant integer buffid = 'B00H'
endglobals

private struct data
    unit cast
    unit targ
    integer level
    real life
    real counter
    real dmg
    
    method onDestroy takes nothing returns nothing
        if GetUnitAbilityLevel(this.targ, buffid) > 0 then
            if IsUnitEnemy(this.targ, GetOwningPlayer(this.cast)) then
                call UnitDamageTargetEx(this.cast, this.targ, this.dmg, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_DARK, true)
            endif
        endif
        call UnitRemoveAbility(this.targ, buffid)
    endmethod
endstruct

private function boom takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local data d = GetTimerStructA(t)
    call ClearTimerStructA(t)
    call DestroyTimer(t)
    call d.destroy()
    set t = null
endfunction
    
private function sodc takes nothing returns boolean
    return GetUnitAbilityLevel(GetTriggerUnit(), buffid) > 0
endfunction

private function soda takes nothing returns nothing
    local trigger trig = GetTriggeringTrigger()
    local data d = GetTriggerStructA(trig)
    local real dmg = GetEventDamage()
    set d.counter = d.counter + dmg
    if d.counter >= d.life then
        set dmg = 0
        set d.counter = 0
        call UnitRemoveAbility(d.targ, buffid)
        call ClearTriggerStructA(trig)
        call DisableTrigger(trig)
        set trig = null
    else
        call SetUnitState(d.targ, UNIT_STATE_LIFE, GetUnitState(d.targ, UNIT_STATE_LIFE) + dmg)
        set dmg = 0
    endif
endfunction

private function check takes nothing returns boolean
    return GetSpellAbilityId() == spell
endfunction

private function start takes nothing returns nothing
    local timer t = CreateTimer()
    local trigger trig = CreateTrigger()
    local data d = data.create()
    set d.cast = GetTriggerUnit()
    set d.targ = GetSpellTargetUnit()
    set d.level = GetUnitAbilityLevel(d.cast, spell)
    set d.life = 40 * d.level
    set d.dmg = 50 + (50 * d.level)
    set d.counter = 0
    call TriggerRegisterUnitEvent(trig, d.targ, EVENT_UNIT_DAMAGED)
    call TriggerAddCondition(trig, Condition(function sodc))
    call TriggerAddAction(trig, function soda)
    call TimerStart(t, 5, false, function boom)
    call SetTimerStructA(t,d)
    call SetTriggerStructA(trig,d)
    set t = null
endfunction

//===========================================================================
private function init takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(t, Condition(function check))
    call TriggerAddAction( t, function start )
endfunction

endscope
 

Forty

New Member
Reaction score
6
you should know that EVENT_UNIT_DAMAGED runs your trigger before the unit actually recieves damage. so you should run a 0.00 second timer and then block the damage in the callback function
 

waaaks!

Zinctified
Reaction score
256
you should know that EVENT_UNIT_DAMAGED runs your trigger before the unit actually recieves damage. so you should run a 0.00 second timer and then block the damage in the callback function
im sure that youre not sure about your answer
 

Viikuna

No Marlo no game.
Reaction score
265
That event really happens before unit looses its HP.

I have never coded a shield spell, but I believe that this is how they are done:

Add some bonus Max HP ability to prevent your unit from dying, then use 0.0 timer like Forty suggested. In timer callback unit has allready received the damage, so you should remove max hp ability, and set units hp back to what it was.

Events are kinda funny, because many of them happen before that thing actually happens. Like Unit Is Attacked event, and Pick Item event. I once had a problem with Pick Item event, because when it fired, my unit didnt have that item yet. A 0.0 second wait solved that problem.
 

waaaks!

Zinctified
Reaction score
256
there are lots of shield spell in the web and as i observed the code, it is similar to mine, and those spells ive seen dont have any problem, that means theres no problem in the event

EVENT_UNIT_DAMAGED runs whenever the unit takes damage, not like unit is attacked, it is different, because attacked triggers when the unit started it's attack "animation" not when u damaged the attacked unit
 

Viikuna

No Marlo no game.
Reaction score
265
You might be right.

I found some damage blocking spell, which used 0.0 timer, and Im gonna take a look of it later and see for what that timer is used.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top