• 🏆 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] Timers instead of waits

Status
Not open for further replies.
Level 9
Joined
Sep 5, 2007
Messages
358
Hey!
I started learning JASS about two days ago, and I've been messing with it using natives instead of BJ's and so on.
And I got a problem.
I don't wish to use waits. I know the evil they're.
I read posts saying "use timers instead of waits".

can somebody tell me how to do this? or at least show me a tutorial or a simple example code?

Thanks in advance :thumbs_up:
 
JASS:
function MyFunc2 takes nothing returns nothing
    //declare locals
    call DestroyTimer(GetExpiredTimer())
    //carry on with the code
endfunction

function MyFunc takes nothing returns nothing
    local timer t=CreateTimer()
    //do code
    call TimerStart(t, <duration>, false, function MyFunc2)
    //here you can do more code which won't be interrupted by the wait
    set t=null
    //clean leaks blahblahblah
endfunction
This is kinda the basic code for using a timer instead of a wait. Where it says <duration> you put how long you want to wait for. The main problem with this is the issue of transferring variables to the other function. For this, if you do not wish to make it MUI, you can store the values in global variables, or you can use some kind of timer attachment system.

Hope I helped!
 
JASS:
function Spell_TIMER takes nothing returns nothing
    // Do whatever after the "Wait"
    call DestroyTimer( GetExpiredTimer() )
endfunction

function Spell takes nothing returns nothing
 local timer T = CreateTimer()
    // Instead of "Wait 10.0 seconds"
    call TimerStart( T, 10.0, false, function Spell_TIMER )
    // "false" means "one-shot timer"
endfunction

Spell would be your trigger Actions function. It creates a timer and then starts it, executing another function with the rest of the Actions.

The problem is that, without Local Handle Vars or CSCache or any similar system, you won't be able to get any Event Response like GetTriggerUnit() unless you use globals. However, this way, it won't be MUI...

EDIT: Element was faster and remembered/reminded me of destroying the timer xD
(Just forgot which word to use: remembered or reminded? o.o' )
 
Level 12
Joined
Mar 23, 2008
Messages
942
You can do something like

JASS:
globals
    unit array MySpell_TempUnit[10]
endglobals

function FindNullSpace takes unit tu returns nothing
     local integer i
     loop
     if MySpell_TempUnit[i] == null then
          set MySpell_TempUnit[i] = tu
          set i = 10
     exitwhen i == 10
     endloop
endfunction

And make another function that runs every 1s doing actions to all units of the array. Or use a unitgroup, depending of what you want will save you some time and a cleaner code.
 
Level 13
Joined
Mar 16, 2008
Messages
941
Okay, I'm going to use Element of Water's example^^

JASS:
function MyFunc2 takes nothing returns nothing
    //declare locals
    call DestroyTimer(GetExpiredTimer())
    //carry on with the code
endfunction

function MyFunc takes nothing returns nothing
    local timer t=CreateTimer()
    //do code
    call TimerStart(t, <duration>, false, function MyFunc2)
    //here you can do more code which won't be interrupted by the wait
    set t=null
    //clean leaks blahblahblah
endfunction

Now you can see the line

JASS:
call TimerStart(t, <duration>, false, function MyFunc2)

't' is the timer itself. 'Duration' is the timeout, meaning after 'duration' seconds the callback is called.
'function MyFunc2' is the callback. Whenever the timer runs out the callback is called.
The 'false' you can see there is a boolean that stands for the repeating. If it's true, the timer will run again and again with that duration.

Now a timer that should do something every second would look like this:
JASS:
call TimerStart(t, 1., true, function MyFunc2)
 
Level 13
Joined
Mar 16, 2008
Messages
941
That was just an example to show that starting with vJass isn't a bad idea.
I know it by myself, I learned new stuff as fast as I was able to. GUI, crappy BJ-Jass, leakless game cache Jass and afterwards vJass. I rewrote my map a million times and stopped working because it annoyed me like hell. Realy, start with vJass and skip that useless normal Jass^^
 
Level 14
Joined
Nov 18, 2007
Messages
816
if he learns how to use TimerUtils NOW, theres no need to ask questions in the future. I think its better if he asks questions about TimerUtils now, instead of just destroying Timers and then wondering why his map malfunctions.

And the basics of vJass are not that hard to understand.
globals block, libraries, scopes and textmacro. Heck, even structs are not that hard to understand.
 
Level 13
Joined
Mar 16, 2008
Messages
941
Wc3 campaigns is down :/
Well, the idea behind TimerUltis is simple:
Normaly you create and destroy timers whenever you need them.
TimerUltis has only two functions:
NewTimer, that returns a free timer (one that isn't used) or creates a new one if all timers are used at the moment.
ReleaseTimer, that marks a timer as "free".
Destroying timers can mess up the handle stack or something... it's bad :p
This is the most performant way of fixing this, also creating a new timer needs more time then getting a freed one (afaik :p)
 
Level 9
Joined
Aug 2, 2008
Messages
219
Hey guys,
I´ve had the same problem as Wizardrum, and i think this thread will help me a lot to get to know how to use the timers instead of TriggerSleep.:thumbs_up:

But hey wait a sec problem solved but another one created. If i understood everything right we use the callback of the timers and this means we are not able to use any parameters and unless we dont want to use globals we need CSgamechache(). Ok the next problem is how to use a gamcache…ill try to solve that by myself. Anyway thanx 4 your help. Ill add some +REP.
 
Level 9
Joined
Aug 2, 2008
Messages
219
Ye ye i already knew that but unfortunatly my windows dislikes the program…i cant make it run .But im working on a method that allows me to move data through the memory into other functions…
Ok the method i mentioned is attatching (dident know it already had that name). And more good news…now im able to use Jass Helper that means i´ll look up the manuals.
 
Last edited:
Status
Not open for further replies.
Top