Jump to content
thy_

Pretty sure you know how make these few lines even smaller

Recommended Posts

How can I simplify these three functions without losing their purpose? I get itchy hands when I find myself coding like this, without how to improve my time coding yet.

If you'll suggest a functions merge, I will appreciate it if you also explain how should I call it. I have not enough experience with params yet (studying in progress...)

 

THY_fnc_VO_checkPlayerVehRepair = 
{
	params ["_eachVeh"];
	
	(alive _eachVeh) AND (speed _eachVeh < 2 AND speed _eachVeh > -2) AND (!underwater _eachVeh) AND ((getPos _eachVeh) select 2 < 0.1) AND (_serviceInProgress == false) AND (!isEngineOn _eachVeh) AND (damage _eachVeh > VO_minRepairService)
};

THY_fnc_VO_checkPlayerVehRefuel = 
{
	params ["_eachVeh"];
	
	(alive _eachVeh) AND (speed _eachVeh < 2 AND speed _eachVeh > -2) AND (!underwater _eachVeh) AND ((getPos _eachVeh) select 2 < 0.1) AND (_serviceInProgress == false) AND (!isEngineOn _eachVeh) AND (fuel _eachVeh > VO_minRefuelService)
};

THY_fnc_VO_checkPlayerVehRearm = 
{
	params ["_eachVeh"];
	
	(alive _eachVeh) AND (speed _eachVeh < 2 AND speed _eachVeh > -2) AND (!underwater _eachVeh) AND ((getPos _eachVeh) select 2 < 0.1) AND (_serviceInProgress == false) AND (({getNumber (configFile >> "CfgMagazines" >> _x select 0 >> "count") != _x select 1} count (magazinesAmmo _eachVeh)) > 0)
};

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
THY_fnc_VO_checkServicability = {
	params ["_eachVeh"];
	alive _eachVeh and abs speed _eachVeh < 2 and !underwater _eachVeh and getPos _eachVeh select 2 < 0.1 and !_serviceInProgress
};

THY_fnc_VO_checkPlayerVehRepair = 
{
	params ["_eachVeh"];
	_eachVeh call THY_fnc_VO_checkServicability AND (!isEngineOn _eachVeh) AND (damage _eachVeh > VO_minRepairService)
};

THY_fnc_VO_checkPlayerVehRefuel = 
{
	params ["_eachVeh"];
	_eachVeh call THY_fnc_VO_checkServicability AND (!isEngineOn _eachVeh) AND (fuel _eachVeh > VO_minRefuelService)
};

THY_fnc_VO_checkPlayerVehRearm = 
{
	params ["_eachVeh"];
	_eachVeh call THY_fnc_VO_checkServicability AND (({getNumber (configFile >> "CfgMagazines" >> _x select 0 >> "count") != _x select 1} count (magazinesAmmo _eachVeh)) > 0)
};

There is rarerly, if ever, the need to copy code. You can move common checks into a separate function (works in this case because you are only using "and"). But there is an error in your function: _serviceInProgress is not defined. You might not get one because it is defined in the parent function but this is bad practice to rely on a non defined variable in your code. Also comparisions with boolean values is unneccessary, "true == _var" and "_var" are the same, just like "false == _var" and "!_var".

  • Thanks 1

Share this post


Link to post
Share on other sites

Got it! I already extended these amends on my end. Thanks.

Love that abs command to simplify the speed condition.

 

Functions and params are driving me crazy in my early steps into them.  

 

For example, even with your orientation and @Larrow inbox example (about another topic but around the same script), I'm struggling to get the rules behind params and how to call them properly.  Look at this example where I am stuck in fn_VO_coreGrd.sqf:

 

THY_functions.hpp

Spoiler

class THY_functions 
{
	tag = "THY"; 
	
	class vehiclesOverhauling	
	{
		file = "vehiclesOverhauling";
		
		class VO_parameters { preInit = 1 };
		class VO_coreGrd { preInit = 1 };	
		class VO_coreAir { preInit = 1 };
		class VO_coreNau { preInit = 1 };
		
		// utility functions:		
		class VO_globalFunctions {};	
	};
};

 

fn_VO_coreGrd.sqf

private [ /* a lot of variables */ ];

[] spawn 
{
	// CODE...

	While { /* condition */ } 
	{
		{
			if ( [_eachVeh, _x, VO_grdServiceRange] call THY_fnc_VO_checkStation ) then     // <--- "Error Undefined variable in expressian THY_fnc..."
			{
				// CODE...
			};

		} forEach _fullAndRepairStations;

		// CODE...
	};
};

 

fn_VO_globalFunctions.sqf

Spoiler

// ANOTHER FUNCTION...

THY_fnc_VO_checkStation = 
{
	params ["_veh","_station","_range"];
	
	// the station is alive, the player's veh is close enough, and the station is NOT serving itself.
	(alive _station) AND ( (_veh distance _station) < _range ) AND (_veh != _station)
};

// ANOTHER FUNCTION...

 

 

 

Share this post


Link to post
Share on other sites

spawn creates a new scope which does not know about the variables you defined in the scope above. You have to pass the variables to the spawn:

[_eachVeh, _x] spawn 
{
	params ["_eachVeh", "_x"]; // though it is bad practice to use magic variables as variable names
	// CODE...

	While { /* condition */ }
	{
		{
			if ( [_eachVeh, _x, VO_grdServiceRange] call THY_fnc_VO_checkStation ) then
			{

 

Share this post


Link to post
Share on other sites

I'd like to see how these code blocks are actually used as you are definitely doing something wrong in a higher scope to end up in this debacle

 

On 3/7/2022 at 2:54 AM, 7erra said:

spawn creates a new scope which does not know about the variables you defined in the scope above

For brevity, this statement is only true for local variables, global variables can still be referenced in a higher scope:

testString = "myTest";
[] spawn {
	while {true} do {
		hintSilent testString;
	}
}
  • Like 1

Share this post


Link to post
Share on other sites

After all these months, here I am back.

 

Since last March, I'm studying programming through Python in my free time and, gosh, that helps me to get functions and params. Next step will be OOP/classes hehe. But for now, here is the result: I already launched the script we were debating here. 

 

 

Thanks for @7erra and @Larrow support as usual. 

 

thy

  • 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

×