Jump to content
Sign in to follow this  
BEAKSBY

addMPEventHandler [MPKilled] Vs addEventHandler [Killed] with call BIS_fnc_MP;

Recommended Posts

HI All,

I'm a bit confused with how I should handle the following.

Currently my addEventHandler [Killed] script only runs once for the player client side but works all the time for the player on the server side.

I'm not sure if I should be using addMPEventHandler [MPKilled] instead? I'm worried that if I use MPKilled then call my money script to add money to the _killer, I will get this function [ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP; running on every machined and slow down the game?

I based this assumption on "Triggered when the unit is killed. EH can be added on any machine and EH code will trigger globally on every connected client and server. Add it only on server because if you have 10 machines on network and add this EH to every machine, when triggered the EH code will be executed 10 x 10 = 100 times." See MPKilled.

My script is in the initServer.sqf as follows:

veh = [[1200, "B_MBT_01_TUSK_F"],[900, "B_APC_Tracked_01_rcws_F"],[750, "B_APC_Tracked_01_AA_F"],[600, "I_APC_Wheeled_03_cannon_F"],
[300,"B_MRAP_01_gmg_F"],[150,"B_MRAP_01_hmg_F"],[100,"B_G_Offroad_01_armed_F"],[200,"B_static_AT_F"],[200, "B_static_AA_F"],[150,"B_soldier_LAT_F"],
[100,"B_G_Soldier_LAT_F"],[25, "B_Soldier_F"],[150,"B_GMG_01_high_F"],[100,"B_HMG_01_high_F"],[1200, "O_MBT_02_cannon_F"],[900, "O_APC_Tracked_02_cannon_F"],
[750, "O_APC_Tracked_02_AA_F"],[600, "O_APC_Wheeled_02_rcws_F"],[300,"O_MRAP_02_gmg_F"],[150,"O_MRAP_02_hmg_F"],[100,"O_G_Offroad_01_armed_F"],
[200,"O_static_AT_F"],[200, "O_static_AA_F"],[150,"O_soldierU_LAT_F"],[100,"O_G_Soldier_LAT_F"],[150,"O_GMG_01_high_F"],[100,"O_HMG_01_high_F"],
[25, "O_Soldier_F"], [66, "O_crew_F"],[66, "B_crew_F"]];   

{  
  //--- Add killed EH to unit FOR VEHICLES AND UNITS IN VEHICLES
  _handle = _x addEventHandler [ "Killed", { 

       _unit = _this select 0; 
       _killer = _this select 1; 
       _handle = _unit getVariable [ "DNA_EH_Killed_Money", -1 ]; 
       _unitValue = 0; 
       _unitsInVehicleSumValue = 0; 
       _unitType = "";
       _unitArrayInVehicle = [];
       _countUnitsInVehicle = 0;
       _unitsInVehicleSumValue = 0;
       _unitInVehicle = "";

      	if (side group _killer != side group _unit) then {   //--- Check if opposite side is killed by _killer 
            _unitType = typeOf vehicle _unit;   //--- Determine money value of _unit based on unit class  

     		if (_unitType isKindOf "LandVehicle") then {  //---obtain count and type of units killed inside as well as the vehicle killed
    		_unitArrayInVehicle =  units _unit;   // array of units in vehicle
           	              _countUnitsInVehicle = count _unitArrayInVehicle;   // number of units in vehicle

           		for "_i" from  0 to (count _unitArrayInVehicle)-1  do {   //---sum value of all units in vehicle
            		_unitInVehicle = typeOf (_unitArrayInVehicle select _i); 

		//_textcrew = [];
			for "_i" from  0 to (count veh)-1  do {   //---check value for each unit killed in vehicle and obtain its name and picture
                    			if (_unitInVehicle == ((veh select _i) select 1)) then {_unitsInVehicleSumValue = _unitsInVehicleSumValue + (((veh select _i) select 0)/2);   // each unit value is half original price listed above
				//_vehicle = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "displayname");		             
				//_picture = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture");
				//_textcrew = str _textcrew + str (parseText format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle]);
				//hint _textcrew;
				};
			}; 
           		}; 

           		              _textvehicle = [];
		for "_i" from  0 to (count veh)-1  do {   //---also add the value of vehicle killed and obtain its name and picture
               		if (_unitType == ((veh select _i) select 1)) then {_unitValue = (((veh select _i) select 0)/2);   //---each unit value is half original price listed above
				_vehicle = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "displayname");		             
				_picture = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture");
				_textvehicle = parsetext format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle];
				// hint _textvehicle;
			};	
		}; 
           		              _unitValue = _unitValue + _unitsInVehicleSumValue;   //---total value of vehicle and units killed inside
		_textKilled = str _textvehicle + str _textcrew;   //---total texts of vehicle and units killed inside 
		// hint format["_unit: %1 \n _unitType: %2 \n _unitValue: %3 \n _unitArrayInVehicle: %4 \n _countUnitsInVehicle: %5 \n _unitsInVehicleSumValue: %6 \n _textKilled: %7", _unit,_unitType, _unitValue, _unitArrayInVehicle, _countUnitsInVehicle, _unitsInVehicleSumValue, _textKilled ];  //TEST & WORKS		
       	              }; 
       	              //--- Add money to the client 
	[ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP; 

       }; 
       //--- Remove the event handler 
       _unit removeEventHandler [ "Killed", _handle ]; 
   } ]; 
   //--- Store killed EH handle in unit namespace 
   _x setVariable [ "DNA_EH_Killed_Money", _handle ]; 

}  forEach vehicles;  // THIS WORKS  

{  
   //--- Add killed EH to unit FOR UNITS ONLY (NOT IN VEHICLES)
   _handle = _x addEventHandler [ "Killed", { 

       _unit = _this select 0; 
       _killer = _this select 1; 
       _handle = _unit getVariable [ "DNA_EH_Killed_Money", -1 ]; 
       _unitValue = 0; 
       _unitType = "";

      	//--- Check if opposite side is killed by _killer 
   	if (side group _killer != side group _unit) then {   //--- Check if opposite side is killed by _killer  
	if (vehicle _unit == _unit) then {   //---hint "_unit is NOT in a vehicle"
		//--- Determine money value of _unit based on unit class  
		_unitType = typeOf _unit; 
		// array of original price of units                                     
		 for "_i" from  0 to (count veh)-1  do {   // value of vehicle killed also
			if (_unitType == ((veh select _i) select 1)) then {_unitValue = (((veh select _i) select 0)/2)};   //---each unit value is half original price listed above

			//--- Add money to the client 
		 	[ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;  		
		}; 
	};
};
//--- Remove the event handler 
_unit removeEventHandler [ "Killed", _handle ]; 
   } ]; 
   //--- Store killed EH handle in unit namespace 
   _x setVariable [ "DNA_EH_Killed_Money", _handle ]; 

}  forEach allUnits; // THIS WORKS 

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
Sign in to follow this  

×