with: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(adapted page by stating with as an operator on it's own)
mNo edit summary
 
(53 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma2 |= Game name
|game1= arma2
|version1= 1.00


|1.00|= Game version
|game2= arma2oa
____________________________________________________________________________________________
|version2= 1.50


| Creates a [[With Type]] that is used inside a [[do]] construct in order to execute code inside a given namespace.  
|game3= tkoh
{{warning | when used in a do-construct in scripts with allowed suspension, an unexpected namespace switch could happen (see note below)}}
|version3= 1.00
'''NOTE''' for the reasons unknown, namespace switching might unexpectedly occur inside some scopes ([[for]], [[if]], [[try]], [[call]]) started in scheduled scripts ([[canSuspend]] true) after small suspension if [[with]] was not the main scope. For example:  
 
<code>[] [[spawn]]
|game4= arma3
|version4= 0.50
 
|gr1= Program Flow
|gr2= Namespaces
 
|descr= Creates a [[With Type]] that is used inside a [[do]] construct in order to execute code inside a given namespace.
{{Feature|important|To avoid the below-mentioned issue error (whether in [[Scheduler#Scheduled Environment|scheduled]] or [[Scheduler#Unscheduled Environment|unscheduled]] environment) use [[setVariable]]/[[getVariable]] with the desired [[Namespace]].}}
 
|pr= 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 [[Scheduler#Scheduled Environment|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:
{{{!}} class="wikitable"
! namespace switch risk
! namespace switch safe
{{!}}-
{{!}}
<sqf>] spawn
{
{
[[with]] [[uiNamespace]] do  
with uiNamespace do
{
{
[[for]] "_i" [[from]] 1 [[to]] 1 [[do]]
for "_i" from 1 to 1 do  
{
{
[[systemChat]] [[str]] [
systemChat str [
[[currentNamespace]] [[isEqualTo]] [[uiNamespace]],  
currentNamespace isEqualTo uiNamespace,  
[[currentNamespace]] [[isEqualTo]] [[missionNamespace]]
currentNamespace isEqualTo missionNamespace
];  
];
// result [true, false]
// result [true, false]
[[sleep]] 0.05; //<-- small suspension
sleep 0.05; // <-- small suspension
[[systemChat]] [[str]] [
systemChat str [
[[currentNamespace]] [[isEqualTo]] [[uiNamespace]],  
currentNamespace isEqualTo uiNamespace,  
[[currentNamespace]] [[isEqualTo]] [[missionNamespace]]
currentNamespace isEqualTo missionNamespace
];  
];
// result [false, true] <-- switching
// result [false, true] <-- switching
};
};
};
};
};</code>
};
However if [[with]] used in parent scope, everything works correctly:
</sqf>
<code>[[with]] [[uiNamespace]] do
{{!}}
<sqf>
with uiNamespace do
{
{
[] [[spawn]]
[] spawn
{
{
[[for]] "_i" [[from]] 1 [[to]] 1 [[do]]
for "_i" from 1 to 1 do  
{
{
[[systemChat]] [[str]] [
systemChat str [
[[currentNamespace]] [[isEqualTo]] [[uiNamespace]],  
currentNamespace isEqualTo uiNamespace,  
[[currentNamespace]] [[isEqualTo]] [[missionNamespace]]
currentNamespace isEqualTo missionNamespace
];  
];
// result [true, false]
// result [true, false]
[[sleep]] 0.05; //<-- small suspension
sleep 0.05; // <-- small suspension
[[systemChat]] [[str]] [
systemChat str [
[[currentNamespace]] [[isEqualTo]] [[uiNamespace]],  
currentNamespace isEqualTo uiNamespace,  
[[currentNamespace]] [[isEqualTo]] [[missionNamespace]]
currentNamespace isEqualTo missionNamespace
];  
];
// result [true, false] <-- NO switching
// result [true, false] <-- NO switching
};
};
};
};
};</code>
};
To eliminate possibility of error you can also use [[setVariable]] or [[getVariable]] with desired [[Namespace]], which is also scheduled environment save.
</sqf>
|= Description
{{!}}}
____________________________________________________________________________________________


| [[with]] namespace |=Syntax
|s1= [[with]] namespace


|p1= namespace: [[Namespace]] |= Parameter 1
|p1= namespace: [[Namespace]]


| [[With Type]] |= Return value
|r1= [[With Type]]
____________________________________________________________________________________________


|x1= <code> _myWithType = [[with]] [[uiNamespace]]; </code> |=Example 1
|x1= <sqf>_myWithType = with uiNamespace;</sqf>
|x2= <code>[[with]] [[missionNamespace]] [[do]] {global = global + 1};</code> |=Example 2
____________________________________________________________________________________________


| [[ArmA:_Editing#Forward_Compatibility|Forward Compatibility]], [[missionNamespace]], [[uiNamespace]], [[parsingNamespace]] |= See also
|x2= <sqf>with missionNamespace do { global = global + 1 };</sqf>


|seealso= [[missionNamespace]] [[uiNamespace]] [[parsingNamespace]] [[currentNamespace]]
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= Hypoxic125
<!-- Note Section BEGIN -->
|timestamp= 20230223070505
<!-- Note Section END -->
|text= '''Using with:'''
</dl>
<sqf>
with profileNamespace do {
myVariable = "Hello!";
};
</sqf>
'''is the same as:'''
<sqf>
profileNamespace setVariable ["myVariable", "Hello!"];
</sqf>


<h3 style="display:none">Bottom Section</h3>
This allows you to quickly modify/use variables within a namespace without using '''get/setvariable''' every time.
 
}}
[[Category:Scripting Commands|WITH]]
[[Category:Command Group: Program Flow|WITH]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]

Latest revision as of 09:05, 23 February 2023

Hover & click on the images for description

Description

Description:
Creates a With Type that is used inside a do construct in order to execute code inside a given namespace.
To avoid the below-mentioned issue error (whether in scheduled or unscheduled environment) use setVariable/getVariable with the desired Namespace.
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:
namespace switch risk namespace switch safe

] spawn { with uiNamespace do { for "_i" from 1 to 1 do { systemChat str [ currentNamespace isEqualTo uiNamespace, currentNamespace isEqualTo missionNamespace ]; // result [true, false] sleep 0.05; // <-- small suspension systemChat str [ currentNamespace isEqualTo uiNamespace, currentNamespace isEqualTo missionNamespace ]; // result [false, true] <-- switching }; }; };

with uiNamespace do { [] spawn { for "_i" from 1 to 1 do { systemChat str [ currentNamespace isEqualTo uiNamespace, currentNamespace isEqualTo missionNamespace ]; // result [true, false] sleep 0.05; // <-- small suspension systemChat str [ currentNamespace isEqualTo uiNamespace, currentNamespace isEqualTo missionNamespace ]; // result [true, false] <-- NO switching }; }; };

Groups:
Program FlowNamespaces

Syntax

Syntax:
with namespace
Parameters:
namespace: Namespace
Return Value:
With Type

Examples

Example 1:
_myWithType = with uiNamespace;
Example 2:
with missionNamespace do { global = global + 1 };

Additional Information

See also:
missionNamespace uiNamespace parsingNamespace currentNamespace

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
Hypoxic125 - c
Posted on Feb 23, 2023 - 07:05 (UTC)
Using with:
with profileNamespace do { myVariable = "Hello!"; };
is the same as:
profileNamespace setVariable ["myVariable", "Hello!"];
This allows you to quickly modify/use variables within a namespace without using get/setvariable every time.