• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Jass timers

Status
Not open for further replies.
Level 29
Joined
Jul 29, 2007
Messages
5,174
Hey all Im starting to learn timers in jass im not sure how to start using them. I havnt used them before so i have no idea how to go about doing this

Anyhelp will be great

JASS:
function callback takes nothing returns nothing
    // do stuff
endfunction

function start takes ... returns ...
    local timer t = CreateTimer()
    TimerStart(t, real, boolean, function callback) // real is the time untill call back 
                                                    // and boolean is periodic or not 
                                                    //(true for periodic)
endfunction
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
basics:
A timer measures time, it is a countdown timer, starts from where you tell it and finishes at 0. When it reaches zero it calls a function, after that if you specify it will start again.
Natives with timers:
JASS:
local timer t=CreateTimer() // creates a timer
call TimerStart(t,89.87,false,function function's_name)//see below
call PauseTimes(t) //pauses the timer
call ResumeTimer(t) // upauses
call DestroyTimer(t) //destroys the timer preventing leaks before destruction, you should first pause them(if they are repeatable
set t=null //should null them
set t=GetExpiredTimer() //useful in the callback function
TimerGetRemaining(t) //how much time remains
TimerGetElapsed(t) //how much time has passed since it has been started(not including pauses)
TimerGetTimeout(t) //the initial amount of time the timer was set to
There are more but if I haven't missed any, they are all connected with timer dialogs.
Starting - t is the timer, 89.87(just a random number i thought of) is a the time it takes for the timer to reach zero. false/true states if it should be repeatable or it only has to run 1 time(true = infinite, false = 1). "function's_name" is the name of the function you are going to call(callback function) it should take nothing and return nothing.
Basically like this:
JASS:
function timer_callback takes nothing returns nothing
call BJDebugMsg("cool")
endfunction
function some_func takes whaever_you want returns more_of_it
local timer t=CreateTimer()
call TimerStart(t,1,true,function timer_callback)
set t=null
endfunction
This would show a simple message every second.
To do something a bit deferent:

JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
call DestroyTimer(t)
call BJDebugMsg("cool")
set t=null
endfunction
function some_func takes whaever_you want returns more_of_it
local timer t=CreateTimer()
call TimerStart(t,1,false,function timer_callback)
set t=null
endfunction


JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
call PauseTimer(t)
call DestroyTimer(t)
call BJDebugMsg("cool")
set t=null
endfunction
function some_func takes whaever_you want returns more_of_it
local timer t=CreateTimer()
call TimerStart(t,1,true,function timer_callback)
set t=null
endfunction
That would show the message only once even though the timer is repeatable.


JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
if(udg_timercount==10) then
call PauseTimer(t)
call DestroyTimer(t)
else 
set udg_timercount=udg_timercount+1
call BJDebugMsg(I2S(udg_timercount))
endif
set t=null
endfunction
function some_func takes whaever_you want returns more_of_it
local timer t=CreateTimer()
set udg_timercount=0
call TimerStart(t,1,true,function timer_callback)
set t=null
endfunction
That Would show numbers from 1 to 10 :)
Note that is not MUI

JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
call KillUnit(udg_tempunit)
call DestroyTimer(t)
call BJDebugMsg("cool")
set t=null
endfunction
function some_func takes unit a returns nothing
local timer t=CreateTimer()
set udg_tempunit=a
call TimerStart(t,1,false,function timer_callback)
set t=null
endfunction
This will kil a unit after one second, but note it is not MUI
Going more advanced requires an attachment system(handle variables, hsas, abc, hail or you own).
Here are basic examples not compatible with any sytem

JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
local integer i=GetAttachedIntegerFromTimer(t)
if(i==10) then
call DestroyTimer(t)
else
call SeIntegertAttachedToTimer(t,i+1)
call BJDebugMsg(I2S(i))
endif
set t=null
endfunction
function some_func takes whaever_you want returns more_of_it
local timer t=CreateTimer()
local integer i=0
call AttachIntegerToTimer(t,i)
call TimerStart(t,1,false,function timer_callback)
set t=null
endfunction
Bassicaly the same from above, but MUI.



JASS:
function timer_callback takes nothing returns nothing
local timer t=GetExpiredTimer()
call KillUnit(GetAttachedUnitFromTimer(t))
call DestroyTimer(t)
call BJDebugMsg("cool")
set t=null
endfunction
function some_func takes unit a returns nothing
local timer t=CreateTimer()
call AttachUnitToTimer(t,a)
call TimerStart(t,1,false,function timer_callback)
set t=null
endfunction
Again, the same from above, but MUI.

Cleaning up attached stuff is required(depending on attachment system)!!!
That I have not covered.
Timer Dialogs are not covered as well, but their usage in games is very rare.
These were some basics that I just came up with.
You should probably be able of using timers if you understood them.
You should know that there are tutorials on such things and you should really search for them :D
 
Last edited:
Status
Not open for further replies.
Top