waitUntil: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\|seealso= *\[\[([^ ]+)\]\], \[\[([^ ]+)\]\]" to "|seealso= $1 $2")
m (Text replacement - "[Rr]oehre" to "RoehrenRadio")
 
(8 intermediate revisions by 3 users not shown)
Line 19: Line 19:


|descr= Suspends execution of [[Scheduler|scheduled]] script until the given condition satisfied.  
|descr= Suspends execution of [[Scheduler|scheduled]] script until the given condition satisfied.  
* This command will loop and call the code inside {{hl|{}}} '''mostly every frame''', depends on complexity of the condition and the overall engine load, until the [[Code|code]] returns [[true]]
* This command will loop and call the code inside <sqf inline>{}</sqf> '''mostly every frame''', depends on complexity of the condition and the overall engine load, until the [[Code|code]] returns [[true]]
 
* If the very first execution of the code returns [[true]] the command will exit immediately, therefore it will not produce any "Suspending not allowed in this context" error when used inside [[Scheduler#Unscheduled_Environment|non-scheduled]] script. For all other uses it must be executed in environment that allows [[Scheduler#Suspension|suspension]] ([[canSuspend]]), such as [[spawn]]ed or [[execVM]]ed code
* If the very first execution of the code returns [[true]] the command will exit immediately, therefore it will not produce any "Suspending not allowed in this context" error when used inside [[Scheduler#Unscheduled_Environment|non-scheduled]] script. For all other uses it must be executed in environment that allows [[Scheduler#Suspension|suspension]] ([[canSuspend]]), such as [[spawn]]ed or [[execVM]]ed code
* Avoid doing <sqf inline>waitUntil { time > 20 };</sqf> and use <sqf inline>sleep 20;</sqf> instead!
* If you can, add a [[sleep]] to the condition to save some cpu cycles <sqf inline>waitUntil { sleep 1; !alive player };</sqf>


* Avoid doing {{ic|[[waitUntil]] {[[time]] > 20};}} and use {{ic|[[sleep]] 20;}} instead!
{{Feature|arma3|Since {{arma3}} v1.94, a ''condition'' returning anything other than [[true]] or [[false]] '''will''' result in an appropriate type error.}}
 
* If you can, add a [[sleep]] to the condition to save some cpu cycles {{ic|[[waitUntil]] {[[sleep]] 1; ![[alive]] [[player]]};}}
 
{{Feature|arma3|Since Arma 3 v1.94, a ''condition'' returning anything other than [[true]] or [[false]] '''will''' result in an appropriate type error.}}


|pr= For some unknown reason if you have [[waitUntil]] loop active and game is '''saved/loaded''', some variables in the expression may appear undefined for a short time. As a workaround, assign expression to a variable and make sure it is defined before [[waitUntil]] checks it:
|pr= For some unknown reason if you have [[waitUntil]] loop active and game is '''saved/loaded''', some variables in the expression may appear undefined for a short time. As a workaround, assign expression to a variable and make sure it is defined before [[waitUntil]] checks it:
<code>[[waitUntil]] { [[private]] _expression {{=}} var1 && (var2 > 10); ![[isNil]] "_expression" && { _expression } };</code>
<sqf>waitUntil { private _expression = someBooleanVar && (someNumberVar > 10); !isNil "_expression" && { _expression } };</sqf>


|s1= [[waitUntil]] condition
|s1= [[waitUntil]] condition


|p1= condition: [[Code]] - The expression that '''must''' return a [[Boolean]], [[true]] to finish waiting or [[false]] to continue waiting
|p1= condition: [[Code]] - the expression that '''must''' return a [[Boolean]], [[true]] to finish waiting or [[false]] to continue waiting


|r1= [[Anything]] - The value the condition evaluates to when the wait is over (normally [[true]])
|r1= [[Anything]] - the value the condition evaluates to when the wait is over (normally [[true]])


|x1= <code>[[waitUntil]] { [[not]] [[alive]] [[player]] };</code>
|x1= <sqf>waitUntil { not alive player };</sqf>


|x2= <code>_i = 0; [[waitUntil]] { _i = _i + 1; _i >= 100 };</code>
|x2= <sqf>_i = 0; waitUntil { _i = _i + 1; _i >= 100 };</sqf>


|x3= <code>[[waitUntil]] {[[sleep]] 1; [[not]] [[alive]] [[player]] }; {{cc|Checks every 1 second}}</code>
|x3= [[waitUntil]] can lead to performance loss if used improperly:
<sqf>
waitUntil { not alive player }; // bad
waitUntil { sleep 1; not alive player }; // good - checks every 1 second
player addEventHandler ["Killed", {  }]; // best - don't forget about Event Handlers
</sqf>


|x4= An on-the-fly custom ''[[Arma_3:_Event_Handlers|event handler]]'':
|x4= An on-the-fly custom [[Arma 3: Event Handlers|event handler]]:
<code>_myEH = ["ZoomIn"] [[spawn]] {
<sqf>
[[while]] { [[true]] } [[do]] {
_myEH = ["ZoomIn"] spawn {
[[waitUntil]] {
while { true } do
[[inputAction]] ([[Magic Variables#this|_this]] [[select]] 0) == 1;
{
};
waitUntil { inputAction (_this select 0) == 1 };
[[diag_log]] [[format]] ["%1 @ %2", [[Magic Variables#this|_this]] [[select]] 0, [[diag_tickTime]]<nowiki>]</nowiki>;
diag_log format ["%1 @ %2", _this select 0, diag_tickTime];
};
};
};</code>
};
Although perhaps better to use [[BIS_fnc_addStackedEventHandler|onEachFrame]], depending on the application.
</sqf>
Although it may be better to use [[onEachFrame]] ([[BIS_fnc_addStackedEventHandler|stacked]]) [[Arma_3:_Mission_Event_Handlers#EachFrame|mission Event Handler]], depending on the application.


|x5= Use [[getVariable]] with default value to prevent unexcepted script errors:
|x5= Use [[getVariable]] with default value to prevent unexcepted script errors:
<code>[[waitUntil]] { bank [[getVariable]] ["money", 0] > 0 };
<sqf>
[[waitUntil]] { [[missionNamespace]] [[getVariable]] ["isready", [[false]]] };</code>
waitUntil { bank getVariable ["money", 0] > 0 };
waitUntil { missionNamespace getVariable ["isready", false] };
</sqf>


|x6= Always return [[Boolean]]:<code>{{cc|bad}}
|x6= Always return [[Boolean]]:
[[waitUntil]] { [[if]] ([[not]] [[alive]] [[player]]) [[exitWith]] {}; _time {{=}} _time + 1 };
<sqf>
{{cc|good}}
waitUntil { sleep 1; if (not alive player) exitWith {}; _time = _time + 1 }; // bad
[[waitUntil]] { [[if]] ([[not]] [[alive]] [[player]]) [[exitWith]] { [[true]] }; _time {{=}} _time + 1; [[false]] };
waitUntil { sleep 1; if (not alive player) exitWith { true }; _time = _time + 1; false }; // good
{{cc|perfect}}
waitUntil { sleep 1; not alive player }; // perfect
[[waitUntil]] { [[not]] [[alive]] [[player]] };</code>
</sqf>


|seealso= [[sleep]] [[uiSleep]] [[canSuspend]] [[spawn]] [[execVM]] [[while]] [[Control Structures]]
|seealso= [[sleep]] [[uiSleep]] [[canSuspend]] [[spawn]] [[execVM]] [[while]] [[Control Structures]]
}}
}}


<dl class="command_description">


<dt><dt>
{{Note
<dd class="notedate">Posted on December 20, 2006 - 19:55</dd>
|user= CrashDome
<dt class="note">[[User:CrashDome|CrashDome]]</dt>
|timestamp= 20061220195500
<dd class="note">
|text= waitUntil suspends both SQF functions and SQF scripts. In functions, the calling script is still in suspension due to waiting for a return from the [[call]] command. The game engine will continue, however. See [[Function]] for more detail.
waitUntil suspends both SQF functions and SQF scripts. In functions, the calling script is still in suspension due to waiting for a return from the [[call]] command. The game engine will continue, however. See [[Function]] for more detail.
}}
<dt><dt>
 
<dd class="notedate">Posted on April 2, 2010 - 17:10</dd>
{{Note
<dt class="note">[[User:Roehre|Roehre]]</dt>
|user= RoehrenRadio
<dd class="note">
|timestamp= 20100402171000
Prior to Arma 3 v1.94 if [[waitUntil]] uses an undefined [[call]] code, [[waitUntil]] won't release, even when this code is separated from other conditions through [[or]]. Be warned that this won't cause an error message.
|text= Prior to {{arma3}} v1.94 if [[waitUntil]] uses an undefined [[call]] code, [[waitUntil]] won't release, even when this code is separated from other conditions through [[or]]. Be warned that this won't cause an error message.
<dt><dt>
}}
<dd class="notedate">Posted on December 13, 2014 - 23:25 (UTC)</dd>
 
<dt class="note">[[User:Commy2|Commy2]]</dt>
{{Note
<dd class="note">
|user= Commy2
If you want to use [[waitUntil]] together with [[exitWith]], remember that the loop only exits if the code block returns true.<br>
|timestamp= 20141213232500
|text= If you want to use [[waitUntil]] together with [[exitWith]], remember that the loop only exits if the code block returns true.<br>
<br>
<br>
It should look like this:
It should look like this:
<code>[[waitUntil]] {
<sqf>
{{cc|exit loop if the unit gets deleted}}
waitUntil {
[[if]] ([[isNull]] _unit) [[exitWith]] { [[true]] }; {{cc|has to return true to continue}}
// exit loop if the unit gets deleted
if (isNull _unit) exitWith { true }; // has to return true to continue


![[alive]] _unit;
!alive _unit;
};
};
</code>
</sqf>
</dd>
}}


</dl>
{{Note
|user= AgentRev
|timestamp= 20220419060501
|text= It's a good idea to include a timeout with the condition if there's any possibility that it never becomes true:
<sqf>
disableUserInput true;
player moveInDriver _vehicle; // this command is asynchronous and can fail sometimes
_time = time;
waitUntil {vehicle player == _vehicle || time - _time > 3};
disableUserInput false;
</sqf>
}}

Latest revision as of 10:49, 8 April 2024

Hover & click on the images for description

Description

Description:
Suspends execution of scheduled script until the given condition satisfied.
  • This command will loop and call the code inside {} mostly every frame, depends on complexity of the condition and the overall engine load, until the code returns true
  • If the very first execution of the code returns true the command will exit immediately, therefore it will not produce any "Suspending not allowed in this context" error when used inside non-scheduled script. For all other uses it must be executed in environment that allows suspension (canSuspend), such as spawned or execVMed code
  • Avoid doing waitUntil { time > 20 }; and use sleep 20; instead!
  • If you can, add a sleep to the condition to save some cpu cycles waitUntil { sleep 1; !alive player };
Arma 3
Since Arma 3 v1.94, a condition returning anything other than true or false will result in an appropriate type error.
Problems:
For some unknown reason if you have waitUntil loop active and game is saved/loaded, some variables in the expression may appear undefined for a short time. As a workaround, assign expression to a variable and make sure it is defined before waitUntil checks it:
waitUntil { private _expression = someBooleanVar && (someNumberVar > 10); !isNil "_expression" && { _expression } };
Groups:
Program Flow

Syntax

Syntax:
waitUntil condition
Parameters:
condition: Code - the expression that must return a Boolean, true to finish waiting or false to continue waiting
Return Value:
Anything - the value the condition evaluates to when the wait is over (normally true)

Examples

Example 1:
waitUntil { not alive player };
Example 2:
_i = 0; waitUntil { _i = _i + 1; _i >= 100 };
Example 3:
waitUntil can lead to performance loss if used improperly:
waitUntil { not alive player }; // bad waitUntil { sleep 1; not alive player }; // good - checks every 1 second player addEventHandler ["Killed", { }]; // best - don't forget about Event Handlers
Example 4:
An on-the-fly custom event handler:
_myEH = ["ZoomIn"] spawn { while { true } do { waitUntil { inputAction (_this select 0) == 1 }; diag_log format ["%1 @ %2", _this select 0, diag_tickTime]; }; };
Although it may be better to use onEachFrame (stacked) mission Event Handler, depending on the application.
Example 5:
Use getVariable with default value to prevent unexcepted script errors:
waitUntil { bank getVariable ["money", 0] > 0 }; waitUntil { missionNamespace getVariable ["isready", false] };
Example 6:
Always return Boolean:
waitUntil { sleep 1; if (not alive player) exitWith {}; _time = _time + 1 }; // bad waitUntil { sleep 1; if (not alive player) exitWith { true }; _time = _time + 1; false }; // good waitUntil { sleep 1; not alive player }; // perfect

Additional Information

See also:
sleep uiSleep canSuspend spawn execVM while Control Structures

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


CrashDome - c
Posted on Dec 20, 2006 - 19:55 (UTC)
waitUntil suspends both SQF functions and SQF scripts. In functions, the calling script is still in suspension due to waiting for a return from the call command. The game engine will continue, however. See Function for more detail.
RoehrenRadio - c
Posted on Apr 02, 2010 - 17:10 (UTC)
Prior to Arma 3 v1.94 if waitUntil uses an undefined call code, waitUntil won't release, even when this code is separated from other conditions through or. Be warned that this won't cause an error message.
Commy2 - c
Posted on Dec 13, 2014 - 23:25 (UTC)
If you want to use waitUntil together with exitWith, remember that the loop only exits if the code block returns true.

It should look like this:
waitUntil { // exit loop if the unit gets deleted if (isNull _unit) exitWith { true }; // has to return true to continue !alive _unit; };
AgentRev - c
Posted on Apr 19, 2022 - 06:05 (UTC)
It's a good idea to include a timeout with the condition if there's any possibility that it never becomes true:
disableUserInput true; player moveInDriver _vehicle; // this command is asynchronous and can fail sometimes _time = time; waitUntil {vehicle player == _vehicle || time - _time > 3}; disableUserInput false;