Problem with spell.

Carnerox

The one and only.
Reaction score
84
I made a spell which creates a glaive that circles around the caster for #n spins and then is destroyed after, but for some reason, when you cast it sometimes, the glaive will be created, but wont circle around the caster.


Here is the trigger.
JASS:
scope MoonGlaive
    globals
        private constant integer ABIL_ID = 'A003'
        private constant integer GLAIVE_ID = 'h00P'
        private constant real PERIODIC = 0.00235
        private constant string EFFECT_ID = "Abilities\\Spells\\Other\\Stampede\\StampedeMissileDeath.mdl"
        private constant string ATTACHMENT = "chest"
        private constant attacktype ATTACK_TYPE = ATTACK_TYPE_HERO
        private constant damagetype DAMAGE_TYPE = DAMAGE_TYPE_NORMAL
        private constant weapontype WEAPON_TYPE = null
        private unit GLAIVE
        private group GROUP
    endglobals
    
    native UnitAlive takes unit whitchUnit returns boolean
    
    private function Distance takes nothing returns real
        return 200.00
    endfunction
    
    private function MaxSpins takes integer level returns real
        return 2.00*level+2.00
    endfunction
    
    private function AreaOfEffect takes nothing returns real
        return 160.00
    endfunction
    
    private function Damage takes integer level returns real
        return 120.00
    endfunction

    struct MoonGlaive
        unit caster
        unit glaive
        player owner
        integer level
        integer spins
        real casterX
        real casterY
        real distance
        real angle

        static method onDamage takes nothing returns boolean
            local unit t = GetFilterUnit()
            if (IsUnitEnemy(t,GetOwningPlayer(GLAIVE)) and UnitAlive(t)) and not (IsUnitInGroup(t,GROUP)) then
                call UnitDamageTargetEx(GLAIVE,t,Damage,true,false,ATTACK_TYPE,DAMAGE_TYPE,WEAPON_TYPE)
                call DestroyEffect(AddSpecialEffectTarget(EFFECT_ID,t,ATTACHMENT))
                call GroupAddUnit(GROUP,t)
            endif
            set t=null
            return false
        endmethod
        

        static method onLoop takes nothing returns nothing
            local timer t = GetExpiredTimer()
            local thistype this = GetTimerData(t)
            local real x
            local real y
            set this.casterX=GetUnitX(this.caster)
            set this.casterY=GetUnitY(this.caster)
            if (this.angle>=360.00) then
                set this.angle=0.00
                set this.spins=this.spins+1
                call GroupClear(GROUP)
                if (this.spins>=MaxSpins(this.level)) then
                    call KillUnit(this.glaive)
                    call ReleaseTimer(t)
                    call ReleaseGroup(GROUP)
                    call DestroyGroup(GROUP)
                    call DestroyTimer(t)
                    set t=null
                    set GROUP=null
                endif
            else
                set this.angle=this.angle+1.00
            endif
            set x=this.casterX+Distance()*Cos(this.angle*bj_DEGTORAD)
            set y=this.casterY+Distance()*Sin(this.angle*bj_DEGTORAD)
            call SetUnitX(this.glaive,x)
            call SetUnitY(this.glaive,y)
            set GLAIVE=this.glaive
            call GroupEnumUnitsInArea(GROUP,x,y,AreaOfEffect(),Filter(function thistype.onDamage))
            set t=null
        endmethod
        
        static method onEffect takes nothing returns boolean
            local thistype this = thistype.allocate()
            local timer t = NewTimer()
            local real x
            local real y
            set this.caster=GetTriggerUnit()
            set this.owner=GetOwningPlayer(this.caster)
            set this.level=GetUnitAbilityLevel(this.caster,ABIL_ID)
            set this.casterX=GetUnitX(this.caster)
            set this.casterY=GetUnitY(this.caster)
            set this.angle=0.00
            set this.spins=0
            set x=this.casterX+Distance()*Cos(this.angle*bj_DEGTORAD)
            set y=this.casterY+Distance()*Sin(this.angle*bj_DEGTORAD)
            set this.glaive=CreateUnit(this.owner,GLAIVE_ID,x,y,0.00)
            set GROUP=NewGroup()
            call SetTimerData(t,this)
            call TimerStart(t,PERIODIC,true,function thistype.onLoop)
            set t=null
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger trig = CreateTrigger()
            call TriggerAddCondition(GT_RegisterStartsEffectEvent(trig,ABIL_ID),Condition(function thistype.onEffect))
        endmethod
    endstruct
