{"name":"onEachFrame","title":"onEachFrame","type":"command","url":"https://community.bistudio.com/wiki/onEachFrame","revid":369525,"updated":"2024-09-15T18:15:30Z","groups":["Event Handlers"],"status":"ok","games":{"arma2oa":"1.63","tkoh":"1.00","arma3":"0.50"},"since":"arma2oa 1.63","arma3_since":"0.50","description":"Runs given statement every frame in an unscheduled environment.\n\n> [!NOTE]\n> **Arma 3:** In order to keep compatibility between official and community content:\n>\n> - Since Arma 3 v1.58 a stackable version of this EH is available: EachFrame Mission Event Handler\n> - Before that, the scripted version of the event handler should be used instead (using BIS_fnc_addStackedEventHandler and BIS_fnc_removeStackedEventHandler).","syntaxes":[{"syntax":"onEachFrame statement","params":[{"name":"statement","type":"String or Code","type_expr":"String | Code"}],"returns":{"type":"Nothing","type_expr":"Nothing"},"form":"unary","right":{"text":"statement"}}],"examples":[{"code":["onEachFrame { hintSilent str getPosWorld player }; // hints position every frame"]},{"text":"Private variables defined outside of the onEachFrame scope are not inherited:\n\n```sqf\n_myVar = \"bob\";\nMyVar = \"bill\";\nonEachFrame { hintSilent str [_myVar, MyVar]; };\n// Result: [any, \"bill\"]\n```","code":["_myVar = \"bob\";\nMyVar = \"bill\";\nonEachFrame { hintSilent str [_myVar, MyVar]; };\n// Result: [any, \"bill\"]"]},{"text":"Only one onEachFrame loop can exist at any time:\n\n```sqf\nonEachFrame { player sideChat \"first\"; };\nonEachFrame { player sideChat \"second\"; };    // replaces the first one\nonEachFrame {};                                // reset to the initial state\n```","code":["onEachFrame { player sideChat \"first\"; };\nonEachFrame { player sideChat \"second\"; };\t// replaces the first one\nonEachFrame {};\t\t\t\t\t\t\t\t// reset to the initial state"]},{"text":"Script suspension is not permitted within onEachFrame scope:\n\n```sqf\n// will throw an error\nonEachFrame { sleep 1; };\n```","code":["// will throw an error\nonEachFrame { sleep 1; };"]},{"text":"```sqf\nonEachFrame { if (diag_frameNo mod 60 isEqualTo 0) then { /* code */ } };    // executes every 60 frames (every second @ 60 FPS, every two seconds @ 30 FPS, etc)\n\nonEachFrame {                                                                // this version is the closest to a precise 1 second tick\n    if (isNil \"TAG_LastHint\") then\n    {\n        TAG_LastHint = diag_tickTime;\n    }\n    else\n    {\n        if (diag_tickTime - TAG_LastHint >= 1) then // set the duration in seconds here\n        {\n            hint format [\"a second has passed (%1s since game start)\", diag_tickTime];\n            TAG_LastHint = diag_tickTime;\n        };\n    };\n};\n```","code":["onEachFrame { if (diag_frameNo mod 60 isEqualTo 0) then { /* code */ } };\t// executes every 60 frames (every second @ 60 FPS, every two seconds @ 30 FPS, etc)\n\nonEachFrame {\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// this version is the closest to a precise 1 second tick\n\tif (isNil \"TAG_LastHint\") then\n\t{\n\t\tTAG_LastHint = diag_tickTime;\n\t}\n\telse\n\t{\n\t\tif (diag_tickTime - TAG_LastHint >= 1) then // set the duration in seconds here\n\t\t{\n\t\t\thint format [\"a second has passed (%1s since game start)\", diag_tickTime];\n\t\t\tTAG_LastHint = diag_tickTime;\n\t\t};\n\t};\n};"]}],"see_also":["diag_frameNo","diag_fps"]}