Really really small waits

killingdyl

Active Member
Reaction score
6
Ok i know i have to use a timer, but i dont know how to do that.

I want make my loop have a .01 seconds delay between each loop, this way it will draw on. How would i do that using timers?

Heres my loop:
JASS:
loop
        exitwhen i > 1080
        set xx = (r * (k - 1) * Cos((i) * bj_DEGTORAD)) + (r * Cos(((k - 1) * (i)) * bj_DEGTORAD))
        set xx = (xx + x)
        set yy = (r * (k - 1) * Sin((i) * bj_DEGTORAD)) - (r * Sin(((k - 1) * (i)) * bj_DEGTORAD))
        set yy = (yy + y)
        call CreateUnit(p, 'h004', xx, yy, 0)
        set i = i + 5
    endloop
 

Sevion

The DIY Ninja
Reaction score
424
Well, assuming you want MUI (if you're creating/changing handles), you'll have to "attach" data to a timer and run the timer with a callback doing these things.

Though, it'd be easier to figure out your situation's answer if we had the full code.
 

killingdyl

Active Member
Reaction score
6
heres the full code, and i dont want to use TSA cuz i know its inaccurate and i cant get the super small .01 second wait

the code isnt finished so dont tell me it will leak or i have unnecesarry variables.

JASS:
function Trig_Test_Conditions takes nothing returns boolean
    if ( not ( GetSpellAbilityId() == 'A004' ) ) then
        return false
    endif
    return true
endfunction

function Trig_Test_Actions takes nothing returns nothing
    local unit caster = GetTriggerUnit()
    local unit array dummy
    local player p = GetOwningPlayer(caster)
    local real x = GetUnitX(caster)
    local real y = GetUnitY(caster)
    local real xx
    local real yy
    local real r = 250
    local real k = 1.6666
    local integer i = 1
    local unit fog = null
    local group g = CreateGroup()
    loop
        exitwhen i > 1080
        set xx = (r * (k - 1) * Cos((i) * bj_DEGTORAD)) + (r * Cos(((k - 1) * (i)) * bj_DEGTORAD))
        set xx = (xx + x)
        set yy = (r * (k - 1) * Sin((i) * bj_DEGTORAD)) - (r * Sin(((k - 1) * (i)) * bj_DEGTORAD))
        set yy = (yy + y)
        call CreateUnit(p, 'h004', xx, yy, 0)
        set i = i + 5
    endloop
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    set gg_trg_Test = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Test, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Test, Condition( function Trig_Test_Conditions ) )
    call TriggerAddAction( gg_trg_Test, function Trig_Test_Actions )
endfunction
 

Nestharus

o-o
Reaction score
84
Make i a global named like iTrigTestLoop or something and store the index from there. Put the loop code into its own function and run it on a timer that's set for .032 if you want it to be super fast, however .2 is probably fast enough depending on what you are doing ; ).

If you have any other variables for it, just make them globals too.

A scope or a struct is highly suggested = ). You can store the instance of the struct into the timer so that you can have multiple instances of the loop going on at once.

Another method that will keep the loop going on forever is by putting it into a trigger as a triggercondition and evaluating that trigger forever. However, I would not suggest that as #1, it's instance, and #2, memory is not cleaned up until the cycle ends, so an infinite loop of that nature would eventually crash a computer.

So really, the timer method I talked about above is your best option ; ). It's best in a struct or at least a scope to support extensibility and maintainability, but it can be done in plain JASS with a few globals or even a few arrays.


Good luck : D.
 

killingdyl

Active Member
Reaction score
6
confused as hell! was just hoping for a little piece of code that would help me do that, or if someone can teach me how to do it in struct(whatever that is)
 

Strilanc

Veteran Scripter
Reaction score
42
You're going to have to use timers to perform the wait. Unfortunately, they don't allow you to just plop in a 'wait X time here' statement. Instead, they call a function when they expire. That means you need to somehow store your local variables and retrieve them in the called function.

