Snippet TargetTimedEffects

wraithseeker

Tired.
Reaction score
122
JASS:
library TargetTimedEffects requires TimerUtils

private struct data
    effect EFFECT
    timer t

static method create takes nothing returns data
    local data d = data.allocate()
    set d.t = NewTimer()
    return d
endmethod

method onDestroy takes nothing returns nothing
    call DestroyEffect(this.EFFECT)
    call ReleaseTimer(.t)
    set .EFFECT = null
    set .t = null
endmethod
endstruct

private function DestroyEffects takes nothing returns nothing
    local data d = data(GetTimerData(GetExpiredTimer()))
    call d.destroy()
endfunction
    
function StartTimedEffects takes string EFFECT,unit target, string Attachpoint,real TIME returns nothing
    local data d = data.create()
    call SetTimerData(d.t,d)
    call TimerStart(d.t,TIME,false,function DestroyEffects)
    set d.EFFECT = AddSpecialEffectTarget(EFFECT,target,Attachpoint)
endfunction

endlibrary


I know many people have done this before but I needed one for attaching a effect to a target and haven't found one so I decided to put it up here as a snippet.

Can you all tell me how to improve on it more? I null the struct members for safety, used Tyrand3_Max handle counter and saw handles going by like 800 up when I do a loop of 800 and found out that after nulling, 400 handles go down.
 
Reaction score
91
> local data d
You're not getting anything here...

EDIT: Which means you just create structs that never get destroyed.
 

Flare

Stops copies me!
Reaction score
662
Code:
static method create takes [B]string EFFECT[/B] returns data
    local data d = data.allocate()
    set d.t = NewTimer()
    return d
endmethod
Now, why is that bolded part there? You're not making much use of it...

Code:
    local data d 
    call d.destroy()
As Tyrande_ma3x said, you're not going anywhere with that. Did you even test this before submitting? I can't see many special effects disappearing if you are trying to destroy them by calling .destroy for a non-existant struct instance :p

Also, adding in XY Timed Effects would be a nice idea. As it is, it's extremely limited since you can only use units. Addition of XY would make it a bit more useful since then it'd have pretty much everything and a loc function, although you could bundle everything into one function e.g.
JASS:
function Callback takes nothing returns nothing
  call Data(GetTimerData(GetExpiredTimer())).destroy ()
//or... - probably more advisable one, a bit more readable without all those brackets
  local Data d = GetTimerData (GetExpiredTimer ())
  call d.destroy ()
endfunction

//Order of priority is location -> unit -> coordinates, passing null to location or unit will skip to the next in the list
function TimedEffect takes location loc, unit u, real x, real y, string model, string attachpt, real dur returns nothing
local Data d = Data.create ()
if loc != null then
  set x = GetLocationX (l)
  set y = GetLocationY (l)
  set d.fx = AddSpecialEffectLoc (...)
elseif u != null then
  set d.fx = AddSpecialEffectTarget (...)
else
  set d.fx = AddSpecialEffect (...)
endif
set d.t = NewTimer ()
call SetTimerData (t, d)
call TimerStart (...)
endfunction

or make individual functions - it's a toss-up between being able to name the functions according to their 'parent' (e.g. TimedEffect -> AddSpecialEffect, TimedEffectLoc -> AddSpecialEffectLoc, and so on), or bundling everything into one function, so there's minimal things to remember other than loc, unit, coords :p
 

Azlier

Old World Ghost
Reaction score
461
>function StartTimedEffects takes string EFFECT,unit target, string Attachpoint,real TIME returns nothing

Your naming conventions are horrific! Seems to be totally random.

Have you tried, you know, seeing if this actually works?

JASS:
library TimedTargetEffect requires TimerUtils

struct TimedEffect
    
    timer Ti
    effect Fx
    
    static method finish takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local thistype d = GetTimerData(t)
        call DestroyEffect(d.Fx)
        call ReleaseTimer(d.Ti)
        set t = null
        set d.Ti = null
        set d.Fx = null
        call d.destroy()
    endmethod
    
    static method create takes unit target, string model, string attach, real duration returns thistype
        local thistype d = thistype.allocate()
        set d.Ti = NewTimer()
        set d.Fx = AddSpecialEffectTarget(model, target, attach)
        call SetTimerData(d.Ti, d)
        call TimerStart(d.Ti, duration, false, function thistype.finish)
        return d
    endmethod
endstruct

function AddTimedTargetEffect takes unit target, string model, string attach, real duration returns nothing
    call TimedEffect.create(target, model, attach, duration)
endfunction

endlibrary


You can shave off AddTimedTargetEffect if you want to use the create method directly. (Totally untested.)
 

Viikuna

No Marlo no game.
Reaction score
265
I hope you realize that it is useless to null timer that is recycled..

And make it take effect, so it works for both target and x&y effects.
 
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