params: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>") |
Lou Montana (talk | contribs) m (Text replacement - "↵↵</sqf>" to " </sqf>") |
||
Line 179: | Line 179: | ||
params [["_array", [], [[]], 2]]; | params [["_array", [], [[]], 2]]; | ||
}; // Succeeds | }; // Succeeds | ||
</sqf> | </sqf> | ||
Revision as of 19:48, 3 September 2024
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.54, onscreen errors are displayed for when the input is of the wrong type or size. - Groups:
- VariablesArrays
Syntax
- Syntax:
- params [element1, element2, ...]
- Parameters:
- elementN: String or Array
- String:name of the private variable (must begin with underscore _, e.g. "_myVar")
- Array format [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: Array of direct Data Types - (Optional) 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: Number or Array - (Optional) 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, ...]
- 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
- elementN: String or Array
- String:name of the private variable (must begin with underscore _, e.g. "_myVar")
- Array format [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: Array of direct Data Types - (Optional) 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: Number or Array - (Optional) 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:
- Example 2:
- Example 3:
- Example 4:
- 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 8:
- 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:
- param select # _this isEqualTypeAll isEqualType isEqualTypeParams isEqualTypeArray isEqualTypeAny
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 2016-11-03 - 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: But when the one Parameter is an array that parameter has to be inside of an array when the function is called
Example: - Posted on 2019-07-04 - 16:54 (UTC)
- 7erra
-
It is valid to redefine the _this variable and use params again like this:
- Posted on Nov 01, 2021 - 16:00 (UTC)
-
Here's how to validate HashMap parameters:
_myHashMap = createHashMapFromArray [["a",1],["b",2],["c",3]]; [_myHashMap] call { params [["_theHashMap",createHashMap,[createHashMap]]]; };