Problems with BuffStruct&SpellStruct

Immolation

Member
Reaction score
20
JASS:
scope LightningStrike

    globals
        private constant integer ABILITY_ID = 'A006'
    endglobals
    
    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true)) * (0.8 + 0.2 * GetUnitAbilityLevel(caster, ABILITY_ID))
    endfunction
    
    private function Duration takes unit caster returns real
        return 10. + 2. * GetUnitAbilityLevel(caster, ABILITY_ID) + I2R(GetHeroInt(caster, true))
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct

        method onEffect takes nothing returns nothing
            local LightningStrike a = LightningStrike.create(caster)
            set a.damage = Damage(caster)
            call a.destroyTimed(Duration(caster))
        endmethod
            
        private static method onInit takes nothing returns nothing
            set thistype.abil = ABILITY_ID
        endmethod
    endstruct
endscope

JASS:
//! runtextmacro BuffType("LightningStrike")
    //! runtextmacro SetBuffName("Lightning Strike")
    //! runtextmacro SetBuffAlignment("POSITIVE")
    //! runtextmacro SetBuffTooltip("This unit is enhanced with lightning; its next attack will deal splashing damage.")
    //! runtextmacro SetBuffIcon("ReplaceableTextures\\CommandButtons\\BTNStormHammer.blp")
//! runtextmacro BuffStruct()
    private effect e
    real damage
    method onApply takes nothing returns nothing
        set this.e = AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", this.unit, "weapon")
    endmethod
    method onDamageDealt takes nothing returns nothing
        if Damage_IsAttack() then
            call Damage_Spell(this.unit, GetTriggerUnit(), .damage)
            call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", GetTriggerUnit(), "chest"))
            call .setUnit(null)
        endif
    endmethod
    method onRemove takes nothing returns nothing
        call DestroyEffect(this.e)
    endmethod
//! runtextmacro EndBuff()


So I got these two triggers here, but I got two problems with them:
1) The buff doesn't appear on the unit. (EDIT: Restarting NewGen seemed to fix it.)
2) If I use the ability again, the effect is created twice instead of cancelling the other instance. How to solve this?
 

Laiev

Hey Listen!!
Reaction score
188
2) check if the unit already got the buff, if yes, don't create, or destroy old and create a new
 

saw792

Is known to say things. That is all.
Reaction score
280
Now that I am thinking straight and it is no longer 3am:
JASS:
//! runtextmacro BuffType("LightningStrike")
    //! runtextmacro SetBuffName("Lightning Strike")
    //! runtextmacro SetBuffAlignment("POSITIVE")
    //! runtextmacro SetBuffTooltip("This unit is enhanced with lightning; its next attack will deal splashing damage.")
    //! runtextmacro SetBuffIcon("ReplaceableTextures\\CommandButtons\\BTNStormHammer.blp")
//! runtextmacro BuffStruct()
    private effect e
    real damage
    method onApply takes nothing returns nothing
        local TrackLightning t = TrackLightning[.unit]
        if t.lastinstance != 0 then
            call t.lastinstance.setUnit(null)
        endif
        set t.lastinstance = this
        set this.e = AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", this.unit, "weapon")
    endmethod
    method onDamageDealt takes nothing returns nothing
        if Damage_IsAttack() then
            call Damage_Spell(this.unit, GetTriggerUnit(), .damage)
            call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", GetTriggerUnit(), "chest"))
            call .setUnit(null)
        endif
    endmethod
    method onRemove takes nothing returns nothing
        set TrackLightning[.unit].lastinstance = 0
        call DestroyEffect(this.e)
    endmethod
//! runtextmacro EndBuff()

struct TrackLightning extends array
  //! runtextmacro AIDS()
  LightningStrike lastinstance

  method AIDS_onCreate takes nothing returns nothing
    set .lastinstance = 0
  endmethod

endstruct
 

Immolation

Member
Reaction score
20
@Laeiv, @13lade619
I do know that I have to check for the buff, but I don't know how.

@saw792
Thanks for the help, your trigger works(effect is destroyed) but the buff icon is removed upon casting the second time.
 