There are several ways to associate data with a timer. The easiest is to just use a hashtable with GetHandleId(timer) as one of the keys.

So, in the method where you want to wait, you create the timer and store the necessary data in the hash table using the timer as the primary key. You then start the timer, pointing it at the method you want to run after the wait. In the called method, you use GetExpiredTimer as the primary key for retrieving the data you stored earlier.

Remember to clear out the saved data and destroy the timer.
 

Strilanc

Veteran Scripter
Reaction score
42
Well, your loop is essentially going to be a function called by a repeating timer. Quick example, which probably has a bunch of incorrect function names and typos:

JASS:

globals
    hashtable h = InitHashtable()
endglobals

private function Iterate takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer i = LoadInteger(h, GetHandleId(t), 0)

    //loop body
    call BJDebugMsg(I2S(i))

    //decrement stored 'i' for next loop    
    call SaveInteger(h, GetHandleId(t), 0, i-1)    

    //stop when i hits 0
    if i <= 0 then
        call StopTimer(t)
        call DestroyTimer(t)
        call ClearInteger(h, GetHandleId(t), 0)
    endif

    set t = null
endfunction

function StartIterate takes integer repeatCount returns nothing
    local timer t = CreateTimer()
    call SaveInteger(h, GetHandleId(t), 0, repeatCount)
    call TimerStart(t, 0.01, function Iterate)
    set t = null
endfunction
 

Jesus4Lyf

Good Idea™
Reaction score
397
This should be easy to understand. It executes a loop 32 times a second (every 0.03125 seconds - not 0.01 but it's better because it won't lag and still doesn't frame up horribly :)).

Requires T32.
JASS:
struct Trig_Test_Struct
    // local vars go here.
    unit caster = GetTriggerUnit()
    unit array dummy
    player p = GetOwningPlayer(caster)
    real x = GetUnitX(caster)
    real y = GetUnitY(caster)
    real xx
    real yy
    real r = 250
    real k = 1.6666
    integer i = 1
    unit fog = null
    group g = CreateGroup()
    
    method periodic takes nothing returns nothing
        // loop goes here.
        if i > 1080 then // <-- exitwhen
            // code after the loop goes here.
            // then add these lines.
            call this.stopPeriodic() // add this line.
            call this.destroy() // add this line.
            return // add this line.
        endif
        set xx = (r * (k - 1) * Cos((i) * bj_DEGTORAD)) + (r * Cos(((k - 1) * (i)) * bj_DEGTORAD))
        set xx = (xx + x)
        set yy = (r * (k - 1) * Sin((i) * bj_DEGTORAD)) - (r * Sin(((k - 1) * (i)) * bj_DEGTORAD))
        set yy = (yy + y)
        call CreateUnit(p, 'h004', xx, yy, 0)
        set i = i + 5
    endmethod
    
    implement T32x // magic.
    
    method act takes nothing returns nothing
        // things before the loop go here.
        call this.startPeriodic() // finish with this line.
    endmethod
endstruct

function Trig_Test_Actions takes nothing returns nothing
    // use this:
    call Trig_Test_Struct.create().act()
endfunction

That is an exact port of your code.
 

killingdyl

Active Member
Reaction score
6
i copied your code but it says that all my variables are undeclared, so i tried adding local in front of all of it and it says that some variables were redeclared or something like that
 

killingdyl

Active Member
Reaction score
6
Perfecto works like a charm! little slow but hey cant help it since im creating around 400 units +rep
 

Jesus4Lyf

Good Idea™
Reaction score
397
Yay! I'm happy for you.

On a related note, I've seen people say that T32 is difficult to learn compared to TU or KT2. I'd like for people to note this thread, because it is actually the easiest to learn, in my opinion. Heck, if it's as simple as copy/paste and add a few lines, I doubt you can really go wrong. :thup:
 

Sevion

The DIY Ninja
Reaction score
424
T32 is difficult to learn? How o_O'

I think in terms of difficulty they go: KT2, TU, T32
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top