params: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "|= Game version" to "|Game version=")
Line 11: Line 11:
In addition to simple parsing directly into variables, input can be tested in case it is undefined, of the wrong type or of the wrong size (if array) and substituted if necessary with default values. Since Arma 3 v1.53.132691, onscreen errors are displayed for when the input is of the wrong type or size.<br><br>
In addition to simple parsing directly into variables, input can be tested in case it is undefined, of the wrong type or of the wrong size (if array) and substituted if necessary with default values. Since Arma 3 v1.53.132691, onscreen errors are displayed for when the input is of the wrong type or size.<br><br>


{{Important | All variables names must start with underscore and be enclosed in quotes: <tt>[[params]] [<div style{{=}}"color:red;font-weight:bold;display:inline;">"_</div>myVar1<div style{{=}}"color:red;font-weight:bold;display:inline;">"</div>, <div style{{=}}"color:red;font-weight:bold;display:inline;">"_</div>myVar2<div style{{=}}"color:red;font-weight:bold;display:inline;">"</div>];</tt>
{{Important | All variables names must start with underscore and be enclosed in quotes: <tt>[[params]] [<span style{{=}}"color:red;font-weight:bold">"_</span>myVar1<span style{{=}}"color:red;font-weight:bold">"</span>, <span style{{=}}"color:red;font-weight:bold">"_</span>myVar2<span style{{=}}"color:red;font-weight:bold">"</span>];</tt>
}}
}}
|DESCRIPTION=
|DESCRIPTION=
Line 19: Line 19:




|p1= [element1, element2,...elementN]: [[Array]] - format array|=
|p1= [element1, element2,...elementN]: [[Array]] - format array|Parameter 1=
 
|p2= elementN: [[String]] or [[Array]]
|p2= elementN: [[String]] or [[Array]]
<br> If [[String]] then it is the name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
<br> If [[String]] then it is the name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
Line 32: Line 33:
| [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |RETURNVALUE=
| [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |RETURNVALUE=


| s2= argument [[params]] [element1, element2,...elementN] |SYNTAX=
| s2= argument [[params]] [element1, element2,...elementN] |SYNTAX2=


|p21= argument: [[Anything]] - A usual array of params is expected. If a non-array argument is passed, it will be converted to 1 element array |=
|p21= argument: [[Anything]] - A usual array of params is expected. If a non-array argument is passed, it will be converted to 1 element array |Parameter 21=
|p22= [element1, element2,...elementN]: [[Array]] - format array|=
|p22= [element1, element2,...elementN]: [[Array]] - format array|Parameter 22=
|p23= elementN: [[String]] or [[Array]]:
|p23= elementN: [[String]] or [[Array]]:
<br> If [[String]] then it is the name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
<br> If [[String]] then it is the name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
Line 45: Line 46:
<dd class="param"><span style="padding-left:20px">''defaultValue'': [[Anything]] - default value to return if input element is undefined, of the wrong type or of the wrong size (if array).</span></dd>
<dd class="param"><span style="padding-left:20px">''defaultValue'': [[Anything]] - default value to return if input element is undefined, of the wrong type or of the wrong size (if array).</span></dd>
<dd class="param"><span style="padding-left:20px">''expectedDataTypes'' (Optional): [[Array]] of direct [[Data Types]] -  checks if passed value is one of listed Data Types. If not, default value is used instead. Empty array [] means every data type is accepted.</span></dd>
<dd class="param"><span style="padding-left:20px">''expectedDataTypes'' (Optional): [[Array]] of direct [[Data Types]] -  checks if passed value is one of listed Data Types. If not, default value is used instead. Empty array [] means every data type is accepted.</span></dd>
<dd class="param"><span style="padding-left:20px">''expectedArrayCount'' (Optional): [[Number]] or [[Array]]  - a single size or array of sizes. If passed input value is an array, checks that it has a certain number of elements. If not, default value is used instead. Empty array [] means any size is accepted.</span></dd>|=
<dd class="param"><span style="padding-left:20px">''expectedArrayCount'' (Optional): [[Number]] or [[Array]]  - a single size or array of sizes. If passed input value is an array, checks that it has a certain number of elements. If not, default value is used instead. Empty array [] means any size is accepted.</span></dd>|Parameter 23=
| r2 = [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |RETURNVALUE=
|r2 = [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |RETURNVALUE2=
____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
Line 54: Line 55:
_two = _this [[select]] 1;
_two = _this [[select]] 1;
_three = _this [[select]] 2;
_three = _this [[select]] 2;
// .....
{{cc|...}}
};
};


// Same as above, only using params
{{cc|Same as above, only using params}}
[1, 2, 3] [[call]] {
[1, 2, 3] [[call]] {
[[params]] ["_one", "_two", "_three"];
[[params]] ["_one", "_two", "_three"];
// .....
{{cc|...}}
};</code> |=
};</code> |Example 1=


|x2= <code>[123] [[call]] {
|x2= <code>[123] [[call]] {
Line 67: Line 68:
};
};


// Below would produce the same result as above
{{cc|Below would produce the same result as above}}
123 [[call]] {
123 [[call]] {
[[params]] ["_myvar"];
[[params]] ["_myvar"];
};</code> |=
};</code> |Example 2=


|x3= <code>[[position]] [[player]] [[params]] ["", "", "_z"];
|x3= <code>[[position]] [[player]] [[params]] ["", "", "_z"];
[[if]] (_z > 10) [[then]] {
[[if]] (_z > 10) [[then]] {
[[hint]] "YOU ARE FLYING!";
[[hint]] "YOU ARE FLYING!";
};</code> |=
};</code> |Example 3=


|x4= <code>[1, [[nil]], 2] [[params]] ["_var1", "_var2", "_var3"];
|x4= <code>[1, [[nil]], 2] [[params]] ["_var1", "_var2", "_var3"];
// All 3 variables are made private but only _var1 and _var3 are defined
{{cc|All 3 variables are made private but only _var1 and _var3 are defined}}


[1, [[nil]], 2] [[params]] ["_var1", ["_var2", 23], "_var3"];
[1, [[nil]], 2] [[params]] ["_var1", ["_var2", 23], "_var3"];
// All 3 variables are private and defined</code> |=
{{cc|All 3 variables are private and defined}}</code> |Example 4=


|x5= <code>[1, 2] [[call]] {
|x5= <code>[1, 2] [[call]] {
Line 88: Line 89:
};
};
};
};
// The hint shows [1,2,true]
{{cc|The hint shows [1,2,true]}}
//Script exits, default value was used due to missing value
{{cc|Script exits, default value was used due to missing value}}


