Thread: Timed Effects
View Single Post
Old 01-21-2009, 07:47 PM   #1
moyack
Evil Emoticon
 
moyack's Avatar


Respected User
Project Leader: PoC
 
Join Date: Jan 2006
Posts: 3,279

Submissions (17)

moyack is a splendid one to behold (666)moyack is a splendid one to behold (666)moyack is a splendid one to behold (666)moyack is a splendid one to behold (666)

AI Tournament #2 - 2nd PlaceHero Contest - Second place

Send a message via MSN to moyack
Default Timed Effects

Timed Effects
by moyack. 2009

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, 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?

Just add an effect as parameter and then you set its duration.

Example: call StartTimedEffect(AddSpecialEffect(fx, 0., 0.), 2.)

You won't need anything else, the system will care of cleaning the effect properly and recycle the values for you.

The code.

This library requires TimerUtils
Collapse TimedEffects - TimerUtils version:
//***********************
//*    Timed Effects    *
//*   by moyack. 2011   *
//***********************
//*
//* 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 (http://poc.it.cx), 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.)
//*
//* You won't need anything else, the system will care of cleaning the effect properly 
//* and recycle the values for you.

library_once 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

private function DestroyTimedEffect takes nothing returns nothing
    local data d = data(GetTimerData(GetExpiredTimer()))
    call DestroyEffect(d.f)
    call ReleaseTimer(d.t)
    call d.destroy()
endfunction

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

endlibrary

Collapse TimedLoop version:
//***********************
//*    Timed Effects    *
//*   by moyack. 2011   *
//***********************
//*
//* Requires Jass NewGen Pack and TimedLoop by Vexorian
//*    
//* 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 (http://poc.it.cx), 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.)
//*
//* You won't need anything else, the system will care of cleaning the effect properly 
//* and recycle the values for you.

library_once TimedEffects requires TimedLoop

private struct data
    effect f = null
    real time = 0.
    real dur 
    
    private method onTimedLoop takes nothing returns boolean
        set .time = .time + TimedLoop_PERIOD
        if .time > .dur then
            call DestroyEffect(.f)
            return TimedLoop_STOP
        endif
        return TimedLoop_CONTINUE
    endmethod
    
    implement TimedLoop
    
    static method create takes effect f, real t returns thistype
        local thistype dt = thistype.allocate()
        set dt.dur = t
        set dt.f = f
        call dt.startTimedLoop()
        return dt
    endmethod
endstruct

function StartTimedEffect takes effect f, real dur returns nothing
    local data d = data.create(f,dur)
endfunction

endlibrary

.:
I did by request an addition to the code: a library which will allow to the user to set a delayed effect. In other words: if you need that an effect takes X seconds to start and keeps visible by Y seconds, this extension allows you to do that in one single line.

Expand TimedEffects Extension (TEEX), adds delay settings. Uses TimerUtils:

Usage of TEEX:
Collapse JASS:
call StartSpecialEffectTimed("Some_File_Path.mdx", X, Y, DELAY, DURATION)
call StartSpecialEffectTargetTimed("Some_File_Path.mdx", SOMEWIDGET, "Attach_point", DELAY, DURATION)

Now enjoy your new code :) Comments and such are welcome and appreciated.

Changelog:

* 1/21/2009: First Release
* 3/08/2009: Now StartTimedEffect returns the index of the TimedEffect object and added the StopTimedEffect function so you can stop it manually.
* 3/20/2009: Simplified more to avoid the .execute call
* 6/10/2011: Added TimedLoop version
moyack is offline   Reply With Quote
Sponsored Links - Login to hide this ad!