Jump to content
Robustcolor

Passing arguments to params in switch/exitwith

Recommended Posts

Hi, how can i pass different arguments to params in the same switch/exitiwith fnc?

 

Examples what i'm trying to do

 

[_patrol] spawn fnc_switch;

[_hunt] spawn fnc_switch;

fnc_switch = 
{

Params ["???"];

call {
	if (_patrol) exitWith
	{
		//code 1
	};
	if (_hunt) exitWith
	{
		//code 2
	};
	if (_garrison) exitWith
	{
		//code 3
	};
	//default will be _patrol
};
};

 

Share this post


Link to post
Share on other sites

What datatype (number, string, ...) are _patrol/_hunt/_garrison?

Share this post


Link to post
Share on other sites
23 minutes ago, 7erra said:

What datatype (number, string, ...) are _patrol/_hunt/_garrison?

The thing is i just want to pass an argument to the fnc so it activates one of the if exitwith or cases when one of the condition/name is same as the passed argument.

Share this post


Link to post
Share on other sites

Yes I know but that depends on what data type the params are. The information is not enough to work with for me. If it is a string/number/object then comparing these is no problem but it gets harder with code/arrays.

Share this post


Link to post
Share on other sites
fnc_switch = {
	//params[ [ "_mode", "PATROL", [ "" ] ] ];
	
	params[
		//First var
		[
			"_mode",	//variable name
			"PATROL",	//default value
			[ "" ]		//Must be of type STRING
		]
	];
	
	switch ( toUpper _mode ) do {
		case "PATROL" : {
			//Do something
		};
		case "HUNT" : {
			//Do something
		};
	};
};

[ "PATROL" ] spawn fnc_switch;

 

  • Like 1

Share this post


Link to post
Share on other sites
8 hours ago, Larrow said:

fnc_switch = {
	//params[ [ "_mode", "PATROL", [ "" ] ] ];
	
	params[
		//First var
		[
			"_mode",	//variable name
			"PATROL",	//default value
			[ "" ]		//Must be of type STRING
		]
	];
	
	switch ( toUpper _mode ) do {
		case "PATROL" : {
			//Do something
		};
		case "HUNT" : {
			//Do something
		};
	};
};

[ "PATROL" ] spawn fnc_switch;

 

Thank you @Larrow

 

I'm combining it like this, might be a more efficient way of doing it. Since i want to have one function that spawns ai but still be able to call different scripts for the different Ai groups.

 

private _m1 = ["GUARD",5,_markerpos,_range] spawn VUnits;

private _m2 = ["HUNT",5,_markerpos,_range] spawn VUnits;

 

VUnits = {

params [["_mode","GUARD",[""]],"_g","_markerpos","_range"];

for "_i" from 1 to _g do

{
private _group = createGroup [east, true];

private _pos = [_markerpos, 0, _range, 5, 0, 0.4, 0, [],_markerpos] call BIS_fnc_findSafePos;

private _unit = _group createUnit [selectRandom Unitpatrol, _pos, [], 0, "NONE"];

sleep 1;

};

switch (toUpper _mode) do {
case "GUARD" : {
private _Guardscript = [_group,_markerpos] spawn Robust_fnc_Guard;
};
case "HUNT" : {
private _Attackscript = [_group,_markerpos] spawn Robust_fnc_Attack;
};
};
};

 

Share this post


Link to post
Share on other sites

Since a switch is kinda slow, is it possible to convert it into this

call { 
if (PATROL) exitWith {
//code 1 
};
if (GUARD) exitWith { 
//code 2 
};
if (GARRISON) exitWith {
//code 3 
};
//default code 
};

I also changed toUpper command into toUpperANSI since it could be 3x faster than toUpper.

Share this post


Link to post
Share on other sites

Not exactly:

params[ [ "_mode", "PATROL", [ "" ] ] ];

call {

  if (_mode == "PATROL") exitWith { //code 1 };

  if (_mode == "GUARD") exitWith { //code 2 };

  if (_mode == "GARRISON") exitWith { //code 3 };

  //default code

};

 

using   ==    rather than   isEqualTo     avoids a check for case sensitivity and you will not notice a great difference for code optimization.

 

NB: when you write if (PATROL)... the engine is waiting for ?  ?     ??

Spoiler

a boolean (i.e. TRUE or FALSE)

So, you could avoid such error, just reading at the BIKI syntax for EVERY command (at least when you begin).

Share this post


Link to post
Share on other sites
On 1/4/2020 at 6:18 AM, pierremgi said:

params[ [ "_mode", "PATROL", [ "" ] ] ];

call {

  if (_mode == "PATROL") exitWith { //code 1 };

  if (_mode == "GUARD") exitWith { //code 2 };

  if (_mode == "GARRISON") exitWith { //code 3 };

  //default code

};

Ye i can't say it was any noticeably different from the switch when testing it.

 

Using it with isEqualTo.

params[ [ "_mode", "PATROL", [ "" ] ] ];

call {
  if (_mode isEqualTo "PATROL") exitWith { //code 1 };
  if (_mode isEqualTo "GUARD") exitWith { //code 2 };
  if (_mode isEqualTo "GARRISON") exitWith { //code 3 };
  //default code
};

 

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

×