• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] What is wrong with this piece of code?

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi all. The following code has this problem: when I use both unit A and unit B to cast it, instead of displaying both A's and B's names, only the unit's name of the first caster will be displayed. For example, instead of displaying"A,B,A,B,A,B....", it displays "A,A,A,A,A,A,A,....".(The second timer does fire, but with the name "A", not "B"). It seems "this.caster" does not catch the second caster...Why is it? Furthermore, if I use "this.deallocate" in the "callback" method, then only the unit's name of the second caster will be displayed. ... Thanks.

JASS:
struct test

    private unit caster
    private timer t
    
    private thistype next
    private thistype prev

        private static method callback takes nothing returns nothing
            local thistype this = thistype(0).next

            call BJDebugMsg(GetUnitName(this.caster))
            //call this.deallocate()
            set this = this.next
        endmethod
        
        private static method run takes nothing returns boolean
            local thistype this = thistype.allocate()
            
            set this.next = 0
            set this.prev = thistype(0).prev
            set thistype(0).prev.next = this
            set thistype(0).prev = this
            
            set this.caster=GetTriggerUnit()
            set this.t=CreateTimer() 
                
            call TimerStart(this.t,1.00,true, function thistype.callback)
            return false
        endmethod

        private static method onCast takes nothing returns boolean
            if GetSpellAbilityId() == 'A003' then
                call BJDebugMsg("oncast")
                call thistype.run()
            endif
                
            return false
        endmethod
     
        private static method onInit takes nothing returns nothing
            local trigger t
            set t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Condition(function thistype.onCast))
            set t = null     
        endmethod
endstruct
 
Don't forget to recycle it with ReleaseTimer.

JASS:
struct test

    private unit caster
    private timer t
    
    private thistype next
    private thistype prev

        private static method callback takes nothing returns nothing
            local thistype this = GetTimerData(GetExpiredTimer())

            call BJDebugMsg(GetUnitName(this.caster))
            //call this.deallocate()
            set this = this.next
        endmethod
        
        private static method run takes nothing returns boolean
            local thistype this = thistype.allocate()
            
            set this.next = 0
            set this.prev = thistype(0).prev
            set thistype(0).prev.next = this
            set thistype(0).prev = this
            
            set this.caster=GetTriggerUnit()
            set this.t=NewTimerEx(this) 
                
            call TimerStart(this.t,1.00,true, function thistype.callback)
            return false
        endmethod

        private static method onCast takes nothing returns boolean
            if GetSpellAbilityId() == 'A003' then
                call BJDebugMsg("oncast")
                call thistype.run()
            endif
                
            return false
        endmethod
     
        private static method onInit takes nothing returns nothing
            local trigger t
            set t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Condition(function thistype.onCast))
            set t = null     
        endmethod
endstruct
 
Level 11
Joined
Oct 11, 2012
Messages
711
Don't forget to recycle it with ReleaseTimer.

JASS:
struct test

    private unit caster
    private timer t
    
    private thistype next
    private thistype prev

        private static method callback takes nothing returns nothing
            local thistype this = GetTimerData(GetExpiredTimer())

            call BJDebugMsg(GetUnitName(this.caster))
            //call this.deallocate()
            set this = this.next
        endmethod
        
        private static method run takes nothing returns boolean
            local thistype this = thistype.allocate()
            
            set this.next = 0
            set this.prev = thistype(0).prev
            set thistype(0).prev.next = this
            set thistype(0).prev = this
            
            set this.caster=GetTriggerUnit()
            set this.t=NewTimerEx(this) 
                
            call TimerStart(this.t,1.00,true, function thistype.callback)
            return false
        endmethod

        private static method onCast takes nothing returns boolean
            if GetSpellAbilityId() == 'A003' then
                call BJDebugMsg("oncast")
                call thistype.run()
            endif
                
            return false
        endmethod
     
        private static method onInit takes nothing returns nothing
            local trigger t
            set t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Condition(function thistype.onCast))
            set t = null     
        endmethod
endstruct

It works!! Thank you TriggerHappy. I had worked on this issue for two hours and couldn't figured it out by myself. +Rep if I can.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
JASS:
            local trigger t
            set t = CreateTrigger()
---> local trigger t = CreateTrigger()

Note that's not a mistake, it's just shorter and looks better :)

And why do you insist in using a linked list here?

This is how I code create and destroy in a struct which extends array, if I'm not using Alloc.
JASS:
struct MyStruct extends array
    private static integer array rn
    private static integer ic = 0
        
        static method create takes nothing returns thistype
            local thistype this = rn[0]
            if this == 0 then
                set ic = ic + 1
                set this = ic
            else
                set rn[0] = rn[this]
            endif
            
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            set rn[this] = rn[0]
            set rn[0] = this
        endmethod
        
        //private static method run takes nothing returns nothing
            //local thistype this = create()
            //set this.caster = GetTriggerUnit()
            //call TimerStart(NewTImerEx(this), 0.03125, true, function thistype.periodic)
        //endmethod
endstruct
 
Level 11
Joined
Oct 11, 2012
Messages
711
what is the point of set this = this.next in callback method?

JASS:
            local trigger t
            set t = CreateTrigger()
---> local trigger t = CreateTrigger()

Note that's not a mistake, it's just shorter and looks better :)

And why do you insist in using a linked list here?

This is how I code create and destroy in a struct which extends array, if I'm not using Alloc.
JASS:
struct MyStruct extends array
    private static integer array rn
    private static integer ic = 0
        
        static method create takes nothing returns thistype
            local thistype this = rn[0]
            if this == 0 then
                set ic = ic + 1
                set this = ic
            else
                set rn[0] = rn[this]
            endif
            
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            set rn[this] = rn[0]
            set rn[0] = this
        endmethod
        
        //private static method run takes nothing returns nothing
            //local thistype this = create()
            //set this.caster = GetTriggerUnit()
            //call TimerStart(NewTImerEx(this), 0.03125, true, function thistype.periodic)
        //endmethod
endstruct

I am just using the spell model made by Magtheridon96.
http://www.hiveworkshop.com/forums/...80/efficient-clean-vjass-spell-models-216166/

I am a beginner in vJass, so I suppose using these models keeps me away from common mistakes. :)
 
Status
Not open for further replies.
Top