Jesus4Lyf

Good Idea™
Reaction score
397
@saw792
Thanks for the help, your trigger works(effect is destroyed) but the buff icon is removed upon casting the second time.
If a buff goes on a unit twice, and then one instance is removed, there is a bug that will remove the icon. It is a known BuffStruct bug which will be fixed somewhere after July 23 when I get time. :)
JASS:
//          Use .isOn(unit) --> boolean to see if a buff is on a unit.

From the BuffStruct documentation.

I haven't read the whole thread, just throwing in two cents. If it's not resolved I'll look into it properly. :)
 

Immolation

Member
Reaction score
20
Ok, so I decided to screw BuffStruct for now, waiting for an update :mad: xD
I'll stick with SpellStruct :p

Anyway, I now got a problem with this trigger:
JASS:

scope LightningStrike

    globals
        private constant integer Ability_ID = 'A006'
        private constant integer Buff_ID = 'B000'
        private constant real Duration = 5.00
        private constant real Radius = 236
        
        private constant string SfxOnDamaged = "Abilities\\Weapons\\Bolt\\BoltImpact.mdl"
    endglobals
    
    globals
        private unit Caster
    endglobals

    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true) + GetHeroInt(caster,true)) * (1.6 + 0.4 * GetUnitAbilityLevel(caster, Ability_ID))
    endfunction
    
    private function Splash takes unit caster returns real
        return 0.45 + 0.05 * GetUnitAbilityLevel(caster, Ability_ID)
    endfunction

    private function IsValidEnemy takes nothing returns boolean
        return IsUnitAlive(GetFilterUnit()) == true and IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(Caster)) == true
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct
    
        private method onDamage takes nothing returns nothing
            local unit damagedUnit = GetTriggerUnit()
            local unit current
            set Caster = GetEventDamageSource()
            if Damage_IsPhysical() and GetUnitAbilityLevel(Caster, Buff_ID) > 0 then
                call UnitRemoveAbility(Caster, Buff_ID)
                call Damage_Physical(Caster, damagedUnit, Damage(Caster), ATTACK_TYPE_HERO, false, false)
                call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", damagedUnit, "origin"))
                call GroupEnumUnitsInRange(GROUP, GetUnitX(damagedUnit), GetUnitY(damagedUnit), Radius, Filter(function IsValidEnemy))
                call GroupRemoveUnit(GROUP, damagedUnit)
                loop
                    set current = FirstOfGroup(GROUP)
                    exitwhen current == null
                    call Damage_Physical(Caster, current, (Damage(Caster) + GetEventDamage()) * Splash(Caster), ATTACK_TYPE_HERO, false, false)
                    call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", current, "origin"))
                    call GroupRemoveUnit(GROUP, current)
                    set current = null
                endloop 
            endif
        endmethod
            
        private method timerCallback takes nothing returns nothing
            call this.stopTimer(thistype.timerCallback)
            call UnitRemoveAbility(caster, Buff_ID)
        endmethod

        private method onEffect takes nothing returns nothing
            call this.startTimer(thistype.timerCallback, Duration)
            call Damage_RegisterEvent(this.createTrigger(thistype.onDamage))
        endmethod

        private static method onInit takes nothing returns nothing
            set thistype.abil = Ability_ID
        endmethod

    endstruct
endscope


The problem lies when I cast the spell again while it's still activated.
How can I stop the other timer from running its callback and thus removing the buff?
 

Jesus4Lyf

Good Idea™
Reaction score
397
You could store the spell instance on the unit using AIDS, and then call SomeAidsStruct[Caster].theSpellInstance.stopTimer(thistype.timerCallback) to stop the already running instance. Then overwrite with the instance attached to AIDS? Something like that may do it.

Actually, this looks fun to write. I might give it a shot myself when I get a chance. SpellStruct has built in AoE functionality which could shrink this down a lot, I reckon. :)

You know within the timer callback you can refer to the caster of the spell with just .caster? :p

You can also just .destroy() a spell instance at any point, which will stop all timers and destroy all triggers created for it. :)
 

Immolation

