WC3Jass.com Forum Index WC3Jass.com
"The Jass Vault"
 
 FAQ   Search   Memberlist   Usergroups   Register 
 Profile   Log in to check your private messages   Log in  
 Forum   Scripts   Files   Chat   Pastebin   Function finder  
Affiliates
The HIVE Workshop Games Modding
The Hubb Wc3Campaigns
WC3-Mapping.net

Using Timers and Handle Vars

 
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials
View previous script :: View next script  

Author
KaTTaNa
Site Admin

Joined: 04 Apr 2005
Posts: 655
Back to top
Message
PostPosted: Tue Apr 19, 2005 11:08 am    Post subject: Using Timers and Handle Vars Reply with quote

Timers are much more accurate than TriggerSleepAction and PolledWait, and are easy to use once you get familiar with them.

Update: You need the Local Handle Variables in your map header to use these examples.

Using a timer
Here is a template for making a timer loop through the same actions X times.
If it only needs to execute once (like a delayed call), you can remove the steps part and destroy the timer at the end of the Update function.
Code:

function UPDATE_FUNC takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer steps = GetHandleInt(t, "steps")

    // Get parameters again using GetHandleHandle(t, "...")

    // Do loop actions....

    if ( steps <= 1 ) then // Check if loop is over
        call FlushHandleLocals(t)
        call DestroyTimer(t)
        return
    endif
    call SetHandleInt(t, "steps", steps-1)
endfunction

function START_FUNC takes ...variables..., real duration returns nothing
    local timer t = CreateTimer()
    local real update = 0.10 // Update interval

    // Store parameters using SetHandleHandle(t, "...", ...)

    call SetHandleInt(t, "steps", R2I(duration/update))
    call TimerStart(t, update, true, function UPDATE_FUNC)
endfunction



Learn by example
Say we want to make a function that will simulate a poison effect, dealing damage over time to a given unit.
Code:
function PoisonUnit_Update takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local real damage = GetHandleReal(t, "dmg")
    local unit victim = GetHandleUnit(t, "victim")
    local unit source = GetHandleUnit(t, "source")
    local integer steps = GetHandleInt(t, "steps")
    call UnitDamageTarget(source, victim, damage, false, true, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_POISON, WEAPON_TYPE_WHOKNOWS)
    if ( steps <= 1 ) then
        call FlushHandleLocals(t)
        call DestroyTimer(t)
        return
    endif
    call SetHandleInt(t, "steps", steps-1)
endfunction

function PoisonUnit takes unit source, unit victim, real dmgPerSec, real duration returns nothing
    local timer t = CreateTimer()
    local real update = 0.50 // Damage interval
    call SetHandleReal(t, "dmg", dmgPerSec*update)
    call SetHandleInt(t, "steps", R2I(duration/update))
    call SetHandleHandle(t, "victim", victim)
    call SetHandleHandle(t, "source", source)
    call TimerStart(t, update, true, function PoisonUnit_Update)
endfunction


Other examples
Timers are useful for a bunch of other stuff. Here are some examples you can look at:


Last edited by KaTTaNa on Wed Oct 19, 2005 8:55 pm; edited 2 times in total
View user's profile Send private message Send e-mail Visit poster's website

Author
Guest




Back to top
Message
PostPosted: Sat Apr 30, 2005 10:21 am    Post subject: Reply with quote

At last someone sorted it out. Great job!

Author
Guest 007
Guest



Back to top
Message
PostPosted: Thu Aug 25, 2005 9:43 pm    Post subject: Reply with quote

If the StartTimer is to call a function which takes something, what will be the syntax?

Author
Heptameron


Joined: 13 Aug 2005
Posts: 15
Back to top
Message
PostPosted: Thu Aug 25, 2005 10:08 pm    Post subject: Reply with quote

Guest 007 wrote:
If the StartTimer is to call a function which takes something, what will be the syntax?

TimerStart can't call a function which takes parameters. That's what this tutorial is about; a way to fake that.
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number

Author
Grurf


Joined: 19 Oct 2005
Posts: 1
Back to top
Message
PostPosted: Wed Oct 19, 2005 3:42 pm    Post subject: Reply with quote

