 |
WC3Jass.com "The Jass Vault"
|
Affiliates
|
 |
|
 |
|
|
| Message |
Posted:
Tue Apr 19, 2005 11:08 am Post subject:
Using Timers and Handle Vars |
|
|
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 |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Sat Apr 30, 2005 10:21 am Post subject:
|
|
|
| At last someone sorted it out. Great job! |
|
|
|
 |
|
|
|
| Message |
Posted:
Thu Aug 25, 2005 9:43 pm Post subject:
|
|
|
| If the StartTimer is to call a function which takes something, what will be the syntax? |
|
|
|
 |
|
|
|
| Message |
Posted:
Thu Aug 25, 2005 10:08 pm Post subject:
|
|
|
| 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. |
|
|
|
 |
|
|
|
| Message |
Posted:
Wed Oct 19, 2005 3:42 pm Post subject:
|
|
|
"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. |
|
|
|
 |
|
|
|
| Message |
Posted:
Wed Oct 19, 2005 8:55 pm Post subject:
|
|
|
| Yes, thanks for pointing that out. |
|
|
|
 |
|
|
|
| Message |
Posted:
Wed Nov 22, 2006 10:07 am Post subject:
|
|
|
I always think that using return bug and gamecache is a better way.
GetHandle functions is a little difficult. |
|
|
|
 |
|
|
|
| Message |
Posted:
Sun Nov 26, 2006 8:42 pm Post subject:
|
|
|
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 |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Tue Nov 28, 2006 9:43 am Post subject:
|
|
|
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. |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Thu Nov 30, 2006 5:36 pm Post subject:
|
|
|
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. |
|
|
|
 |
|
 |
|
 |
 |
|
 |
|
|
| Message |
Posted:
Sat Dec 02, 2006 8:00 am Post subject:
|
|
|
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. |
|
|
|
 |
|
 |
|
 |
|
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
|
|