[1, 2, 3] [[call]] {
[1, 2, 3] [[call]] {
Line 96: Line 97:
};
};
};
};
// The hint shows [1,2,true]
{{cc|The hint shows [1,2,true]}}
//Script exits, default value was used due incorrect value type</code> |=
{{cc|Script exits, default value was used due incorrect value type}}</code> |Example 5=


|x6= <code>[1, "ok", [1, 2, 3]] [[call]] {
|x6= <code>[1, "ok", [1, 2, 3]] [[call]] {
Line 107: Line 108:
[[hint]] "ok";
[[hint]] "ok";
};
};
// Passes validation
{{cc|Passes validation}}


[1, 2, [3, 4, 5]] [[call]] {
[1, 2, [3, 4, 5]] [[call]] {
Line 113: Line 114:
[[hint]] "ok";
[[hint]] "ok";
};
};
// Fails, because passed array is expected to be of 0 length, i.e. empty</code> |=
{{cc|Fails, because passed array is expected to be of 0 length, i.e. empty}}</code> |Example 6=
|x7= <code>[[position]] [[player]] [[params]] ["_x", "_y"];  
|x7= <code>[[position]] [[player]] [[params]] ["_x", "_y"];  
[[player]] [[setPos]] [_x, _y, 100];</code> |=
[[player]] [[setPos]] [_x, _y, 100];</code> |Example 7=


|x8= <code>[1, 2, 3, [4, 5, 6]] [[call]] {
|x8= <code>[1, 2, 3, [4, 5, 6]] [[call]] {
[[params]] ["_one", "_two", "_three"];
[[params]] ["_one", "_two", "_three"];
_this [[select]] 3 [[params]] ["_four", "_five", "_six"];
_this [[select]] 3 [[params]] ["_four", "_five", "_six"];
};</code> |=
};</code> |Example 8=


|x9= <code>{
|x9= <code>{
_x [[params]] ["_group", "_index"];
_x [[params]] ["_group", "_index"];
// .....
{{cc|...}}
} [[forEach]] [[waypoints]] [[group]] [[player]];
} [[forEach]] [[waypoints]] [[group]] [[player]];


