canSuspend: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(see also)
m (Fix the fix)
 
(51 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| A3 |= Game
|game1= arma3
|1.58|= Game Version (number surrounded by NO SPACES)
|version1= 1.58


____________________________________________________________________________________________
|gr2= Program Flow


| Returns [[true]] if [[sleep]], [[uiSleep]] or [[waitUntil]] commands can be used in current scope. Usually when suspension is not allowed but used, for example when code is executed in unscheduled environment, the script engine would ignore any suspension command and throw error: "Suspending not allowed in this context". Using [[canSuspend]] command allows to detect the correct environment for the code.
|descr= Returns [[true]] if [[sleep]], [[uiSleep]] or [[waitUntil]] commands can be used in the current scope.
Usually when suspension is not allowed but used, for example when code is executed in [[Scheduler#Unscheduled Environment|unscheduled environment]], the script engine would ignore any suspension command and throw error: "Suspending not allowed in this context".
Using [[canSuspend]] command allows to detect the correct environment for the code.


<br><br>'''Note''': The definition of scheduled and unscheduled environment is not the same as whether or not the script execution can or cannot be suspended. For example while .''sqs'' and .''fsm'' scripts are scheduled (i.e. added to the scheduler: [[diag_activeSQSScripts]], [[diag_activeMissionFSMs]]), they cannot use [[sleep]] or be suspended like [[execVM]] or [[spawn]] scripts can, therefore [[canSuspend]] for these types of scripts will return [[false]].  
{{Feature|informative|
|= Description
The definition of [[Scheduler#Scheduled Environment|scheduled]] and [[Scheduler#Unscheduled Environment|unscheduled environment]] is not the same as whether or not the script execution can or cannot be suspended.
____________________________________________________________________________________________
For example while .''sqs'' and .''fsm'' scripts are [[Scheduler#Scheduled_Environment|scheduled]] (i.e. added to the [[Scheduler|scheduler]]:
[[diag_activeSQSScripts]], [[diag_activeMissionFSMs]]), they cannot use [[sleep]] or be suspended like [[execVM]] or [[spawn]] scripts can, therefore [[canSuspend]] for these types of scripts will return [[false]].
}}


| '''canSuspend'''  |= Syntax
|s1= [[canSuspend]]


| [[Boolean]] |= RETURNVALUE
|r1= [[Boolean]]


|x1= <sqf>
onEachFrame
{
systemChat str canSuspend; // false
0 spawn { hint str canSuspend }; // true
onEachFrame {};
};
</sqf>


|x1= <code>[[onEachFrame]]
|x2= Make sure the function code is always spawned even when called:
<sqf>
private _sleepingCode =
{
{
[[systemChat]] [[str]] [[canSuspend]]; //false
if (!canSuspend) exitWith { _this spawn _sleepingCode };
[] [[spawn]] {[[hint]] [[str]] [[canSuspend]]}; //true
sleep _this;
[[onEachFrame]] {};
hint ("slept " + str _this);
};</code>|= EXAMPLE1
|x2= Make sure the function code is always spawned even when called:<code>mysleep =
{
[[if]] (![[canSuspend]]) [[exitWith]] {_this [[spawn]] mysleep};
[[sleep]] _this;
[[hint]] ("slept " + [[str]] _this);  
};
};


5 [[call]] mysleep;</code>|= EXAMPLE2
5 call _sleepingCode;
</sqf>


____________________________________________________________________________________________
|seealso= [[sleep]] [[uiSleep]] [[waitUntil]] [[call]] [[spawn]] [[execVM]] [[isRemoteExecuted]] [[isRemoteExecutedJIP]] [[remoteExecutedOwner]] [[isUIContext]] [[isGamePaused]] [[isGameFocused]]
 
| [[sleep]], [[uiSleep]], [[waitUntil]], [[call]], [[spawn]], [[execVM]], [[isRemoteExecuted]] |= SEEALSO
 
|  |= MPBEHAVIOUR
____________________________________________________________________________________________
}}
}}
<h3 style='display:none'>Notes</h3>
<dl class='command_description'>
<!-- Note Section BEGIN -->
<!-- Note Section END -->
</dl>
<h3 style='display:none'>Bottom Section</h3>
[[Category:Arma_3:_New_Scripting_Commands_List|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]

Latest revision as of 21:47, 17 March 2024

Hover & click on the images for description

Description

Description:
Returns true if sleep, uiSleep or waitUntil commands can be used in the current scope. Usually when suspension is not allowed but used, for example when code is executed in unscheduled environment, the script engine would ignore any suspension command and throw error: "Suspending not allowed in this context". Using canSuspend command allows to detect the correct environment for the code.
The definition of scheduled and unscheduled environment is not the same as whether or not the script execution can or cannot be suspended.

For example while .sqs and .fsm scripts are scheduled (i.e. added to the scheduler:

diag_activeSQSScripts, diag_activeMissionFSMs), they cannot use sleep or be suspended like execVM or spawn scripts can, therefore canSuspend for these types of scripts will return false.
Groups:
Program Flow

Syntax

Syntax:
canSuspend
Return Value:
Boolean

Examples

Example 1:
onEachFrame { systemChat str canSuspend; // false 0 spawn { hint str canSuspend }; // true onEachFrame {}; };
Example 2:
Make sure the function code is always spawned even when called:
private _sleepingCode = { if (!canSuspend) exitWith { _this spawn _sleepingCode }; sleep _this; hint ("slept " + str _this); }; 5 call _sleepingCode;

Additional Information

See also:
sleep uiSleep waitUntil call spawn execVM isRemoteExecuted isRemoteExecutedJIP remoteExecutedOwner isUIContext isGamePaused isGameFocused

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