Member
Reaction score
20
I got no clue as to how to use AIDS :p

Also, SpellStruct has AoE functionality, but my spell has no target. Well, it has, but only when the unit is damaged, so I can't use this.forUnitsInRangeTarget, since there's no target automatically set by SpellStruct.

New code(with .caster):
JASS:
scope LightningStrike

    globals
        private constant integer Ability_ID = 'A006'
        private constant integer Buff_ID = 'B000'
        private constant real Duration = 5.00
        private constant real Radius = 236
        
        private constant string SfxOnDamaged = "Abilities\\Weapons\\Bolt\\BoltImpact.mdl"
    endglobals
    
    globals
        private unit Caster
    endglobals

    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true) + GetHeroInt(caster,true)) * (1.6 + 0.4 * GetUnitAbilityLevel(caster, Ability_ID))
    endfunction
    
    private function Splash takes unit caster returns real
        return 0.45 + 0.05 * GetUnitAbilityLevel(caster, Ability_ID)
    endfunction

    private function IsValidEnemy takes nothing returns boolean
        return IsUnitAlive(GetFilterUnit()) == true and IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(Caster)) == true
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct
    
        private method onDamage takes nothing returns nothing
            local unit damagedUnit = GetTriggerUnit()
            local unit current
            set Caster = GetEventDamageSource()
            if Damage_IsPhysical() and GetUnitAbilityLevel(Caster, Buff_ID) > 0 then
                call UnitRemoveAbility(Caster, Buff_ID)
                call Damage_Physical(Caster, damagedUnit, Damage(Caster), ATTACK_TYPE_HERO, false, false)
                call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", damagedUnit, "origin"))
                call GroupEnumUnitsInRange(GROUP, GetUnitX(damagedUnit), GetUnitY(damagedUnit), Radius, Filter(function IsValidEnemy))
                call GroupRemoveUnit(GROUP, damagedUnit)
                loop
                    set current = FirstOfGroup(GROUP)
                    exitwhen current == null
                    call Damage_Physical(Caster, current, (Damage(Caster) + GetEventDamage()) * Splash(Caster), ATTACK_TYPE_HERO, false, false)
                    call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", current, "origin"))
                    call GroupRemoveUnit(GROUP, current)
                    set current = null
                endloop 
            endif
        endmethod
            
        private method timerCallback takes nothing returns nothing
            call this.stopTimer(thistype.timerCallback)
            call UnitRemoveAbility(.caster, Buff_ID)
        endmethod

        private method onEffect takes nothing returns nothing
            if GetUnitAbilityLevel(caster, Buff_ID) > 0 then
                call this.stopTimer(thistype.timerCallback)
                call UnitRemoveAbility(caster, Buff_ID)
            else
                call this.startTimer(thistype.timerCallback, Duration)
            endif
            call Damage_RegisterEvent(this.createTrigger(thistype.onDamage))
        endmethod

        private static method onInit takes nothing returns nothing
            set thistype.abil = Ability_ID
        endmethod
    endstruct
endscope


My spell looks fun to code? Why thanks :D
 

13lade619

is now a game developer :)
Reaction score
399
JASS:
scope LightningStrike

    globals
        private constant integer ABILITY_ID = 'A006'
    endglobals
    
    //! runtextmacro PUI_PROPERTY("", "boolean", "LSActive", "false")
    
    //^ that does work. a global AIDS variable... so you can use it in the buffstruct.
    //^ i use those stuff in my map.
    
    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true)) * (0.8 + 0.2 * GetUnitAbilityLevel(caster, ABILITY_ID))
    endfunction
    
    private function Duration takes unit caster returns real
        return 10. + 2. * GetUnitAbilityLevel(caster, ABILITY_ID) + I2R(GetHeroInt(caster, true))
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct

        method onEffect takes nothing returns nothing
            local LightningStrike a 
        
            if not LSActive[.caster] then
                set LSActive[.caster] = true
                set a = LightningStrike.create(.caster)
                set a.damage = Damage(.caster)
                call a.destroyTimed(Duration(caster))
            endif
        
        endmethod
            
        private static method onInit takes nothing returns nothing
            set thistype.abil = ABILITY_ID
        endmethod
    endstruct
