• 🏆 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!

Timer in Jass?

Status
Not open for further replies.
Level 22
Joined
Aug 27, 2013
Messages
3,973
I just started to learn Jass recently. Could someone help me to use timer?
are timer in Jass and periodic timer the same? how to make something with timer? for example knockback spell?
i tried to learn this from tutorial but there is no tutorial about this (well, i couldn't find it)
if there is and you know it, please give me the link
 
Level 20
Joined
Aug 13, 2013
Messages
1,696
@Rheiko I have a impact spell Pure JASS in the spell section so maybe you can use it as a reference

How to use timer in jass?
Simple.


JASS:
local timer myTimer = CreateTimer()
call TimerStart( myTimer, 0.03125, true, function onPeriodic )
You need to create a variable for a timer.
How to create?: to create timer you must write this:CreateTimer()
then start the timer:
What is the arguments of the timer?:
call TimerStart( myTimer, 0.03125, true, function onPeriodic )

TimerStart = this is the function in JASS that you need to write to start the timer.

myTimer = this is timer, this is our variable that we create: local timer myTimer = CreateTimer()

0.03125 = this is real, this is the periodic_interval seconds of the timer

true = this is boolean, if you want to periodic the timer then set this to true, if you want time elapsed then set this to false

function onPeriodic = this is function or codefunc, this is the function that we can call from the timer so the timer will loop to this function call onPeriodic. onPeriodic must be always on the top when you call it.
Example:
JASS:
function OnPeriodic takes nothing returns nothing
    local timer t = GetExpiredTimer() // Get the starting timer that we create.
    call BJDebugMsg(" bla bla timer is looping to this function yahoo!" )
    call PauseTimer( t ) // Pause the timer before destroying it.
    call DestroyTimer( t ) // Destroy like destroying groups
    set t = null // null it of course
endfunction

function ExecutionOfTheSpell takes nothing returns nothing
    local timer t = CreateTimer( t ) // Create timer
    call TimerStart( t, 0.03125, true, function OnPeriodic ) // Starting the timer
endfunction
 
Last edited:
Level 19
Joined
Aug 8, 2007
Messages
2,765
please dont use white colored text, different themes can't see it.

Basically (If you dont want to get started with stuff such as CTL or T32) all vJass timers rely on this function
(0.03125 and true might be swapped around, not sure)
JASS:
call TimerStart(CreateTimer(), true, 0.03125, function onLoop)

This will call the function onLoop 32 times every second (this is the recommended speed for something like knockback or jump because its the most accurate of 1loop/frame)

You can also attach timer data (again, if you dont want to use AttachTimerData) through a hashtable if needed

JASS:
local timer t = CreateTimer()
call SaveInteger(hash, 0, GetHandleId(t), 1337)
call TimerStart(t...
set t = null

than retrieve it through

JASS:
local integer int = LoadInteger(hash, 0, GetHandleId(GetExpiredTimer()))

Also, if your going to use false as the parameter for timer start, dont forget

JASS:
call DestroyTimer(GetExpiredTimer())
 
Level 22
Joined
Aug 27, 2013
Messages
3,973
please dont use white colored text, different themes can't see it.

Basically (If you dont want to get started with stuff such as CTL or T32) all vJass timers rely on this function
(0.03125 and true might be swapped around, not sure)
JASS:
call TimerStart(CreateTimer(), true, 0.03125, function onLoop)

This will call the function onLoop 32 times every second (this is the recommended speed for something like knockback or jump because its the most accurate of 1loop/frame)

You can also attach timer data (again, if you dont want to use AttachTimerData) through a hashtable if needed

JASS:
local timer t = CreateTimer()
call SaveInteger(hash, 0, GetHandleId(t), 1337)
call TimerStart(t...
set t = null

than retrieve it through

JASS:
local integer int = LoadInteger(hash, 0, GetHandleId(GetExpiredTimer()))

Also, if your going to use false as the parameter for timer start, dont forget

JASS:
call DestroyTimer(GetExpiredTimer())
first of all, sorry for the white text.. just too familiar with it
second, as jakeZinc said.. i want Jass, but still.. thanks

@Rheiko
I edited my post that how to create timer.

@Arhowk

He wants Pure JASS not vJASS lol :D.
Thanks jake
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Arhowk gave a jass example, not vjass...
@Maker, you still need to null a local timer variable when exiting a local function, that would leak a reference, and the handle id won't be recycled.

Read this again, carefully this time:
Basically (If you dont want to get started with stuff such as CTL or T32) all vJass timers rely on this function
(0.03125 and true might be swapped around, not sure)
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
@chobibo
Oh sorry, I thought it is vJASS because on this words: (If you dont want to get started with stuff such as CTL or T32)

My code was JASS. -_- CTL and T32 are in vJass, but as i explicitly stated, that example is without the use of CTL or T32

E/ Even though i did say vJass timers (which was a typo, ive been coding in vJass alone for three years so its second nature)

@Maker: I dont think so. "local timer t" will still have a handle reference to the timer, even though nothing's there. (bench test anybody?)
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
There is no memory consumption increase with this
JASS:
library leakTest initializer onInit
    private function leaks takes nothing returns nothing
        local timer t = GetExpiredTimer()
    endfunction
    
    private function start takes nothing returns nothing
        call TimerStart(CreateTimer(), 0.001, true, function leaks)
    endfunction

    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddAction(t, function start)
    endfunction
endlibrary
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
EDIT: Okay got it.

Made my own tests:
JASS:
library TimerTest initializer onInit

    globals
        constant timer T=CreateTimer()
    endglobals
    
    function B takes nothing returns nothing
        call DestroyTimer(GetExpiredTimer())
    endfunction

    function A takes nothing returns nothing
        local timer t=CreateTimer()
        call TimerStart(t,1.00,false,function B)
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,50.00,I2S(GetHandleId(t)))
    endfunction
    
    function onInit takes nothing returns nothing
        call TimerStart(T,2.0,true,function A)
    endfunction
endlibrary

The result was:
122581-albums6788-picture75669.png


JASS:
library TimerTest initializer onInit

    globals
        constant timer T=CreateTimer()
    endglobals
    
    function B takes nothing returns nothing
        call DestroyTimer(GetExpiredTimer())
    endfunction

    function A takes nothing returns nothing
        local timer t=CreateTimer()
        call TimerStart(t,1.00,false,function B)
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,50.00,I2S(GetHandleId(t)))
    endfunction
    
    function C takes nothing returns nothing
        local timer t=CreateTimer()
        call TimerStart(t,1.00,false,function B)
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,50.00,I2S(GetHandleId(t)))
        set t=null
    endfunction
    
    function onInit takes nothing returns nothing
        call TimerStart(T,2.0,true,function C)
    endfunction
endlibrary

122581-albums6788-picture75670.png


It leaks references.
I attached the the map.
 

Attachments

  • TimerTest.w3m
    12.2 KB · Views: 81
Last edited:
Level 22
Joined
Sep 24, 2005
Messages
4,821
But the reference is still lost, even if you don't destroy the timer. And the example you supplied does indeed use memory, you created a timer. I just wanted to clear that up, so just ignore my post.

JASS:
library leakTest initializer onInit
    private function leaks takes nothing returns nothing
        local timer t = GetExpiredTimer()
    endfunction
   
    private function start takes nothing returns nothing
        call TimerStart(CreateTimer(), 0.001, true, function leaks)
    endfunction

    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddAction(t, function start)
    endfunction
endlibrary
 
Status
Not open for further replies.
Top