onEachFrame: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<code>([^ ]*)\[\[([a-zA-Z][a-zA-Z0-9_]+)\]\]([^ ]*)<\/code>" to "<code>$1$2$3</code>")
m (Text replacement - "Since {{arma3}} v1.57 a stackable version of this EH is available:" to "Since {{arma3}} v1.58 a stackable version of this EH is available:")
 
(18 intermediate revisions by 3 users not shown)
Line 12: Line 12:
|gr1= Event Handlers
|gr1= Event Handlers


|descr= Runs given statement every frame.
|descr= Runs given statement every frame in an [[Scheduler#Unscheduled Environment|unscheduled environment]].
{{Feature|arma3 |
{{Feature|arma3|
In order to keep compatibility between official and community content:
In order to keep compatibility between official and community content:
* Since {{arma3}} v1.57 a stackable version of this EH is available: [[Arma 3: Mission Event Handlers#EachFrame|EachFrame Mission Event Handler]]
* Since {{arma3}} v1.58 a stackable version of this EH is available: [[Arma 3: Mission Event Handlers#EachFrame|EachFrame Mission Event Handler]]
* Before that, the scripted version of the event handler should be used instead (using [[BIS_fnc_addStackedEventHandler]] and [[BIS_fnc_removeStackedEventHandler]]).}}
* Before that, the scripted version of the event handler should be used instead (using [[BIS_fnc_addStackedEventHandler]] and [[BIS_fnc_removeStackedEventHandler]]).
}}


|s1= [[onEachFrame]] statement
|s1= [[onEachFrame]] statement
Line 24: Line 25:
|r1= [[Nothing]]
|r1= [[Nothing]]


|x1= <code>onEachFrame { hintSilent [[str]] [[position]] [[player]]}; {{cc|Hints position every frame}}</code>
|x1= <sqf>onEachFrame { hintSilent str getPosWorld player }; // hints position every frame</sqf>


|x2= Private variables defined outside of the [[onEachFrame]] scope are not inherited:
|x2= Private variables defined outside of the [[onEachFrame]] scope are not inherited:
<code>_myvar = "bob";
<sqf>
myvar = "bill";
_myVar = "bob";
[[onEachFrame]] { [[hintSilent]] [[str]] [_myvar, myvar]; };
MyVar = "bill";
{{cc|Result: [any,"bill"]}}</code>
onEachFrame { hintSilent str [_myVar, MyVar]; };
// Result: [any, "bill"]
</sqf>


|x3= Only one [[onEachFrame]] loop can exist at any time:
|x3= Only one [[onEachFrame]] loop can exist at any time:
<code>[[onEachFrame]] { [[player]] [[sideChat]] "first"; };
<sqf>
[[onEachFrame]] { [[player]] [[sideChat]] "second"; };
onEachFrame { player sideChat "first"; };
{{cc|Result: "second", "second", "second"...}}</code>
onEachFrame { player sideChat "second"; }; // replaces the first one
onEachFrame {}; // reset to the initial state
</sqf>


Note how "first" never gets shown even though it precedes "second". This is because script thread is executing within the same frame and first [[onEachFrame]] is overwritten before it has a chance to execute its statement.
|x4= Script suspension is not permitted within [[onEachFrame]] scope:
|x4= Script suspension is not permitted within [[onEachFrame]] scope:
<code>{{cc|Will throw an error}}
<sqf>
[[onEachFrame]] { [[sleep]] 1; };</code>
// will throw an error
onEachFrame { sleep 1; };
</sqf>


|x5= <code>onEachFrame {}; {{cc|Reset event}}</code>
|x5= <sqf>
onEachFrame { if (diag_frameNo mod 60 isEqualTo 0) then { /* code */ } }; // executes every 60 frames (every second @ 60 FPS, every two seconds @ 30 FPS, etc)
 
onEachFrame { // this version is the closest to a precise 1 second tick
if (isNil "TAG_LastHint") then
{
TAG_LastHint = diag_tickTime;
}
else
{
if (diag_tickTime - TAG_LastHint >= 1) then // set the duration in seconds here
{
hint format ["a second has passed (%1s since game start)", diag_tickTime];
TAG_LastHint = diag_tickTime;
};
};
};
</sqf>


|seealso= [[diag_frameNo]] [[diag_fps]]
|seealso= [[diag_frameNo]] [[diag_fps]]
}}
}}

Latest revision as of 19:15, 15 September 2024

Hover & click on the images for description

Description

Description:
Runs given statement every frame in an unscheduled environment.
Arma 3
In order to keep compatibility between official and community content:
Groups:
Event Handlers

Syntax

Syntax:
onEachFrame statement
Parameters:
statement: String or Code
Return Value:
Nothing

Examples

Example 1:
onEachFrame { hintSilent str getPosWorld player }; // hints position every frame
Example 2:
Private variables defined outside of the onEachFrame scope are not inherited:
_myVar = "bob"; MyVar = "bill"; onEachFrame { hintSilent str [_myVar, MyVar]; }; // Result: [any, "bill"]
Example 3:
Only one onEachFrame loop can exist at any time:
onEachFrame { player sideChat "first"; }; onEachFrame { player sideChat "second"; }; // replaces the first one onEachFrame {}; // reset to the initial state
Example 4:
Script suspension is not permitted within onEachFrame scope:
// will throw an error onEachFrame { sleep 1; };
Example 5:
onEachFrame { if (diag_frameNo mod 60 isEqualTo 0) then { /* code */ } }; // executes every 60 frames (every second @ 60 FPS, every two seconds @ 30 FPS, etc) onEachFrame { // this version is the closest to a precise 1 second tick if (isNil "TAG_LastHint") then { TAG_LastHint = diag_tickTime; } else { if (diag_tickTime - TAG_LastHint >= 1) then // set the duration in seconds here { hint format ["a second has passed (%1s since game start)", diag_tickTime]; TAG_LastHint = diag_tickTime; }; }; };

Additional Information

See also:
diag_frameNo diag_fps

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note