endscope

JASS:
//! runtextmacro BuffType("LightningStrike")
    //! runtextmacro SetBuffName("Lightning Strike")
    //! runtextmacro SetBuffAlignment("POSITIVE")
    //! runtextmacro SetBuffTooltip("This unit is enhanced with lightning; its next attack will deal splashing damage.")
    //! runtextmacro SetBuffIcon("ReplaceableTextures\\CommandButtons\\BTNStormHammer.blp")
//! runtextmacro BuffStruct()
    effect e
    real damage
    boolean finished
    method onApply takes nothing returns nothing
        set this.e = AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", this.unit, "weapon")
        set this.finished = false
    endmethod
    method onDamageDealt takes nothing returns nothing
        if Damage_IsAttack() and not this.finished then
            set this.finished = true
            set LSActive[this.unit] = false
            //used the AIDS from before.
            call Damage_Spell(this.unit, GetTriggerUnit(), this.damage)
            //
            //  Group stuff here.. haha.
            //
            //
            //
            call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", GetTriggerUnit(), "chest"))
            call DestroyEffect(this.e)
        endif
    endmethod
//! runtextmacro EndBuff()

.
 

Immolation

Member
Reaction score
20
Well, actually I was looking for a soluction without using BuffStruct. Its downsides are more than the upsides in my case. So...
 

Jesus4Lyf

Good Idea™
Reaction score
397
I told you this looks fun to code...
Here's one with BuffStruct, because I want to demonstrate PUI property with a BuffStruct instance. And using TU to make an extendable buff duration. :)
JASS:
//! runtextmacro PUI_PROPERTY("", "LightningStrike", "CurrentLightningStrike", "LightningStrike(0)")

//! runtextmacro BuffType("LightningStrike")
    //! runtextmacro SetBuffName("Lightning Strike")
    //! runtextmacro SetBuffAlignment("POSITIVE")
    //! runtextmacro SetBuffTooltip("This unit is enhanced with lightning; its next attack will deal splashing damage.")
    //! runtextmacro SetBuffIcon("ReplaceableTextures\\CommandButtons\\BTNStormHammer.blp")
//! runtextmacro BuffStruct()
    private effect e
    real damage = 0.
    method onApply takes nothing returns nothing
        set this.e = AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", this.unit, "weapon")
        set CurrentLightningStrike[unit]=this
    endmethod
    method onDamageDealt takes nothing returns nothing
        if Damage_IsAttack() then
            call Damage_Spell(this.unit, GetTriggerUnit(), .damage)
            call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\FarseerMissile\\FarseerMissile.mdl", GetTriggerUnit(), "chest"))
            call .setUnit(null)
        endif
    endmethod
    method onRemove takes nothing returns nothing
        call DestroyEffect(this.e)
        set CurrentLightningStrike[unit]=thistype(0)
    endmethod
    
    // simple logic to allow extension of a buff's duration.
    private static method onExpire takes nothing returns nothing
        local thistype this=thistype(GetTimerData(GetExpiredTimer()))
        call this.destroy()
    endmethod
    private timer destroyTimer
    method onCreate takes nothing returns nothing
        set destroyTimer=NewTimer()
        call SetTimerData(destroyTimer,this)
    endmethod
    method preDestroy takes nothing returns nothing
        call ReleaseTimer(destroyTimer)
    endmethod
    method operator duration= takes real timeLeft returns nothing
        call PauseTimer(destroyTimer)
        call TimerStart(destroyTimer,timeLeft,false,function thistype.onExpire)
    endmethod
//! runtextmacro EndBuff()

scope LightningStrike

    globals
        private constant integer ABILITY_ID = 'A006'
    endglobals
    
    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true)) * (0.8 + 0.2 * GetUnitAbilityLevel(caster, ABILITY_ID))
    endfunction
    
    private function Duration takes unit caster returns real
        return 10. + 2. * GetUnitAbilityLevel(caster, ABILITY_ID) + I2R(GetHeroInt(caster, true))
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct

        method onEffect takes nothing returns nothing
            local LightningStrike a = CurrentLightningStrike[caster]
            if a==0 then
                set a=LightningStrike.create(caster)
            endif
            set a.damage = a.damage + Damage(caster)
            set a.duration = Duration(caster)
        endmethod
            
        private static method onInit takes nothing returns nothing
            set thistype.abil = ABILITY_ID
        endmethod
    endstruct