endscope
 

Dirac

22710180
Reaction score
147
You're using TimerUtils and you're calling DestroyTimer(t) instead of call ReleaseTimer(t)
I highly suggest you swtich to T32, TU is very slow when it comes to periods
JASS:
scope MoonGlaive
    globals
        private constant integer ABIL_ID = 'A003'
        private constant integer GLAIVE_ID = 'h00P'
        private constant real PERIODIC = 0.00235
        private constant string EFFECT_ID = "Abilities\\Spells\\Other\\Stampede\\StampedeMissileDeath.mdl"
        private constant string ATTACHMENT = "chest"
        private constant attacktype ATTACK_TYPE = ATTACK_TYPE_HERO
        private constant damagetype DAMAGE_TYPE = DAMAGE_TYPE_NORMAL
        private constant weapontype WEAPON_TYPE = null
        private unit GLAIVE
        private group GROUP
    endglobals
    
    native UnitAlive takes unit whitchUnit returns boolean
    
    private function Distance takes nothing returns real
        return 200.00
    endfunction
    
    private function MaxSpins takes integer level returns real
        return 2.00*level+2.00
    endfunction
    
    private function AreaOfEffect takes nothing returns real
        return 160.00
    endfunction
    
    private function Damage takes integer level returns real
        return 120.00
    endfunction

    struct MoonGlaive
        unit caster
        unit glaive
        player owner
        integer level
        integer spins
        real casterX
        real casterY
        real distance
        real angle

        static method onDamage takes nothing returns boolean
            local unit t = GetFilterUnit()
            if (IsUnitEnemy(t,GetOwningPlayer(GLAIVE)) and UnitAlive(t)) and not (IsUnitInGroup(t,GROUP)) then
                call UnitDamageTargetEx(GLAIVE,t,Damage,true,false,ATTACK_TYPE,DAMAGE_TYPE,WEAPON_TYPE)
                call DestroyEffect(AddSpecialEffectTarget(EFFECT_ID,t,ATTACHMENT))
                call GroupAddUnit(GROUP,t)
            endif
            set t=null
            return false
        endmethod
        

        method periodic takes nothing returns nothing
            local real x
            local real y
            set this.casterX=GetUnitX(this.caster)
            set this.casterY=GetUnitY(this.caster)
            if (this.angle>=360.00) then
                set this.angle=0.00
                set this.spins=this.spins+1
                call GroupClear(GROUP)
                if (this.spins>=MaxSpins(this.level)) then
                    call KillUnit(this.glaive)
                    call ReleaseTimer(t)
                    call ReleaseGroup(GROUP)
                    call DestroyGroup(GROUP)
                    call .stopPeriodic()
                    set t=null
                    set GROUP=null
                endif
            else
                set this.angle=this.angle+1.00
            endif
            set x=this.casterX+Distance()*Cos(this.angle*bj_DEGTORAD)
            set y=this.casterY+Distance()*Sin(this.angle*bj_DEGTORAD)
            call SetUnitX(this.glaive,x)
            call SetUnitY(this.glaive,y)
            set GLAIVE=this.glaive
            call GroupEnumUnitsInArea(GROUP,x,y,AreaOfEffect(),Filter(function thistype.onDamage))
            set t=null
        endmethod
        implement T32x
        static method onEffect takes nothing returns boolean
            local thistype this = thistype.allocate()
            local real x
            local real y
            set this.caster=GetTriggerUnit()
            set this.owner=GetOwningPlayer(this.caster)
            set this.level=GetUnitAbilityLevel(this.caster,ABIL_ID)
            set this.casterX=GetUnitX(this.caster)
            set this.casterY=GetUnitY(this.caster)
            set this.angle=0.00
            set this.spins=0
            set x=this.casterX+Distance()*Cos(this.angle*bj_DEGTORAD)
            set y=this.casterY+Distance()*Sin(this.angle*bj_DEGTORAD)
            set this.glaive=CreateUnit(this.owner,GLAIVE_ID,x,y,0.00)
            set GROUP=NewGroup()
            call this.startPeriodic()
            set t=null
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            call TriggerAddCondition(GT_RegisterStartsEffectEvent(CreateTrigger(),ABIL_ID),Condition(function thistype.onEffect))
        endmethod
    endstruct
