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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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