Jump to content

Recommended Posts

I'm going to learn how to use params in order to make future scripts more effective and you might learn something about params, too (please use the debug console if you want to follow along).

A simple example,

fnc_learnThis=
{ 
params ["_caller","_some","_thing","_luck"];

	if (_luck>0) exitWith {
		systemChat format ["%1", _caller]; 
		systemChat format ["%1 %2", _some, _thing];
	};
};

With some luck this call will prompt the functions to run with the parameters set,

[player, "is", "cool",1] call fnc_learnThis;


The next example shows how to select from an array and sub-array. We can create some helpers and make the orb jump between them. Make three sets so the result isn't binary.
Create the array of helpers from named objects like this,

Spoiler

helpers= [[help1_1, help1_2, help1_3], [help2_1, help2_2, help2_3], [help3_1, help3_2, help3_3]];

and an orb named "ufo" with "currentHelp=0" in it's init properties.


Enter the function,

Spoiler

you_fnc_help=
{ 
params ["_orb", "_helper"];

_helperGroup=helpers select _helper; //_helperGroup = select the first sub array from "helpers": [help1_1,help1_2,help1_3]

if (currentHelp==3) then {currentHelp=0};
		currentHelp=currentHelp+1;
		_nextHelp= _helperGroup select currentHelp; //select the next helper from the selected array
		_orb setpos (getpos _nextHelp);
};

 

and call the function with,

[orb, 0] call you_fnc_help;

The orb should continue to go around between the three helpers each time you enter the call. To change to the next helper group,

[orb, 1] call you_fnc_help;

More examples continue throughout the discussion below,

Have fun!

  • Like 3

Share this post


Link to post
Share on other sites

Okay. Ending day one with a (very) minor win,
 

Spoiler

fnc_learnThis=
{ 
	params ["_caller","_some","_thing","_luck"]; 
		if (_luck>0) exitWith {
			systemChat format ["%1", _caller]; 
			systemChat format ["%1 %2", _some, _thing];
			};
}; 
 
[player, "is", "cool",1] call fnc_learnThis;

 

Have fun!

  • Like 2

Share this post


Link to post
Share on other sites

Here's a look at MMF_fnc_soldiers script with params,
 

Spoiler

east_grp =["O_soldier_AR_f","O_soldier_AR_f","O_soldier_AT_f","O_heavyGunner_f","O_soldier_AT_f"];
west_grp =["B_soldier_AR_f","B_soldier_AR_f","B_soldier_AT_f","B_heavyGunner_f","B_soldier_AT_f"];

Destination=[baseMark1, baseMARK1, baseMARK2, baseMark3];


MMF_fnc_soldiers=
{
params ["_spawnLOC", "_spawnNUM", "_spawnSide", "_typeUnit", "_spawnState", "_goTo", "_applyIR"];

private _grpCount = _spawnSide countSide allUnits;
    if (_grpCount <20) exitWith {

private	_grp =[ getPos _spawnLOC, _spawnSIDE, _typeUnit, [],[],[],[],[_spawnNUM, 0],180] call BIS_fnc_spawnGroup;
        _grp setBehaviour _spawnState;
        _grp allowFleeing 0;
        _grp deleteGroupWhenEmpty true;
private	_wp = _grp addWaypoint [(getPos _goTo), 0];
        _wp setWaypointType "HOLD";
        newGroup=_grp;
        leadGroup=leader new_grp;
        if (_applyIR==1 && _spawnSide==WEST) exitWith {
            { 
		IRLight = "NVG_TargetC" createVehicle [0,0,0];
		_x removeMagazine "B_IR_Grenade";
		IRLight attachTo [_x, [0,-0.03,0.07], "LeftShoulder"];
            } forEach units _grp;
        };
    };
};

[baseMARK1, 1, WEST, west_grp, "SAFE", Destination select 1, 1] call MMF_fnc_soldiers;

 

Have fun!

  • Like 1

Share this post


Link to post
Share on other sites

If your making a framework then you really need to ensure all the end users parameters are correct, and supply your own descriptive error messages if not.

