{"name":"canSuspend","operator":null,"title":"canSuspend","type":"command","url":"https://community.bistudio.com/wiki/canSuspend","revid":353403,"updated":"2024-03-17T20:47:33Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"1.58"}],"since":{"game":"arma3","name":"Arma 3","version":"1.58"},"arma3_since":"1.58","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Returns true if sleep, uiSleep or waitUntil commands can be used in the current scope.\nUsually 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\".\nUsing canSuspend command allows to detect the correct environment for the code.\n\n> [!NOTE]\n> The definition of scheduled and unscheduled environment is not the same as whether or not the script execution can or cannot be suspended.\n> For example while .*sqs* and .*fsm* scripts are scheduled (i.e. added to the scheduler:\n> 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.","syntaxes":[{"syntax":"canSuspend","since":null,"effect":null,"exec":null,"params":[],"returns":{"type":"Boolean","types":["Boolean"],"description":null,"text":"Boolean"},"form":"nullary","operator":null,"left":null,"right":null}],"examples":[{"text":"```sqf\nonEachFrame\n{\n    systemChat str canSuspend;            // false\n    0 spawn { hint str canSuspend };    // true\n    onEachFrame {};\n};\n```","code":["onEachFrame\n{\n\tsystemChat str canSuspend;\t\t\t// false\n\t0 spawn { hint str canSuspend };\t// true\n\tonEachFrame {};\n};"]},{"text":"Make sure the function code is always spawned even when called:\n\n```sqf\nprivate _sleepingCode =\n{\n    if (!canSuspend) exitWith { _this spawn _sleepingCode };\n    sleep _this;\n    hint (\"slept \" + str _this);\n};\n\n5 call _sleepingCode;\n```","code":["private _sleepingCode =\n{\n\tif (!canSuspend) exitWith { _this spawn _sleepingCode };\n\tsleep _this;\n\thint (\"slept \" + str _this);\n};\n\n5 call _sleepingCode;"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["sleep","uiSleep","waitUntil","call","spawn","execVM","isRemoteExecuted","isRemoteExecutedJIP","remoteExecutedOwner","isUIContext","isGamePaused","isGameFocused"],"notes":[],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Program Flow","Introduced with Arma 3 version 1.58","Scripting Commands"]}