"function UPDATE_FUNC takes nothing returns nothing"

"call TimerStart(t, update, true, function PoisonUnit_Update)"

Don't you mean UPDATE_FUNC?
_________________
Proud creator of TIHD.

Also known as The_Idea.
View user's profile Send private message Visit poster's website

Author
KaTTaNa
Site Admin

Joined: 04 Apr 2005
Posts: 655
Back to top
Message
PostPosted: Wed Oct 19, 2005 8:55 pm    Post subject: Reply with quote

Yes, thanks for pointing that out.
View user's profile Send private message Send e-mail Visit poster's website

Author
GX-43AH


Joined: 21 Nov 2006
Posts: 8
Back to top
Message
PostPosted: Wed Nov 22, 2006 10:07 am    Post subject: Reply with quote

I always think that using return bug and gamecache is a better way.
GetHandle functions is a little difficult.
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger

Author
Blade.dk


Joined: 20 Apr 2005
Posts: 608
Back to top
Message
PostPosted: Sun Nov 26, 2006 8:42 pm    Post subject: Reply with quote

The whole point of handle functions is making it simpler for the user, and I think most people agree that it is simpler than doing the thing directly. But of course, it's up to you what you prefer doing.
_________________
#wc3dev
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Author
GX-43AH


Joined: 21 Nov 2006
Posts: 8
Back to top
Message
PostPosted: Tue Nov 28, 2006 9:43 am    Post subject: Reply with quote

Simpler?

//=========Original AddTimesLightningSystem=============
Code:
function SetHandleHandle takes handle subject, string name, handle value returns nothing
    if value==null then
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, H2I(value))
    endifendfunction


function SetHandleReal takes handle subject, string name, real value returns nothing
       if value==0 then
        call FlushStoredReal(LocalVars(), I2S(H2I(subject)), name)
    else
        call StoreReal(LocalVars(), I2S(H2I(subject)), name, value)
    endif
endfunction


function AddTimedLightning_Fade takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local lightning li = GetHandleLightning(t, "lightning")
    local real time = GetHandleReal(t, "time")
    local real d = 0.1/time
    local real r = GetLightningColorR(li)
    local real g = GetLightningColorG(li)
    local real b = GetLightningColorB(li)
    local real a = GetLightningColorA(li)-d
    call SetLightningColor(li, r, g, b, a)
    if a <= 0 then
        call DestroyLightning(li)
        call FlushHandleLocals(t)
        call DestroyTimer(t)
    endif
    set t = null
    set li = null
endfunction


function AddTimedLightning_StartFade takes nothing returns nothing
    call TimerStart(GetExpiredTimer(), 0.1, true, function AddTimedLightning_Fade)
endfunction


function AddTimedLightning takes string Code, real x1, real y1, real x2, real y2, real duration, real fadetime returns lightning
    local timer t = CreateTimer()
    local lightning li = AddLightning(Code, true, x1, y1, x2, y2)
    call SetHandleHandle(t, "lightning", li)
    call SetHandleReal(t, "time", fadetime)
    call TimerStart(t, duration, false, function AddTimedLightning_StartFade)
    set t = null
    return li
endfunction


function AddTimedLightningEx takes string Code, real x1, real y1, real z1, real x2, real y2, real z2, real duration, real fadetime returns lightning
    local timer t = CreateTimer()
    local lightning li = AddLightningEx(Code, true, x1, y1, z1, x2, y2, z2)
    call SetHandleHandle(t, "lightning", li)
    call SetHandleReal(t, "time", fadetime)
    call TimerStart(t, duration, false, function AddTimedLightning_StartFade)
    set t = null
    return li
endfunction




//=============================================

The above code is the code of the "AddTimedLightning" system which use the SetHandle functions.Of couse ,if we don't consider about leak and fade in/fade out
the code would not be so involute.Then see the following code:

//==do not consider about leak and fade in/fade out either,need a global variable gamecache udg_GC=======
//=========AddTimesLightningSystem2======================
Code:
function h2i takes handle h returns integer
    return h
    return 0
endfunction


function i2l takes integer i returns lightning
    return i
    return null
endfunction


