params: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(see also)
(description)
Line 7: Line 7:
____________________________________________________________________________________________
____________________________________________________________________________________________


| Parses array of params into array of [[private]] variables, similar to [[BIS_fnc_param]]. 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.  
| Parses input argument into array of [[private]] variables, similar to [[BIS_fnc_param]]. 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.
<br>The values of passed argument array would be assigned to private variables given in the format array and respectful of their order in the array. To skip some values, give an empty string for a private variable name in format array.<br>
<br><br>
To force variable value to a predefined value when argument is undefined, of a wrong type or length, use [variableName, defaultValue] format. To make sure passed value is of a specific type, supply the desired [[Data Types]] values in addition. If input is array, it is also possible to validate the length of the passed array.<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. Substitutes can also be logged into ''.rpt'' file, if [[Description.ext#allowFunctionsLog | allowFunctionsLog]] param in [[description.ext]] is set to 1.
If any of the processed input is undefined, of the wrong type or wrong length, default value is used instead. To log these substitutes into ''.rpt'' file, set [[Description.ext#allowFunctionsLog | allowFunctionsLog]] param in [[description.ext]] to 1.<br>
See also: [[typeName]]
{{warning | W.I.P.}}  
{{warning | W.I.P.}}  
|= Description
|= Description
Line 21: Line 19:
|p1= [element1, element2,...elementN]: [[Array]] - format array|=
|p1= [element1, element2,...elementN]: [[Array]] - format array|=
|p2= elementN: [[String]] or [[Array]]
|p2= elementN: [[String]] or [[Array]]
<br> [[String]] 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")
<br> [[Array]] could be one of the following constructs:
<br> If [[Array]] then it should be one of the following constructs:
* [variableName, defaultValue]
* [variableName, defaultValue]
* [variableName, defaultValue, expectedDataTypes]
* [variableName, defaultValue, expectedDataTypes]
* [variableName, defaultValue, expectedDataTypes, expectedArrayLength]
* [variableName, defaultValue, expectedDataTypes, expectedArrayCount]
''variableName'': [[String]] - name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
<dd class="param"><span style="padding-left:20px">''variableName'': [[String]] - name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")</span></dd>
<br> ''defaultValue'': [[Anything]] - check if passed value is defined, if not (or [[nil]]), use this value.
<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>
<br> ''expectedDataTypes'': [[Array]] of direct [[Data Types]] - check if passed value is of listed [[Data Types]]. If not, use default value supplied. Empty array [] means every data type is ok.
<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>
<br> ''expectedArrayLength'': [[Number]] - if passed value is array, check that it is of a certain length. If not, use default value supplied.|=
<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>|=
| [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |= Return value
| [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |= Return value


Line 37: Line 35:
|p22= [element1, element2,...elementN]: [[Array]] - format array|=
|p22= [element1, element2,...elementN]: [[Array]] - format array|=
|p23= elementN: [[String]] or [[Array]]:
|p23= elementN: [[String]] or [[Array]]:
<br> [[String]] 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")
<br> [[Array]] could be one of the following constructs:
<br> If [[Array]] then it should be one of the following constructs:
* [variableName, defaultValue]
* [variableName, defaultValue]
* [variableName, defaultValue, expectedDataTypes]
* [variableName, defaultValue, expectedDataTypes]
* [variableName, defaultValue, expectedDataTypes, expectedArrayLength]
* [variableName, defaultValue, expectedDataTypes, expectedArrayCount]
''variableName'': [[String]] - name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")
<dd class="param"><span style="padding-left:20px">''variableName'': [[String]] - name of a [[private]] variable (must begin with underscore _, e.g. "_myVar")</span></dd>
<br> ''defaultValue'': [[Anything]] - check if passed value is defined, if not (or [[nil]]), use this value.
<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>
<br> ''expectedDataTypes'': [[Array]] of direct [[Data Types]] - check if passed value is of listed [[Data Types]]. If not, use default value supplied. Empty array [] means every data type is ok.
<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>
<br> ''expectedArrayLength'': [[Number]] - if passed value is array, check that it is of a certain length. If not, use default value supplied.|=
<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>|=
| r2 = [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |= Return value
| r2 = [[Boolean]] - [[false]] if error occurred or default value has been used, otherwise [[true]] |= Return value
____________________________________________________________________________________________
____________________________________________________________________________________________
Line 99: Line 97:
["_var1", 0, [0]],
["_var1", 0, [0]],
["_var2", "", [""]],
["_var2", "", [""]],
["_var3", [0,0,0], <nowiki>[</nowiki>[], [[objNull]], 0], 3]
["_var3", [0,0,0], <nowiki>[</nowiki>[], [[objNull]], 0], [2,3]]
]) [[exitWith]] {};
]) [[exitWith]] {};
[[hint]] "ok";
[[hint]] "ok";

Revision as of 22:33, 9 June 2015

-wrong parameter ("arma3dev") defined!-[[:Category:Introduced with arma3dev version 1.47|1.47]]
Hover & click on the images for description

Description

Description:
Parses input argument into array of private variables, similar to BIS_fnc_param. 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. Substitutes can also be logged into .rpt file, if allowFunctionsLog param in description.ext is set to 1.
W.I.P.
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 value 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] [1, 2, 3] call { if (!params ["_var1", "_var2", ["_var3", true, [true]]) exitWith {}; hint str [_var1, _var2, _var3]; }; // Error! Type Number, expected BOOL
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:
targ addEventHandler ["HitPart", { _this select 0 params ["_target", "_shooter", "_projectile"]; }];

Additional Information

See also:
paramselectsetresizereverseinfindtoArraytoStringforEachcountdeleteAtdeleteRangeappendsortBIS_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

[[Category:Introduced with arma3dev version 1.47]][[ Category: arma3dev: New Scripting Commands | PARAMS]][[ Category: arma3dev: Scripting Commands | PARAMS]]