endscope
You should use more encapsulation too, must of the methods are public when i consider they shouldn't be. And maybe my new snippet might work for your glaive rotation: TCircles
 

Carnerox

The one and only.
Reaction score
84
I doubt the problem has to do with the rotation I use now, else it wouldn't work at all.
It's just when I first cast it, it works, then after that it either works or not.
 

Dirac

22710180
Reaction score
147
Did you even read my post? : / the circle's thing was just a suggestion... plus the code i posted should fix you spell entirely, but it uses T32 instead
 

BlackRose

Forum User
Reaction score
239
If he uses your code, what will he learn? However, you pointed out the reason anyways:
"You're using TimerUtils and you're calling DestroyTimer(t) and (he does both) [del]instead of[/del] call ReleaseTimer(t)"

What happens when you cast the spell:
A timer in the TimerUtils queue is allocated and run, when the glaive hits the maximum spins, the timer is released AND destroyed. This causes your spell to "sometimes" not work. TimerUtils recycles its timer, ReleaseTimer will simply pause the timer and put it back into the available timers queue. However, since you destroyed the timer, it isn't allocating a timer as you destroyed it. The reason it works "sometimes" is because it considers the 'destroyed timer' in use, and allocates the next available timer (which is working). Eventually you'll run out of timers though, as you can see if you set QUANTITY in TimerUtils to a lower value.​


TL;DR: Don't call [LJASS]DestroyTimer[/LJASS] on TimerUtil timers.

P.S.: If you enable Debug Mode, you'll see you are not using GroupUtils groups properly either.
 

Carnerox

The one and only.
Reaction score
84
Did you even read my post? : / the circle's thing was just a suggestion... plus the code i posted should fix you spell entirely, but it uses T32 instead
I did read it, and I was replying to the suggestion.

BlackRose;13_____ said:
If he uses your code, what will he learn? However, you pointed out the reason anyways:
"You're using TimerUtils and you're calling DestroyTimer(t) and (he does both) [del]instead of[/del] call ReleaseTimer(t)"

What happens when you cast the spell:
A timer in the TimerUtils queue is allocated and run, when the glaive hits the maximum spins, the timer is released AND destroyed. This causes your spell to "sometimes" not work. TimerUtils recycles its timer, ReleaseTimer will simply pause the timer and put it back into the available timers queue. However, since you destroyed the timer, it isn't allocating a timer as you destroyed it. The reason it works "sometimes" is because it considers the 'destroyed timer' in use, and allocates the next available timer (which is working). Eventually you'll run out of timers though, as you can see if you set QUANTITY in TimerUtils to a lower value.​


TL;DR: Don't call [LJASS]DestroyTimer[/LJASS] on TimerUtil timers.

P.S.: If you enable Debug Mode, you'll see you are not using GroupUtils groups properly either.
I'm guessing it has to do the same with DestroyGroup, just like the DestroyTimer problem? Or is it something else?

Anyway thank you both, the spell works correctly now that DestroyTimer is gone.
+rep
 

Laiev

Hey Listen!!
Reaction score
188
Yes, both TimerUtils and GroupUtils can't be destroyed, just released (and don't need to be paused before released) :)
 
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