Use loop as a periodic function

Status
Not open for further replies.
This library is a cool one.

JASS:
library LoopTest initializer Init

    private function Init takes nothing returns nothing
        local integer myInteger=0
    
        loop
            exitwhen false
            call TriggerSleepAction(.1)
            
            set myInteger=myInteger+1
            call BJDebugMsg(I2S(myInteger))
        endloop
        
    endfunction
    
endlibrary

Using TriggerSleepAction isn't a good idea to do with, but is this safe?, this will work perfectly in javascript, C/C#/C++, etc.., but how about that on WE :D..

One thing is: It have a limitation is only have periodic timer >= 0.1s.
 
Last edited:
Because in your example you just incremented a counter. But what is the advantage? TriggerSleepAction is not only low-bounded but also imprecise and needs to be synced. Furthermore you cannot abort this loop from the outside. How do you think it's unsafe and what has it to do with inits in specific?
 
Because in your example you just incremented a counter. But what is the advantage? TriggerSleepAction is not only low-bounded but also imprecise and needs to be synced. Furthermore you cannot abort this loop from the outside. How do you think it's unsafe and what has it to do with inits in specific?

Oops!, my bad T.T, Please do not care about what's inside the loop, I just want to know, is this safe if I apply this way to spell, system, etc and use it in multiplayers?
It just an example you know >.<, on exitwhen I can simply add a boolean or some specific conditions to stop the loop. Forgot about it in my example.

@edo: This way is more cooler ;P
 
Oops!, my bad T.T, Please do not care about what's inside the loop, I just want to know, is this safe if I apply this way to spell, system, etc and use it in multiplayers?
It just an example you know >.<, on exitwhen I can simply add a boolean or some specific conditions to stop the loop. Forgot about it in my example.

It depends. Yes, it is "safe", but it is not a good option.

  • TriggerSleepAction is not as accurate as a timer. In multiplayer, it may end up being decently delayed (based on latency/sync times). But even in single player, it will wait only as little as 0.0975 to 0.250 seconds, and it varies. You can definitely notice the delay with a wait as small as 0.1 seconds.
  • You cannot run this kind of wait in a trigger condition. It can only be ran in a trigger action (hence the name).
  • With longer waits, e.g. 5 seconds, you won't really notice the delay, so you can use a loop for that. Still, if you want things to be precise and predictable, then it is always better to use a timer. TriggerSleepAction is convenient, but not ideal.
 
It depends. Yes, it is "safe", but it is not a good option.

  • TriggerSleepAction is not as accurate as a timer. In multiplayer, it may end up being decently delayed (based on latency/sync times). But even in single player, it will wait only as little as 0.0975 to 0.250 seconds, and it varies. You can definitely notice the delay with a wait as small as 0.1 seconds.
  • You cannot run this kind of wait in a trigger condition. It can only be ran in a trigger action (hence the name).
  • With longer waits, e.g. 5 seconds, you won't really notice the delay, so you can use a loop for that. Still, if you want things to be precise and predictable, then it is always better to use a timer. TriggerSleepAction is convenient, but not ideal.

Nice answer, thanks :).
 
Status
Not open for further replies.
Back
Top