Ừ...ý kiến trên ko có gì sai cả..nhưng..
Timer bây giờ ko ai dùng kiểu destroy cả mà New và Realease (hoạt động rất tốt)
Spell theo kiểu periodic hoặc wait kiểu này theo anh nên dùng timer..và hầu hết Jasser đều làm vậy..việc chứng minh thì dành cho các cao thủ ..cũng rất nhiều người đồng tình với việc này...
Có phải bác rex đang nói đến new và realease của Red TimerUtils của Vex ???
Mã:
library_once TimerUtils initializer redInit
//*********************************************************************
//* TimerUtils (Red flavor)
//* ----------
//*
//* To implement it , create a custom text trigger called TimerUtils
//* and paste the contents of this script there.
//*
//* To copy from a map to another, copy the trigger holding this
//* library to your map.
//*
//* (requires vJass) More scripts: htt://www.wc3campaigns.net
//*
//* For your timer needs:
//* * Attaching
//* * Recycling (with double-free protection)
//*
//* set t=NewTimer() : Get a timer (alternative to CreateTimer)
//* ReleaseTimer(t) : Relese a timer (alt to DestroyTimer)
//* SetTimerData(t,2) : Attach value 2 to timer
//* GetTimerData(t) : Get the timer's value.
//* You can assume a timer's value is 0
//* after NewTimer.
//*
//* Red flavor: Fastest, method in existence for timer attaching,
//* only takes an array lookup, H2I and subtraction.
//* However, all the code in your map requires extra care
//* not to forget to call ReleaseTimer. It also requires
//* to preload a lot of timers at map init, they use
//* memory and handle ids.
//*
//********************************************************************
//================================================================
globals
private constant integer QUANTITY = 256
private constant integer ARRAY_SIZE = 8191 //changing this to a higher value would effectively
//cripple the performance making this thing irrelevant
endglobals
//=================================================================================================
private function H2I takes handle h returns integer
return h
return 0
endfunction
//==================================================================================================
globals
private integer array data[ARRAY_SIZE]
private integer OFFSET
endglobals
//It is dependent on jasshelper's recent inlining optimization in order to perform correctly.
function SetTimerData takes timer t, integer value returns nothing
debug if(H2I(t)-OFFSET<0) then
debug call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
debug endif
set data[H2I(t)-OFFSET]=value
endfunction
function GetTimerData takes timer t returns integer
debug if(H2I(t)-OFFSET<0) then
debug call BJDebugMsg("GetTimerData: Wrong handle id, only use GetTimerData on timers created by NewTimer")
debug endif
return data[H2I(t)-OFFSET]
endfunction
//==========================================================================================
globals
private timer array tT
private integer tN = 0
private constant integer HELD=0x28829022
//use a totally random number here, the more improbable someone uses it, the better.
endglobals
//==========================================================================================
function NewTimer takes nothing returns timer
if (tN==0) then
//If this happens then the QUANTITY rule has already been broken, try to fix the
// issue, else fail.
debug call BJDebugMsg("NewTimer: Warning, Exceeding TimerUtils_QUANTITY, please increase it for your map, fix your map's timer leaks or switch to blue flavor when applicable")
set tT[0]=CreateTimer()
if (H2I(tT[0])-OFFSET<0) or (H2I(tT[0])-OFFSET>=ARRAY_SIZE) then
//all right, couldn't fix it
call BJDebugMsg("NewTimer: Unable to allocate a timer, you should probably switch to the blue flavor or fix timer leaks.")
return null
endif
else
set tN=tN-1
endif
call SetTimerData(tT[tN],0)
return tT[tN]
endfunction
//==========================================================================================
function ReleaseTimer takes timer t returns nothing
if(t==null) then
debug call BJDebugMsg("Warning: attempt to release a null timer")
return
endif
if (tN==8191) then
debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")
//stack is full, the map already has much more troubles than the chance of bug
call DestroyTimer(t)
else
call PauseTimer(t)
if(GetTimerData(t)==HELD) then
debug call BJDebugMsg("Warning: ReleaseTimer: Double free!")
return
endif
call SetTimerData(t,HELD)
set tT[tN]=t
set tN=tN+1
endif
endfunction
private function redInit takes nothing returns nothing
local integer i
local integer last
local boolean correct
loop
set tT[0] = CreateTimer()
set OFFSET=H2I(tT[0] )
set i=1
set correct=true
loop
exitwhen (i==QUANTITY)
set tT[i] = CreateTimer()
if(H2I(tT[i])-OFFSET<0) or (H2I(tT[i])-OFFSET>=ARRAY_SIZE) then
debug call BJDebugMsg("TimerUtils_redInit: Failed a initializing attempt")
set correct=false
exitwhen true
endif
set i=i+1
endloop
exitwhen correct
endloop
set tN=QUANTITY
endfunction
endlibrary
Nếu đúng bác dùng cái này thì
.... thực chất nó cũng là create timer và ... pausetimer mà!!!! ... Vex đâu phải god mà có thể tham gia chèn code vào war3 đc .... nên cuối cùng vẫn phải createtimer mà anh.
Kể cả về say người ta dùng ... XoaDongHo và TaoDongHo do Rex làm thì nó vẫn phải base trên cái đó ....
tất nhiên với BlueTimer thì nó sử dụng luôn cái CSData attach nên em ko bàn nhiều mà ở đây bàn đến RedTimer thôi
cũng mới đọc qua cái này thấy cũng hay, nhưng 1 số chỗ em chỉ nghi nó index các timer bằng số ngẫu nhiên nên chắc chắn sẽ có tầm vài phần nghìn % để có bug, tất nhiên em ko phải chuyên tin nên để hỏi lại xem đã ...
CÒn về nguyên tắc của RedTimer như bác Rex đã dùng
Đầu tiên
* function redInit takes nothing returns nothing
tạo ra 256 timer (exitwhen (i==QUANTITY)) QUANTITY = 256 ở trên cùng và có thể thay đổi
sau đó index mỗi cái theo i bắt đầu từ 1 - QUANTITY
Cuối cùng set tN=QUANTITY = 256 ....... hay
* function NewTimer takes nothing returns timer
Nếu Jasser dùng newtimer, set tN=tN-1, tức là bộ đếm lùi cái 256 timer kia
* function ReleaseTimer takes timer t returns nothing
Khi relealse timer thì thông báo cho SYStem này biết để pausetimer và - index set tN=tN+1, coi như timer này free to use
Điểm mạnh ở đây là ko sử dụng DestroyTimer nên tất nhiên tốc độ phải nói là rất nhanh, và tài nguyên bộ nhớ có thể kiểm xoát đc ... tóm lại có nhiều ưu điềm hơn rất nhiều so với cách làm thông thường ... cái này thì em có lẽ đồng ý ....
Như vậy cái RedTimer này là công cụ quản lý timer giúp jasser đỡ lúng túng để quản lý hộ các timer do họ tạo ra thôi, NÓ VẪN PHẢI BASE TRÊN VIỆC TẠO RA TIMER = CREATETIMER
Cách index này hồi trước cũng thấy bác tom làm trên GUI để move mấy cái knockback hay gì gì đó đấy ... mà dùng cách này nghĩ lại thấy đc đấy nhỉ, đỡ phải lo nghĩ đến tràn bộ nhớ