It needs to be as fool proof as possible. Some of the error checking below could also be expanded so as to supply detailed information to end user on what exactly was wrong.

 

Spoiler

 



MMF_fnc_soldiers = {

	//Must have paramters
	//If any of these params are missing( default used ) or are of the wrong type or size( ARRAY )
	//params command will return false
	if !( params[
		//VarName, default value, allowed value types, if array number of allowed indices
		[ "_spawnPos", objNull, [ [], objNull, grpNull, locationNull, "" ], [ 2, 3 ] ],
		[ "_spawnSide", sideUnknown, [ sideUnknown ] ],
		[ "_units", configNull, [ [], configNull ] ],
		[ "_wpPos", objNull, [ [], objNull, grpNull, locationNull, "" ], [ 2, 3 ] ]
	] ) exitWith {
		//Exit with error
		"Incorrect or missing parameters" call BIS_fnc_error;
		grpNull
	};

	//Turn possible Position, Object, Location, Group, Marker into a position
	_spawnPos = _spawnPos call BIS_fnc_position;
	//If position is world origin then a unsupported position was passed
	if ( _spawnPos isEqualTo [0,0,0] ) exitWith {
		"Bad position given for spawn position" call BIS_fnc_error;
		grpNull
	};

	//Same as above for _spawnPos
	_wpPos = _wpPos call BIS_fnc_position;
	if ( _wpPos isEqualTo [0,0,0] ) exitWith {
		"Bad position given for goto position" call BIS_fnc_error;
		grpNull
	};

	//Make sure we have a valid side
	if !( _spawnSide in [ east, west, independent, civilian ] ) exitWith {
		"Bad side given" call BIS_fnc_error;
		grpNull
	};

	//If we have passed a config entry for spawning
	//and it is not a class
	//or a valid CfgGroups entry
	if ( _units isEqualType configNull && { !isClass _units || { !( configName (( configHierarchy ( _units )) select 1 ) == "CfgGroups" ) } } ) exitWith {
		"Bad config given for unit array" call BIS_fnc_error;
		grpNull
	};

	//OR if we have passed a array of unit types
	//and any of the array indices are not STRINGs
	//Or are not of type AllVehicles
	//Or are not spawnable( scope )
	if ( _units isEqualType [] && { !( _units isEqualTypeAll "" ) || { _units findIf{ !( _x isKindOf "AllVehicles" ) || { !( getNumber( configFile >> "CfgVehicles" >> _x >> "scope" ) == 2 ) } } > -1 } } ) exitWith {
		"Bad unit type given" call BIS_fnc_error;
		grpNull
	};


	//Optional parameters index 4 onwards
	//count _this will automatically truncate the selected ARRAY to the number of available indices
	//Here we allow the default value if one has not been supplied
	_this select[ 4, count _this ] params[
		[ "_spawnCount", [1,0], [ [], 0 ], [ 2 ] ],
		[ "_spawnBehaviour", "AWARE", [ "" ] ],
		[ "_direction", 180, [ 0 ] ],
		[ "_cowardiceLevel", true, [ true, 0 ] ],
		[ "_applyIR", true, [ true ] ],
		[ "_condition", { true }, [ {} ] ]
	];

	//If the return from the supplied code is undefined or not of type bool or false
	_condition = call _condition;
	if ( isNil "_condition" || { !( _condition isEqualType true ) || { !_condition } } ) exitWith {
		diag_log "Spawn condition false";
		grpNull
	};

	//If _spawnCount is not an ARRAY
	if !( _spawnCount isEqualType [] ) then {
		//Turn it into one
		_spawnCount = [ _spawnCount, 0 ];
	};

	//If _spawnCount indices are not all of type NUMBER( SCALAR )
	//Or number of units to spawn is zero or less
	if ( !( _spawnCount isEqualTypeAll 0 ) || { _spawnCount select 0 <= 0 } ) exitWith {
		"Bad unit count given" call BIS_fnc_error;
		grpNull
	};

	//Make sure we have a valid group behaviour
	if !( toUpper _spawnBehaviour in [ "CARELESS", "SAFE", "AWARE", "COMBAT", "STEALTH" ] ) exitWith {
		"Bad state given for group behaviour" call BIS_fnc_error;
		grpNull
	};

	//If a boolean was used for _cowardiceLevel( allowFleeing ), turn it into a number
	if ( _cowardiceLevel isEqualType true ) then {
		_cowardiceLevel = parseNumber _cowardiceLevel;
	};

	//Phew!! all good lets spawn the group
	private _new_grp = [ _spawnPos, _spawnSide, _units, [],[],[],[], _spawnCount, _direction ] call BIS_fnc_spawnGroup;
	_new_grp setBehaviour _spawnBehaviour;
	_new_grp allowFleeing _cowardiceLevel;
	_new_grp deleteGroupWhenEmpty true;
	private _wp = _new_grp addWaypoint[ _wpPos, 0 ];
	_wp setWaypointType "HOLD";

	//And add shoulder IR
	if ( _applyIR ) then {
		{
			private _IRLight = "NVG_TargetC" createVehicle [0,0,0];
			_x removeMagazine "B_IR_Grenade";
			_IRLight attachTo[ _x, [0,-0.03,0.07], "LeftShoulder" ];
			//Save reference to shoulder IR on unit
			_x setVariable[ "IRLight", _IRLight ];
		}forEach units _new_grp;
	};

	_new_grp
};


