Using a struct method with a timer

Embrace_It

New Member
Reaction score
9
Say I have a private struct with a non-static method. I also have an "Actions" function that creates an instance of this struct and a timer (using TimerUtils (blue)). I assumed that you can use the member function as the handlerFunc like this:

JASS:

call TimerStart(t, 0.75, true, function p.move)


I've searched the forums and this seems like a valid approach, however I keep getting this error message: "p is not an struct name".

Help! :confused:
 

Embrace_It

New Member
Reaction score
9
Yeah, I forgot about that :rolleyes:

This still needs some polish after I get it to work

JASS:

scope Tremor initializer Init
private keyword Pulse // Enables usage of variable/function before declaration

//  -------------
// | START SETUP |
//  -------------
globals
    private unit TempU
    private filterfunc fltr // Filters units that should not be damaged by the spell
    
    // Constants
    private constant integer ABILITY_CODE = 'A00I' // Ability ID
    private constant integer DUMMY_CODE   = 'h006' // Dummy unit ID
    //private constant integer DOODAD_VARIATION_1 = 'BRrp'
    //private constant integer DOODAD_VARIATION_2 = ''
    //private constant integer DOODAD_VARIATION_3 = 'n00'
    //private constant integer DOODAD_VARIATION_4 = 'n00'
    //private constant integer DOODAD_VARIATION_5 = 'n00'
    //private constant integer DOODAD_VARIATION_6 = 'n00'
    //private constant integer DOODAD_VARIATION_7 = 'n00'
    //private constant string CAST_EFFECT = "" // The effect on the caster
    private constant real FREQUENCY = 0.03 // Frequency of the timer
    
    // Pulse-specific constants
    private constant real PULSE_SPEED  = 60.  // The distance the spell moves every time
    private constant real PULSE_AOE    = 70. // The range of the spell where units take damage
    private constant real MAX_RANGE    = 650.
    private constant real MAX_RUNS     = 8.
    private constant real OFFSET       = 20.
    private constant real LOWER_BOUND  = 0.6
    private constant real UPPER_BOUND  = 2.
endglobals

private function DamagePerLevel takes unit source, integer lvl returns real
    local integer int
    
    if (lvl > 1) then
        set int = GetHeroInt(source, true)
        return (45. * lvl + 5.) + (0.1 * lvl + 0.1) * int
    else
        return 45. * lvl + 5.
    endif
endfunction

private function UnitFilter takes nothing returns boolean
    return IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(TempU)) and GetWidgetLife(GetFilterUnit()) > 0.405 and not IsUnitType(GetFilterUnit(), UNIT_TYPE_FLYING) and not IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE)
endfunction
//  -----------
// | END SETUP |
//  -----------

private struct Pulse
    unit source
    player owner
    group pulsegroup = CreateGroup() // Group to avoid double damage
    real dmg
    real velocity = PULSE_SPEED
    real d = 0. // distance
    real x
    real y
    real cos // cosinus
    real sin // sinus
    integer counts = MAX_RUNS
        
    static method create takes unit source, player owner, real x, real y, real angle returns Pulse
        local Pulse p = Pulse.allocate()
        
        set p.source = source
        set p.owner = owner
        call GroupAddUnit(p.pulsegroup, source) // To avoid the caster being hit
        set p.dmg = DamagePerLevel(source, GetUnitAbilityLevel(source, ABILITY_CODE))
        set p.x = x
        set p.y = y
        set p.cos = Cos(angle)
        set p.sin = Sin(angle)
        
        return p
    endmethod
    
    method move takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local Pulse p = Pulse(GetTimerData(t))
        local group g = CreateGroup()
        local unit f
        local unit u
        local real x
        local real y
        local real bounds = GetRandomReal(LOWER_BOUND, UPPER_BOUND)
        
        set TempU = p.source
        
        if (p.d <= MAX_RANGE and p.counts != 0) then
            set p.x = p.x + p.velocity * p.cos + GetRandomReal(-OFFSET, OFFSET)
            set p.y = p.y + p.velocity * p.sin + GetRandomReal(-OFFSET, OFFSET)
            set u = CreateUnit(p.owner, DUMMY_CODE, p.x, p.y, GetRandomReal(0., 359.))
            call SetUnitScale(u, bounds, bounds, bounds)
            call UnitApplyTimedLife(u, 'BTLF', 1.)
            call GroupEnumUnitsInRange(g, p.x, p.y, PULSE_AOE, fltr)
            
            loop
                set f = FirstOfGroup(g)
                exitwhen f == null
                set x = GetUnitX(f)
                set y = GetUnitY(f)
                if not IsUnitInGroup(f, p.pulsegroup) then
                    call UnitDamageTarget(p.source, f, p.dmg, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS)
                    call CreateFloatingReal(p.dmg, x, y, EARTH_MAGIC)
                    call GroupAddUnit(p.pulsegroup, f)
                endif
                call GroupRemoveUnit(g, f)
            endloop
            
            set p.d = p.d + PULSE_SPEED // Calculate distance
            set p.counts = p.counts - 1
        else
            call ReleaseTimer(t)
            call p.destroy() // Destroy struct  
        endif
        
        call GroupClear(g)
        call DestroyGroup(g)
        set g = null
        set f = null
    endmethod
    
    method onDestroy takes nothing returns nothing
        set .source = null
        call DestroyGroup(.pulsegroup)
        set .pulsegroup = null
        set .owner = null
    endmethod
endstruct

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == ABILITY_CODE
endfunction

private function Actions takes nothing returns nothing
    local Pulse p
    local timer t = NewTimer()
    local location loc = GetSpellTargetLoc()
    local unit source = GetTriggerUnit()
    local player owner = GetOwningPlayer(source)
    local real x = GetUnitX(source)
    local real y = GetUnitY(source)
    local real angle = Atan2(GetLocationY(loc) - y, GetLocationX(loc) - x)

    set p = Pulse.create(source, owner, x, y, angle)
    call SetTimerData(t, integer(p))
    call TimerStart(t, 0.75, true, function p.move)

    set source = null
    set owner  = null
    set t = null
endfunction

// Main function
private function Init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    
    call TriggerRegisterAnyUnitEventBJ(trig, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(trig, Condition(function Conditions))
    call TriggerAddAction(trig, function Actions)
        
    set fltr = Filter(function UnitFilter)
    
    set trig = null
endfunction

endscope
 

Embrace_It

New Member
Reaction score
9
I made a private function called Update that does exactly the same as the move method. Now it works...I would still like to solve the original prolem though.
 

saw792

Is known to say things. That is all.
Reaction score
280
Timer callbacks cannot take arguments. Non-static methods take arguments (even if they say they don't, when compiled they at least take 'integer this'), so cannot be used as timer callbacks.
 
Reaction score
341
JASS:
method example takes nothing returns nothing
     //...
endmethod


Compiles to something like;

JASS:
function structname__example takes integer this returns nothing
     //...
endmethod


callback functions can't have any parameters. Use a static method, or a function instead.
 

Embrace_It

New Member
Reaction score
9
I see! The spell has been fixed accordingly now. Thanks! +rep :thup:

EDIT: Must spread rep first
 
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