{"name":"with","operator":null,"title":"with","type":"command","url":"https://community.bistudio.com/wiki/with","revid":369023,"updated":"2024-08-26T13:58:14Z","groups":["Program Flow","Namespaces"],"status":"ok","replaced_by":[],"games":[{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"arma2","name":"Arma 2","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Creates a With Type that is used inside a do construct in order to execute code inside a given namespace.\n\n> [!IMPORTANT]\n> To avoid the below-mentioned issue error (whether in scheduled or unscheduled environment) use setVariable/getVariable with the desired Namespace.","syntaxes":[{"syntax":"with namespace","since":null,"effect":null,"exec":null,"params":[{"name":"namespace","type":"Namespace","optional":false,"default":null,"description":null,"types":["Namespace"],"since":null,"text":"namespace: Namespace"}],"returns":{"type":"With Type","types":["With Type"],"description":null,"text":"With Type"},"form":"unary","operator":null,"left":null,"right":{"text":"namespace","types":["Namespace"]}}],"examples":[{"text":"```sqf\n_myWithType = with uiNamespace;\n```","code":["_myWithType = with uiNamespace;"]},{"text":"```sqf\nwith missionNamespace do { global = global + 1 };\n```","code":["with missionNamespace do { global = global + 1 };"]}],"multiplayer":null,"problems":"When used in a do-construct in scripts with allowed suspension, a namespace switching might unexpectedly occur inside some scopes (for, if, try, call) started in a scheduled environment after small suspension if with was not the main scope; however, the issue does not arise if with is used in the parent scope:\n\n| namespace switch risk | namespace switch safe |\n|---|---|\n| ```sqf<br>0 spawn<br>{<br>    with uiNamespace do<br>    {<br>        for \"_i\" from 1 to 1 do <br>        {<br>            systemChat str [<br>                currentNamespace isEqualTo uiNamespace, <br>                currentNamespace isEqualTo missionNamespace<br>            ];<br>            // result [true, false]<br>            sleep 0.05; // <-- small suspension<br>            systemChat str [<br>                currentNamespace isEqualTo uiNamespace, <br>                currentNamespace isEqualTo missionNamespace<br>            ];<br>            // result [false, true] <-- switching<br>        };<br>    };<br>};<br>``` | ```sqf<br>with uiNamespace do<br>{<br>    0 spawn<br>    {<br>        for \"_i\" from 1 to 1 do <br>        {<br>            systemChat str [<br>                currentNamespace isEqualTo uiNamespace, <br>                currentNamespace isEqualTo missionNamespace<br>            ];<br>            // result [true, false]<br>            sleep 0.05; // <-- small suspension<br>            systemChat str [<br>                currentNamespace isEqualTo uiNamespace, <br>                currentNamespace isEqualTo missionNamespace<br>            ];<br>            // result [true, false] <-- NO switching<br>        };<br>    };<br>};<br>``` |","aliases":[],"see_also":["missionNamespace","uiNamespace","parsingNamespace","currentNamespace"],"notes":[{"user":"Hypoxic125","date":"2023-02-23T07:05:05Z","text":"**Using with:**\n\n```sqf\nwith profileNamespace do {\n    myVariable = \"Hello!\";\n};\n```\n\n**is the same as:**\n\n```sqf\nprofileNamespace setVariable [\"myVariable\", \"Hello!\"];\n```\n\nThis allows you to quickly modify/use variables within a namespace without using **get/setvariable** every time."}],"categories":["Arma 2: New Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Namespaces","Command Group: Program Flow","Introduced with Arma 2 version 1.00","Scripting Commands","Take On Helicopters: Scripting Commands"]}