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:
 
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
 
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.
 
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.
 
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.
 
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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      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