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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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