{"name":"with","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","games":{"arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"arma2 1.00","arma3_since":"0.50","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","params":[{"name":"namespace","type":"Namespace","type_expr":"Namespace"}],"returns":{"type":"With Type","type_expr":"With Type"},"form":"unary","right":{"text":"namespace"}}],"examples":[{"code":["_myWithType = with uiNamespace;"]},{"code":["with missionNamespace do { global = global + 1 };"]}],"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>``` |","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."}]}