• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[vJass] TimedEffects

Level 18
Joined
Jan 21, 2006
Messages
2,552
JASS:
library TimedEffects
private function h2i takes handle h returns integer
    return h
    return 0
endfunction

struct timedeffect
    private effect  m_fx    = null
    private timer   m_tmr   = CreateTimer()
    
    private static timedeffect array data
    
    method onDestroy takes nothing returns nothing
        if .m_fx!=null then
            call DestroyEffect(.m_fx)
        endif
        call PauseTimer(.m_tmr)
        call DestroyTimer(.m_tmr)
    endmethod
    
    static method onExpire takes nothing returns nothing
        call timedeffect.data[h2i(GetExpiredTimer())-0x100008].destroy()
    endmethod
    static method create takes effect e, real duration returns timedeffect
        local timedeffect dat=timedeffect.allocate()
        set dat.m_fx=e
        
        set timedeffect.data[h2i(dat.m_tmr)-0x100008]=dat
        call TimerStart(dat.m_tmr, duration, false, function timedeffect.onExpire)
        return dat
    endmethod
endstruct
endlibrary

It is pretty self-explanatory.
 
Level 3
Joined
Apr 17, 2008
Messages
25
Use TimerUtils so you can get Timer Recycling and when you are doing that also switch the attachment stuff to the TU attachment so the user can choose which sort of attachment he wants.
 
Level 15
Joined
Feb 15, 2006
Messages
851
my point is that your script is almost the same as mine, with the difference that you use Destroytimer() which is deprecated due handle stack corruption.

This is the current code at WC3C:

JASS:
//***********************
//*    Timed Effects    *
//*   by moyack. 2009   *
//***********************
//*
//* Requires Jass NewGen Pack and TimerUtils
//*    
//* Introduction
//* ============
//*
//* So... have you been needing a simple way (or function) to create an effect temporally
//* and don't worry about destroying it? or you're needing that an effect with different 
//* animations (birth, stand, death) will show all of them?? if your answer is yes to any
//* of those questions, then you're in the right place.
//*
//* This script is used in my project Power of Corruption ([url]www.pocr.tk)[/url], as you'll see, 
//* it's simple as hell.
//*
//* Just to point out: this library is very useful with effects that have different 
//* animations, so using this library with single animation effect will do the same as 
//* call DestroyEffect(AddSpecialEffect(....))
//*
//* How to use it?
//* ==============
//* - Create a trigger with a convenient name (like Timed Effects :P)
//* - Convert the trigger to custom text
//* - Copy and paste this code and save your map.
//*
//* With all this done, you just have to call the function StartTimedEffect(), and as parameters
//* an effect and a real value which will be the effect duration.
//*
//*   Example: call StartTimedEffect(AddSpecialEffect(fx, 0., 0.), 2.)
//*
//* This function will return an integer which will represent the timed effect index, this value
//* can be used when, for example, you need to end the effect before the duration time. 
//*
//* To stop it, you just call the function StopTimedEffect():
//*
//*   Example: call StopTimedEffect(1) // will stop the timed effect with that index.
//*
//* You won't need anything else, the system will care of cleaning the effect properly 
//* and recycle the values for you.

library TimedEffects requires TimerUtils

private struct data
    effect f = null
    timer t
    static method create takes effect f returns data
        local data dt = data.allocate()
        set dt.t = NewTimer()
        set dt.f = f
        call SetTimerData(dt.t, integer(dt))
        return dt
    endmethod
endstruct

function StopTimedEffect takes integer index returns nothing
    call DestroyEffect(data(index).f)
    call ReleaseTimer(data(index).t)
    call data(index).destroy()
endfunction

private function DestroyTimedEffect takes nothing returns nothing
    call StopTimedEffect(GetTimerData(GetExpiredTimer()))
endfunction

function StartTimedEffect takes effect f, real dur returns integer
    local data d = data.create(f)
    call TimerStart(d.t, dur, false, function DestroyTimedEffect)
    return integer(d)
endfunction

endlibrary
 
Top