Jump to content
Sign in to follow this  
alleycat

Endless loop problem

Recommended Posts

How to endlessly loop?

while (true) would work in theory, however the comref says:

http://www.arma2.com/comref/full.html#While%20TypedoCode

Repeats the code while the condition is true. Note: the maximum repeat count for the loop is 10000. If the condition is still true after the loop was repeated 10000 times, the loop will be terminated and an error message is shown.

Even with using "sleep" my script might easily go above 10000. Is there an efficient way to loop forever?

Share this post


Link to post
Share on other sites

10000 cycles would take a fraction of a second, I've never had this problem, it must refer to something else.

Share this post


Link to post
Share on other sites

Damn, Alleycat had me sweating... I was awaiting an answer too. Thanks F2k-Sel!

Share this post


Link to post
Share on other sites

Well it does happen, though. Happened to me, se here.

It is definitely the case in very rare occasions, though. The while {true} works flawlessly. Wonder waht's the difference.

Share this post


Link to post
Share on other sites

Refereing to this page: http://community.bistudio.com/wiki/Code_Optimisation the 10000 executions limit is given when the code is run non-scheduled. You can avoid this by using spawn, exec or execVM as they are creating threads within the scheduled enviroment. The tradeoff for this is a propably bigger delay between code executions in situations of high load because the script engine pauses for 0.3 ms between every execution of a script's statement within the scheduler. So for example having 30 scripts with 100 lines each loaded in the scheduler means that the scripts are rougly parsed from the top to the end every second. So having a loop in one of these scripts means that the loop can just be executed once every second.

Share this post


Link to post
Share on other sites

So if the while loops are called within a script that was called by execVM, the loop can be endless without triggering the 10000 loops limit?

Share this post


Link to post
Share on other sites

This will easily count past 10000, but using a hint it slows down the loop.

null=[] spawn {aa = 0;     while {true} do {aa = aa+ 1;hintsilent str aa }}

this will be almost instant, it just shows how fast 10000 loops is.

null=[] spawn {aa = 0;     while {true} do {aa = aa+ 1;if (aa>10000) then {hintsilent "done"}}}

Share this post


Link to post
Share on other sites

Yes, as long as you run the infinite loop in an interruptible section the limitation will not apply.

Un-interrubtible code is code running from init lines, trigger conditions and activation fields (can't remember if there's more). And of course any code called from those sections using the call command.

Anything running from spawn/execVM as well as init.sqf is interruptible.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×