tophe 69 Posted June 27, 2009 I'm trying to optimize some of my scripts... Now I'm wondering if the waitUntil command is less, more or equally demanding as looping if-statements. Just to illustrate: example 1: while {_run} do { waitUntil {getDammage _unit > 0.5}; sleep 10; _unit setDamage 0; }; example 2: while {_run} do { if (getDammage _unit > 0.5) then {sleep 10; _unit setDamage 0;} }; The drawback of the waitUntil command would of course be if I would have say 10 if statements every loop. That would make me have to do a lot of waitUntil-statements with tons of alternatives. But which one of the above examples would cause the least stress on the CPU and memory? Share this post Link to post Share on other sites
KJT 0 Posted June 27, 2009 When condition is false (which is assumed to happen most of time), the second example could do several hundreds of loops when it gets control and each time it checks for the condition - but the first example only checks the condition once and the control is returned immediately to other scripts. So the first example is much better. -KJT- Share this post Link to post Share on other sites
xeno 234 Posted June 27, 2009 How about that one: waitUntil {if (damage _unit > 0.5) then {_unit setDamage 0};false}; A one liner that exactly achieves what you are doing with the while loop. The waitUntil will never stop checking as the condition is allways false. And don't worry about performance. As somebody stated (afair it was Kronzky) even 500 waitUntils are no problem. Xeno Share this post Link to post Share on other sites
pogoman979 10 Posted June 28, 2009 thats bizarre. i'm assuming waituntil acts like a loop in that it checks constantly for the condition to be true, so what makes the waituntil loop more efficient than a normal while loop? Share this post Link to post Share on other sites
Rommel 2 Posted June 28, 2009 (edited) Both while loops and wait Until loops operate in the same way, however a while loop executes every cycle (unless there is a sleep). Whereas a wait Until loop does not, it appears to operate every 'few' cycles. Edited June 28, 2009 by Rommel Share this post Link to post Share on other sites
tophe 69 Posted June 28, 2009 Ok.. this is really interesting! So if I put a "sleep 0.1" inside a while-loop it would be pretty much equally demanding as a waitUntil? Share this post Link to post Share on other sites
Rommel 2 Posted June 28, 2009 Ok.. this is really interesting!So if I put a "sleep 0.1" inside a while-loop it would be pretty much equally demanding as a waitUntil? No. If you want to test this out try the following. test.sqf _t = time; while{true}do{ if (_t != time)exitwith{_t = time - _t}; }; _t2 = time; waituntil{ if(_t2 != time)exitwith{_t2 = time - _t2}; 0>1 }; hintsilent format ["While loop cycle time: %1s \n Waituntil cycle time: %2s", _t, _t2]; You may have to times the results by 1000 or it may round off to 0. hintsilent format ["While loop cycle time: %1s \n Waituntil cycle time: %2s", _t * 1000, _t2 * 1000]; It could return what the delays are, but in any case, good chance your PC just may freeze up cause time seems to stop until the while loop completes (compliments of SQF). Share this post Link to post Share on other sites
tophe 69 Posted June 28, 2009 Cool little snippet! I'll have a go with it tonight. Thanks man. I'm thinking... if I could find out how long a waitUntil waits between each cycle and put an equal, or close to, amount of sleep in a while loop. Shouldn't the while loop then loop just as the waitUntil? Share this post Link to post Share on other sites
Rommel 2 Posted June 28, 2009 Yes, but then again the wait until loop cycles may not be have time delay, but a cycle delay. Then again also the time measured in game is purely dependent on FPS as this will be also. So perhaps it will be equal. Share this post Link to post Share on other sites
xeno 234 Posted June 28, 2009 You can use sleep in a waitUntil scope too. Xeno Share this post Link to post Share on other sites
KJT 0 Posted June 28, 2009 Both while loops and wait Until loops operate in the same way, however a while loop executes every cycle (unless there is a sleep).Whereas a wait Until loop does not, it appears to operate every 'few' cycles. There is no such thing as WaitUntil loop. WaitUntil is like Sleep with a condition instead of fixed time interval. As biki says abot WaitUntil: "Suspend execution of function or SQF based script until condition is satisfied." And about Sleep: "Suspend execution for given time in seconds" So suspend is the keyword. I would assume that WaitUntil is called at each frame or each time functions get called just like the while loop. Whereas While loop does not get suspended immediately. It can be execute many times halting other scripts, and that is why you have sleep in it to suspend the function/script execution to let other scripts to run. So if you have sleep 0.1 in while loop (in the example) it is in theory more efficient than WaitUnitil (if your fps > 10). -KJT- Share this post Link to post Share on other sites
xeno 234 Posted June 28, 2009 Well, you don't need a sleep in a while loop in A2 anymore. All scripts have a time limit in each frame. http://community.bistudio.com/wiki/ArmA:_Editing#Scripting Even a script like this _t = 0; while {true} do { _t = _t + 1; }; will not freeze A2 anymore. And, you can use sleep in a waitUntil too. Something like this is totally valid too: _t = 0; waitUntil {sleep 0.1;_t = _t + 1;false}; The difference is that waitUntil seems to get checked only once each frame whereas while runs multiple iterations in each frame. Xeno Share this post Link to post Share on other sites
UNN 0 Posted June 28, 2009 (edited) waitUntil {sleep 0.1;_t = _t + 1;false}; As already mentioned, there isn't much point in this. You are essentialy doing two checks in time/frames with the added sleep command. Unless you want a loop on every frame or you really want the loops exit condition to be fired at the end of the loop, instead of the start. You may as well just use a While loop, with a sleep command? Another thing to consider is, WaitUntil (in Arma1 at least) will not stop when the player hits the escape key during a misison, unlike the While loop. I need to test this in Arma2, esp MP. If a client is manipulating a global object with a while loop and he hits the esc key. It should stop the global object from being updated. Edited June 28, 2009 by UNN Share this post Link to post Share on other sites