Continued …

The forEach loop. I have briefly touched it previously, so this will be short. This loop will iterate through given array one by one and quit when no more elements left to iterate through. If you try to add or delete elements from source array while loop is running with + or –  it will not make any difference to how many iterations the loop will make even though the length of the source array will get altered.

_array = [1,2,3]; diag_log format ["_array:%1", _array]; { _array = _array + [0]; diag_log format ["_x:%1/_forEachIndex:%2/_array:%3", _x, _forEachIndex, _array]; } forEach _array; diag_log format ["_array:%1", _array]; //.rpt file //"_array:[1,2,3]" //"_x:1/_forEachIndex:0/_array:[1,2,3,0]" //"_x:2/_forEachIndex:1/_array:[1,2,3,0,0]" //"_x:3/_forEachIndex:2/_array:[1,2,3,0,0,0]" //"_array:[1,2,3,0,0,0]"

However be extra careful using set command as it will alter the source array while loop is running and may result in never ending loop (min command is used in the following example to limit the length of array to stop the loop from running away).

_array = [1,2,3]; diag_log format ["_array:%1", _array]; { _array set [(count _array) min 5, 0]; diag_log format ["_x:%1/_forEachIndex:%2/_array:%3", _x, _forEachIndex, _array]; } forEach _array; diag_log format ["_array:%1", _array]; //.rpt file //"_array:[1,2,3]" //"_x:1/_forEachIndex:0/_array:[1,2,3,0]" //"_x:2/_forEachIndex:1/_array:[1,2,3,0,0]" //"_x:3/_forEachIndex:2/_array:[1,2,3,0,0,0]" //"_x:0/_forEachIndex:3/_array:[1,2,3,0,0,0]" //"_x:0/_forEachIndex:4/_array:[1,2,3,0,0,0]" //"_x:0/_forEachIndex:5/_array:[1,2,3,0,0,0]" //"_array:[1,2,3,0,0,0]"

There are 3 more specific forEach loops that deal with teams forEachMember, forEachTeamMember and forEachMemberAgent. In all cases _x contains current element, _forEachIndex has its index.

Ah, almost forgot about onEachFrame loop. This is an infinite loop that will execute given code {} every frame. Useful if you want to output something on the screen so it is tied to your frame rate. For example having some while true GUI script run with delay sleep 0.01 will update your HUD 100 times per second, a bit pointless if you only have 60 FPS. In this case onEachFrame command will be more suitable.

onEachFrame {hint str diag_fps}; onEachFrame {hint str diag_tickTime};

This command will spawn a script loop {} so that the rest of your code in the same thread as onEachFrame can carry on. The engine will also only ever spawn 1 instance of onEachFrame so any subsequent call of this command will overwrite previous call. So to cancel onEachFrame run it again with empty code.  It does not tolerate any script suspension either like sleep or waitUntil. Also if you want to pass parameter to the code inside the loop you will need to use a global variable.

_i = "123"; onEachFrame { hint str (isNil "_i"); //hint loops "true" }; cutText [str _i, "PLAN DOWN"]; //script continues and shows "123" onEachFrame {hint "1"; sleep 1; hint "2"}; //.rpt is spamming error i = 0; onEachFrame { i = i + 1; hint str i; //hint starts counting up from 0 }; i = 0; //hint resets counting and starts from 0 onEachFrame {}; //cancels the loop

There is more.

Enjoy,
KK

Pages: 1 2