//[
	//  spawn position,
	//  spawn side,
	//  units,
	//  initial waypoint position,
	//  spawn count,
	//  spawn behaviour,
	//  spawn direction,
	//  cowardice level
	//  apply IR
	//  spawn condition
//] call MMF_fnc_soldiers;


_westGroup = [ "B_soldier_AR_f", "B_soldier_AR_f", "B_soldier_AT_f", "B_heavyGunner_f", "B_soldier_AT_f" ];

_spawnedGroup = [
	baseMark1,
	west,
	_westGroup,
	"wpMarker",
	1,
	"AWARE",
	0,
	false,
	true,
	{ west countSide allUnits < 20 }
] call MMF_fnc_soldiers;

if !( isNull _spawnedGroup ) then {
	hint "Group spawned successfully";
};

 

 

Thoroughly untested. Hopefully will give you some ideas on improving the robustness of your frameworks functions and their parameters.

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

@Larrow,

Thanks!

Incorporating some kind of error-catcher was an eventuality I wasn't prepared for but with this and the debug console I should be able to come up with something.

That and keep the call as simple as possible.

Much appreciated as always,

have fun!

Added "_applyTAG" to only name if necessary (or join) and a killed EH to delete IR lights,
 

Spoiler

MMF_fnc_soldiers=
{
params ["_spawnLOC", "_spawnNUM", "_spawnSide", "_typeUnit", "_spawnState", "_goTo", "_applyIR", "_applyTAG"];

private _grpCount = _spawnSide countSide allUnits;

if (_grpCount <20) exitWith {
private	_grp =[ getPos _spawnLOC, _spawnSIDE, _typeUnit, [],[],[],[],[_spawnNUM, 0],180] call BIS_fnc_spawnGroup;
	_grp setBehaviour _spawnState;
	_grp allowFleeing 0;
	_grp deleteGroupWhenEmpty true;
private	_wp = _grp addWaypoint [(getPos _goTo), 0];
	_wp setWaypointType "HOLD";

	if (_applyTAG !=0) then {new_grp=_grp; lead_grp=leader new_grp;};
	if (_applyTAG ==2) then {{[_x] joinSilent group player} forEach units new_grp};

	if (_applyIR==1 && _spawnSide isEqualto playerSide) exitWith {_lightCount=0;
			{	IRLight = "NVG_TargetC" createVehicle [0,0,0];
				_x removeMagazine "B_IR_Grenade";
				IRLight attachTo [_x, [0,-0.03,0.07], "LeftShoulder"];
				_x addEventHandler ["Killed", {_lightID= _this select 0; {deleteVehicle _x;} forEach attachedObjects _lightID;}];
			} forEach units _grp;
		};
	};
};

 

 

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×