fn_someFnc = {
fn_someFnc = {
[[params]] ["_position", ["_direction", 0], ["_name", ""]];
[[params]] ["_position", ["_direction", 0], ["_name", ""]];
// Extract the x, y, and z from "_position" array:
{{cc|Extract the x, y, and z from "_position" array:}}
_position [[params]] ["_x", "_y", "_z"];
_position [[params]] ["_x", "_y", "_z"];
// .....
{{cc|...}}
};
};


<nowiki>[</nowiki>[[position]] [[player]], [[direction]] [[player]], [[name]] [[player]]] [[call]] fn_someFnc;</code> |=
<nowiki>[</nowiki>[[position]] [[player]], [[direction]] [[player]], [[name]] [[player]]] [[call]] fn_someFnc;</code> |Example 9=




|x10= <code>targ [[addEventHandler]] ["HitPart", {
|x10= <code>[[player]] [[addEventHandler]] ["HitPart", {
_this [[select]] 0 [[params]] ["_target", "_shooter", "_projectile"];
_this [[select]] 0 [[params]] ["_target", "_shooter", "_projectile"];
}];</code> |=
}];</code> |Example 10=
____________________________________________________________________________________________
____________________________________________________________________________________________


Line 186: Line 187:
<code>[1,2] call {
<code>[1,2] call {
params [ ["_array", [], [[]], 2] ];
params [ ["_array", [], [[]], 2] ];
};//Fails
}; {{cc|Fails}}


<nowiki>[[1,2]]</nowiki> call {
<nowiki>[[1,2]]</nowiki> call {
params [ ["_array", [], [[]], 2] ];
params [ ["_array", [], [[]], 2] ];
};//Succeeds</code>
}; {{cc|Succeeds}}</code>


</dd>
</dd>

Revision as of 15:11, 2 September 2019

Hover & click on the images for description

Description

Description:
Parses input argument into array of private variables. When used without argument, as shown in main syntax, internal variable _this, which is usually available inside functions and event handlers, is used as argument.

In addition to simple parsing directly into variables, input can be tested in case it is undefined, of the wrong type or of the wrong size (if array) and substituted if necessary with default values. Since Arma 3 v1.53.132691, onscreen errors are displayed for when the input is of the wrong type or size.

All variables names must start with underscore and be enclosed in quotes: params ["_myVar1", "_myVar2"];
Groups:
Uncategorised

Syntax

Syntax:
params [element1, element2,...elementN]
Parameters:
[element1, element2,...elementN]: Array - format array
elementN: String or Array
If String then it is the name of a private variable (must begin with underscore _, e.g. "_myVar")
If Array then it should be one of the following constructs:
  • [variableName, defaultValue]
  • [variableName, defaultValue, expectedDataTypes]
  • [variableName, defaultValue, expectedDataTypes, expectedArrayCount]
variableName: String - name of a private variable (must begin with underscore _, e.g. "_myVar")
defaultValue: Anything - default value to return if input element is undefined, of the wrong type or of the wrong size (if array).
expectedDataTypes (Optional): Array of direct Data Types - checks if passed value is one of listed Data Types. If not, default value is used instead. Empty array [] means every data type is accepted.
expectedArrayCount (Optional): Number or Array - a single size or array of sizes. If passed input value is an array, checks that it has a certain number of elements. If not, default value is used instead. Empty array [] means any size is accepted.
Return Value:
Boolean - false if error occurred or default value has been used, otherwise true

Alternative Syntax

Syntax:
argument params [element1, element2,...elementN]
Parameters:
argument: Anything - A usual array of params is expected. If a non-array argument is passed, it will be converted to 1 element array
[element1, element2,...elementN]: Array - format array
elementN: String or Array:
If String then it is the name of a private variable (must begin with underscore _, e.g. "_myVar")
If Array then it should be one of the following constructs:
  • [variableName, defaultValue]
  • [variableName, defaultValue, expectedDataTypes]
  • [variableName, defaultValue, expectedDataTypes, expectedArrayCount]
