• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Can't get struck's data?

Status
Not open for further replies.
I'm learning struct and this is my first attempt of using it.
JASS:
library SlideUnit
    struct SlideUnit
        readonly            unit        slidedunit
        readonly            real        speed
        readonly            real        targetx
        readonly            real        targety
        
        private             timer       ti       = CreateTimer()

        private static method onLoop takes nothing returns nothing
            local thistype this
            call BJDebugMsg(I2S(this))
            call SetUnitPositionLoc( this.slidedunit, PolarProjectionBJ(GetUnitLoc(this.slidedunit), this.speed, GetUnitFacing(this.slidedunit)) )
            call KillUnit(this.slidedunit)
            
        endmethod
        
        static method create takes unit u, location loc, real speed returns thistype
            local thistype this = allocate()
            set this.slidedunit = u
            set this.speed      = speed
            set this.targetx    = GetUnitX(u)
            set this.targety    = GetUnitY(u)
            call TimerStart( ti, 0.03, true, function thistype.onLoop )
            set ti = null
            return this
        endmethod
    endstruct
endlibrary
In method "onLoop", the debug msg never displays anything. Why can't I get this.slidedunit nor the value of this.? What thing is wrong in the code?
 
It isn't displaying anything because the thread is crashing.
local thistype this is declared but you're giving it no value.

What you want to do is attach data to the timer that you're running so that you can know the instance this timer corresponds to.
Currently, in your code, there's no way for a timer to tell what instance it corresponds to, so you can't give this any value.

However, you shouldn't be doing that, because that would lead you to use multiple timers.

The solution is to use a system like CTL.

This system will manage 0.03125 second loops for you. You won't have to worry about the timer, the instances or anything.

Also, you may want to use SpellEffectEvent.

Example spell:

JASS:
library Spell requires CTL, SpellEffectEvent

    globals
        private constant integer ABIL_CODE = 'A000'
    endglobals
    
    private function CalculateDamage takes integer level returns real
        return (level + 1) * 60
    endfunction
    
    private struct Spell extends array
        private unit caster
        private unit target
        private real damage
        
        implement CTL
            // local variables here
            local unit u
        implement CTLExpire
            // loop code here using this
            call some_function(caster, target)
            // bleh, put whatever you want here

            if (end_spell) then
                call this.destroy()
            endif
        implement CTLNull
            // null the local variables you created if they are handles
            set u = null
        implement CTLEnd
        
        private static method run takes nothing returns nothing
            local thistype this = create()

            set this.caster = GetTriggerUnit()
            set this.target = GetSpellEventTarget()

        endmethod
        
        private static method onInit takes nothing returns nothing
            call RegisterSpellEffectEvent(ABIL_CODE, function thistype.run)
        endmethod
    endstruct
endlibrary
 
Status
Not open for further replies.
Top