{"name":"onEachFrame","operator":null,"title":"onEachFrame","type":"command","url":"https://community.bistudio.com/wiki/onEachFrame","revid":369525,"updated":"2024-09-15T18:15:30Z","groups":["Event Handlers"],"status":"ok","replaced_by":[],"games":[{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.63"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.63"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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> - 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","since":null,"effect":null,"exec":null,"params":[{"name":"statement","type":"String or Code","optional":false,"default":null,"description":null,"types":["String","Code"],"since":null,"text":"statement: String or Code"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"unary","operator":null,"left":null,"right":{"text":"statement","types":["String","Code"]}}],"examples":[{"text":"```sqf\nonEachFrame { hintSilent str getPosWorld player }; // hints position every frame\n```","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};"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["diag_frameNo","diag_fps"],"notes":[],"categories":["Arma 2: Operation Arrowhead: New Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 3: Scripting Commands","Command Group: Event Handlers","Introduced with Arma 2: Operation Arrowhead version 1.63","Scripting Commands","Take On Helicopters: Scripting Commands"]}