variableName: String - name of a private variable (must begin with underscore _, e.g. "_myVar")
defaultValue: Anything - default value to return if input element is undefined, of the wrong type or of the wrong size (if array).
expectedDataTypes (Optional): Array of direct Data Types - checks if passed value is one of listed Data Types. If not, default value is used instead. Empty array [] means every data type is accepted.
expectedArrayCount (Optional): Number or Array - a single size or array of sizes. If passed input value is an array, checks that it has a certain number of elements. If not, default value is used instead. Empty array [] means any size is accepted.
Return Value:
Boolean - false if error occurred or default value has been used, otherwise true

Examples

Example 1:
[1, 2, 3] call { private ["_one", "_two", "_three"]; _one = _this select 0; _two = _this select 1; _three = _this select 2; // ... }; // Same as above, only using params [1, 2, 3] call { params ["_one", "_two", "_three"]; // ... };
Example 2:
[123] call { params ["_myvar"]; }; // Below would produce the same result as above 123 call { params ["_myvar"]; };
Example 3:
position player params ["", "", "_z"]; if (_z > 10) then { hint "YOU ARE FLYING!"; };
Example 4:
[1, nil, 2] params ["_var1", "_var2", "_var3"]; // All 3 variables are made private but only _var1 and _var3 are defined [1, nil, 2] params ["_var1", ["_var2", 23], "_var3"]; // All 3 variables are private and defined
Example 5:
[1, 2] call { if (!params ["_var1", "_var2", ["_var3", true, [true]]]) exitWith { hint str [_var1, _var2, _var3]; }; }; // The hint shows [1,2,true] // Script exits, default value was used due to missing value [1, 2, 3] call { if (!params ["_var1", "_var2", ["_var3", true, [true]]]) exitWith { hint str [_var1, _var2, _var3]; }; }; // The hint shows [1,2,true] // Script exits, default value was used due incorrect value type
Example 6:
[1, "ok", [1, 2, 3]] call { if (!params [ ["_var1", 0, [0]], ["_var2", "", [""]], ["_var3", [0,0,0], [[], objNull, 0], [2,3]] ]) exitWith {}; hint "ok"; }; // Passes validation [1, 2, [3, 4, 5]] call { if (!params ["_var1", "_var2", ["_var3", [], [[], objNull, 0], 0]]) exitWith {}; hint "ok"; }; // Fails, because passed array is expected to be of 0 length, i.e. empty
Example 7:
position player params ["_x", "_y"]; player setPos [_x, _y, 100];
Example 8:
[1, 2, 3, [4, 5, 6]] call { params ["_one", "_two", "_three"]; _this select 3 params ["_four", "_five", "_six"]; };
Example 9:
{ _x params ["_group", "_index"]; // ... } forEach waypoints group player; fn_someFnc = { params ["_position", ["_direction", 0], ["_name", ""]]; // Extract the x, y, and z from "_position" array: _position params ["_x", "_y", "_z"]; // ... }; [position player, direction player, name player] call fn_someFnc;
Example 10:
player addEventHandler ["HitPart", { _this select 0 params ["_target", "_shooter", "_projectile"]; }];

Additional Information

See also:
paramselectsetresizereverseinfindfindIftoArraytoStringforEachcountdeleteAtdeleteRangeappendsortarrayIntersectsplitStringjoinStringisEqualTypeAllisEqualTypeisEqualTypeParamsisEqualTypeArrayisEqualTypeAnytypeNameBIS_fnc_param

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
Posted on August 30, 2015 - 21:59 (UTC)
Killzone Kid
Because params doubles as private, instead of: [1, 2, 3] params ["_one", "_two", "_three"]; private "_four"; _four = 4; You can just: [1, 2, 3] params ["_one", "_two", "_three", "_four"]; _four = 4;
Posted on November 3, 2016 - 04:07 (UTC)
Dedmen
With a function only taking one Parameter, it doesn't matter whether the parameter is in an array or not:
Example: 1 call { params [ ["_number",0, [0]] ]; }; or [1] call { params [ ["_number",0, [0]] ]; }; But when the one Parameter is an array that parameter has to be inside of an array when the function is called
Example: [1,2] call { params [ ["_array", [], [[]], 2] ]; }; // Fails [[1,2]] call { params [ ["_array", [], [[]], 2] ]; }; // Succeeds
Posted on July 4, 2019 - 16:54 (UTC)
7erra
It is valid to redefine the _this variable and use params again like this:
[1, 2, [3, 4]] call { params ["_one", "_two", "_this"]; params ["_three", "_four"]; };