Guest User

Untitled

a guest
Dec 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. /*
  2. Author: Karel Moricky
  3.  
  4. Description:
  5. Define script parameter
  6.  
  7. Parameter(s):
  8. _this select 0: ARRAY - list of params
  9. _this select 1: NUMBER - selected index
  10. _this select 2 (Optional): ANY - default param (used when param is missing or of wrong type)
  11. - you can overload default value by setting 'BIS_fnc_<functionName>_<index>'
  12. _this select 3 (Optional): ARRAY - list of allowed type examples (e.g. ["",[],0,objnull])
  13. _this select 4 (Optional): NUMBER - If value is ARRAY, checks if it has required number of elements
  14.  
  15. Returns:
  16. ANY - either value from list of params, or default value
  17. */
  18.  
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21. #define FNC_DISABLEERRORS \
  22. private ["_disableErrors"]; \
  23. _disableErrors = false;
  24.  
  25. // _disableErrors = if (count _this > 5) then {_this select 4} else {false};
  26.  
  27. #define FNC_TEXTTYPES \
  28. private ["_textTypes"];\
  29. _textTypes = "";\
  30. {\
  31. _textTypes = _textTypes + typename _x;\
  32. if (_forEachIndex < count _types - 1) then {_textTypes = _textTypes + ", "};\
  33. } foreach _types;
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  36.  
  37. private ["_params","_id","_value","_thisCount"];
  38. //disableserialization; //--- Do not put this here or none of the scripts where BIS_fnc_param is used will be serialized!
  39.  
  40. _thisCount = count _this;
  41. //if (typename _this != typename []) then {_this = [_this]};
  42. _params = if (_thisCount > 0) then {_this select 0} else {[]};
  43. _id = if (_thisCount > 1) then {_this select 1} else {0};
  44. if (typename _params != typename []) then {_params = [_params]};
  45. //if (typename _id != typename 00) then {_id = 0};
  46. _value = if (count _params > _id) then {_params select _id} else {nil};
  47.  
  48. //--- Assign default value
  49. if (_thisCount > 2) then {
  50. private ["_default","_defaultOverload","_types","_typeDefault","_type"];
  51. _default = _this select 2;
  52.  
  53. //--- Overload default value
  54. #ifndef DISABLE_REWRITE
  55. if !(isnil "_fnc_scriptName") then {
  56. _defaultOverload = missionnamespace getvariable (_fnc_scriptName + "_" + str _id);
  57. if !(isnil "_defaultOverload") then {
  58. _default = _defaultOverload;
  59. };
  60. };
  61. #endif
  62.  
  63. //--- Default set
  64. if (isnil "_value") then {
  65. _value = _default;
  66. };
  67.  
  68. //--- Check type
  69. if (_thisCount > 3) then {
  70. _types = _this select 3;
  71. //if (typename _types != typename []) then {_types = [_types]};
  72. _type = typename _value;
  73. _typeDefault = typename _default;
  74.  
  75. if !({_type == typename _x} count _types > 0) then {
  76.  
  77. if ({_typeDefault == typename _x} count _types > 0) then {
  78. FNC_DISABLEERRORS
  79. if (!_disableErrors) then {
  80. FNC_TEXTTYPES
  81. };
  82. _value = _default;
  83. } else {
  84. FNC_DISABLEERRORS
  85. if (!_disableErrors) then {
  86. FNC_TEXTTYPES
  87. };
  88. };
  89. };
  90. };
  91.  
  92. //--- Check number of elements (ARRAY type only)
  93. if (_thisCount > 4) then {
  94. if (typename _value == typename []) then {
  95. private ["_valueCountRequired","_valueCount"];
  96. _valueCountRequired = [_this,4,0,[0,[]]] call bis_fnc_param;
  97. if (typename _valueCountRequired != typename []) then {_valueCountRequired = [_valueCountRequired]};
  98. _valueCount = count _value;
  99. if !(_valueCount in _valueCountRequired) then {
  100. _value = _default;
  101. };
  102. };
  103. };
  104.  
  105. _value
  106. } else {
  107. if (isnil "_value") then {nil} else {_value}
  108. };
Add Comment
Please, Sign In to add comment