• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece!🔗 Click here to enter!

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:
Level 26
Joined
Aug 18, 2009
Messages
4,097
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?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
whats wrong with

JASS:
scope s initializer init
    private function onPeriod takes nothing returns nothing
        //stuff
    endfunction

    function init takes nothing returns nothing
        call TimerStart(CreateTimer(), true, 0.1, function onPeriod)
    endfunction
endscope

?
 
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 :).
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
Cooler != better nor necessarily functioning

Also you cant depend on the timeout if you wish to use this, because even if you wabt to wait for 0.1 seconds, it will be around .3+, so dummy movement cannot be placed within such a loop
 
Status
Not open for further replies.
Top