endscope


Ok, and here's just a modification of your code, which strips out the Caster global. :)
JASS:
scope LightningStrike

    globals
        private constant integer Ability_ID = 'A006'
        private constant integer Buff_ID = 'B000'
        private constant real Duration = 5.00
        private constant real Radius = 236
        
        private constant string SfxOnDamaged = "Abilities\\Weapons\\Bolt\\BoltImpact.mdl"
    endglobals

    private function Damage takes unit caster returns real
        return I2R(GetHeroAgi(caster, true) + GetHeroStr(caster, true) + GetHeroInt(caster,true)) * (1.6 + 0.4 * GetUnitAbilityLevel(caster, Ability_ID))
    endfunction
    
    private function Splash takes unit caster returns real
        return 0.45 + 0.05 * GetUnitAbilityLevel(caster, Ability_ID)
    endfunction

    private struct Spell extends SpellStruct
        implement SpellStruct
        
        private method damageSplash takes unit splashTarget returns nothing
            if IsUnitAlive(GetFilterUnit()) == true and IsUnitEnemy(splashTarget, GetOwningPlayer(caster)) == true then
                call Damage_Physical(caster, splashTarget, (Damage(caster) + GetEventDamage()) * Splash(caster), ATTACK_TYPE_HERO, false, false)
                call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", splashTarget, "origin"))
            endif
        endmethod
    
        private method onDamage takes nothing returns nothing
            local unit damagedUnit = GetTriggerUnit()
            local unit current
            if Damage_IsPhysical() and GetUnitAbilityLevel(caster, Buff_ID) > 0 then
                call UnitRemoveAbility(caster, Buff_ID)
                call Damage_Physical(caster, damagedUnit, Damage(caster), ATTACK_TYPE_HERO, false, false)
                call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\Bolt\\BoltImpact.mdl", damagedUnit, "origin"))
                call GroupEnumUnitsInRange(GROUP, GetUnitX(damagedUnit), GetUnitY(damagedUnit), Radius, null) // shouldn&#039;t use null. Should use &quot;TRUE&quot;, or something. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile    :)" loading="lazy" data-shortname=":)" />
                call GroupRemoveUnit(GROUP, damagedUnit)
                call forGroup(GROUP, damageSplash)
            endif
        endmethod
            
        private method timerCallback takes nothing returns nothing
            call this.stopTimer(thistype.timerCallback)
            call UnitRemoveAbility(.caster, Buff_ID)
        endmethod

        private method onEffect takes nothing returns nothing
            if GetUnitAbilityLevel(caster, Buff_ID) &gt; 0 then
                call this.stopTimer(thistype.timerCallback)
                call UnitRemoveAbility(caster, Buff_ID)
            else
                call this.startTimer(thistype.timerCallback, Duration)
            endif
            call Damage_RegisterEvent(this.createTrigger(thistype.onDamage))
        endmethod

        private static method onInit takes nothing returns nothing
            set thistype.abil = Ability_ID
        endmethod
    endstruct
endscope

And yes, I intend to add all the fancy SpellStruct stuff to BuffStruct, like damage in AoE, etc. I just haven't, yet. This will become a lot faster in BuffStruct once I do, obviously. :p

Edit:
Was there anything else you wanted this to do? Or does that do it...
 

Immolation

Member
Reaction score
20
Your code with Buffstruct works perfectly! Thanks! :)
Though, could you explain what you're doing there?
Especially this line:
JASS:
//! runtextmacro PUI_PROPERTY(&quot;&quot;, &quot;LightningStrike&quot;, &quot;CurrentLightningStrike&quot;, &quot;LightningStrike(0)&quot;)


Also, how do I make the same for all my buffs?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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