function DL takes nothing returns nothing
    call DestroyLightning(i2l(GetStoredInteger(udg_GC,I2S(h2i(GetExpiredTimer())),"li")
endfunction

function AddTimedLightning takes string Code,boolean b real x1, real y1, real z1, real x2, real y2, real z2, real time returns nothing
    local timer tm = CreateTimer()
    call StoreInteger(udg_GC,I2S(h2i(tm)),"li",h2i(AddLightningEx(Code, b, x1, y1, z1, x2, y2, z2)))
    call TimerStart(tm,time,false,function DL)
endfunction



//=============================================

Obviously,the system 2 is simpler than the original system,because the SetHandle functions themselves are too involute.
Just as we all know, the gamecache and return bug are use widely.
Anyway,thanks for teaching me using the SetHandle functions.
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger

Author
KaTTaNa
Site Admin

Joined: 04 Apr 2005
Posts: 655
Back to top
Message
PostPosted: Thu Nov 30, 2006 5:36 pm    Post subject: Reply with quote

That is a horrible example GX. System 2 leaks AND it does not fade the lightning AND it is harder to read.
The vast majority of the code in system 1 is for fading the lightning. You are entitled to not your own opinion about the handle vars system, but do not make overly-biased examples like that.
View user's profile Send private message Send e-mail Visit poster's website

Author
GX-43AH


Joined: 21 Nov 2006
Posts: 8
Back to top
Message
PostPosted: Sat Dec 02, 2006 8:00 am    Post subject: Reply with quote

Horrible example? Yes,it is. ^_^
But I have already told you that system 2 doesn't consider leak&fade in/fade out,didn't I ?
Sorry I didn't explain my thoughts clearly.(my English is not very good)
In fact , if system 1 doesn't consider fade in/fade out,it will be the following code:

Code:
function H2I taks handle h returns integer
    return h
    return 0
endfunction

function SetHandleHandle takes handle subject, string name, handle value returns nothing
    if value==null then
        call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
    else
        call StoreInteger(LocalVars(), I2S(H2I(subject)), name, H2I(value))
    endif
endfunction

function DestroyLightning takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local lightning li = GetHandleLightning(t, "lightning")
    call DestroyLightning(li)
    call FlushHandleLocals(t)
    call DestroyTimer(t)
    set t = null
    set li = null
endfunction

function AddTimedLightningEx takes string Code, real x1, real y1, real z1, real x2, real y2, real z2, real duration returns lightning
    local timer t = CreateTimer()
    local lightning li = AddLightningEx(Code, true, x1, y1, z1, x2, y2, z2)
    call SetHandleHandle(t, "lightning", li)
    call TimerStart(t, duration, false, function DestroyLightning)
    set t = null
    return li
endfunction


If system 2 consider leak,it will be the following code:

Code:
function h2i takes handle h returns integer
    return h
    return 0
endfunction

function i2l takes integer i returns lightning
    return i
    return null
endfunction

function DL takes nothing returns nothing
    call DestroyLightning(i2l(GetStoredInteger(udg_GC,I2S(h2i(GetExpiredTimer())),"li")
    call FlushStoredInteger(udg_GC,I2S(H2I(GetExpiredTimer())),"li")
    call DestroyTimer(GetExpiredTimer())
endfunction

function AddTimedLightning takes string Code,boolean b real x1, real y1, real z1, real x2, real y2, real z2, real time returns nothing
    local timer tm = CreateTimer()
    call StoreInteger(udg_GC,I2S(h2i(tm)),"li",h2i(AddLightningEx(Code, b, x1, y1, z1, x2, y2, z2)))
    call TimerStart(tm,time,false,function DL)
    set tm = null
endfunction


You see , they are almost the same. But the system 2 store the data directly,
and it needs less locals,"call" less and "takes" less. So system 2 is easier to use and remember.
I'm not criticizing your course.In fact your course is very useful.
I'm sorry that I didn't explain my thoughts clearly but I never deal with the Set Handle functions biased ,you just misunderstood me.
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger

Display posts from previous:   
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


FSDark by SkaidonDesigns
Powered by phpBB © 2001, 2002 phpBB Group