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
 
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
 
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
 
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.
 
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
 
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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top