Jump to content
Sign in to follow this  
BEAKSBY

Do my two addEventHandlers “Killed� need a different Number (index)?

Recommended Posts

Hi Folks,

Do my two addEventHandlers “Killed†need a different Number? (The index of the currently added event handler is returned)? Both addEventHandlers are using _handle and incidentally both my removeEventHandlers are using _unit.

Both are included in my initServer.sqf

The first addEventHandlers “Killed†is used to handle vehicles and the units killed inside.

{
//--- 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 ];
               {more code};

_unit removeEventHandler [ "Killed", _handle ];    //--- Remove the event handler
} ]; 

_x setVariable [ "DNA_EH_Killed_Money", _handle ];   //--- Store killed EH handle in unit namespace 
}  forEach vehicles;

The second addEventHandlers “Killed†is used for all units, not inside a vehicle.

{  
//--- 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 ];
              {more different code};

_unit removeEventHandler [ "Killed", _handle ];   //--- Remove the event handler
} ]; 

_x setVariable [ "DNA_EH_Killed_Money", _handle ];   //--- Store killed EH handle in unit namespace 
}  forEach allUnits;

Share this post


Link to post
Share on other sites

Why do you have to remove the EV?

Note that the BIKI page says that any EV with a higher index than the EV you are removing needs to decremented. It seems like it could cause a lot of bugs if you try to remove it and keep track of any index since many addons and likely A3 itself adds killed EV's.

Otherwise you could simply add one EV that handles units inside and outside vehicles

Share this post


Link to post
Share on other sites

Thanks cuel,

I thought by removing them it would avoid any conflicts between both my eventHandlers, but by the sounds of it, using removeEventHandler in this case may only add potential problems.

At first I tried to create on eventHandlers to account for empty vehicles, vehicles and their crew, and only units (soldiers) but ran into problems trying to differentiate the latter two. The {more code} inside my event handlers add a dollar value I assign to allUnits and vehicles.

Here's my complete initServer.sqf with both eventhandlers to give you an idea.

initServer.sqf

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"],[400,"B_APC_Wheeled_01_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 = "";
   _textvehicle = "";

  //--- Check if opposite side is killed by _killer AND obtain count and type of units killed inside as well as the vehicle killed       	
  if ((side group _killer != side group _unit) && (_unitType isKindOf "LandVehicle")) then {   
  _unitType = typeOf vehicle _unit;   //--- Determine money value of _unit based on unit class  

    	_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;   //---THIS RETURNS []crewman
			};
		}; 
           	}; 

           	//_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];
		_textvehicle = parsetext format ["<t size='0.85'> %1",_vehicle];
		//hint _textvehicle;   //---THIS WORKS
		};	

	_unitValue = _unitValue + _unitsInVehicleSumValue;   //---total value of vehicle and units killed inside
	_textKilled = _textvehicle + _textcrew;   //---total texts of vehicle and units killed inside 

       	}; 
       	//--- Add money to the client 
	[ [_unitValue, _textvehicle], "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 AND //---hint "_unit is NOT in a vehicle"
   	if ((side group _killer != side group _unit) && (vehicle _unit == _unit)) then {   //--- Check if opposite side is killed by _killer  
			//--- 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  


Createcenter EAST; 
Createcenter WEST; 
WEST setFriend [EAST,0]; 
EAST setFriend [WEST,0]; 

[west, "WEST1"] call BIS_fnc_addRespawnInventory; 
[west, "WEST2"] call BIS_fnc_addRespawnInventory; 
[west, "WEST3"] call BIS_fnc_addRespawnInventory; 
[east, "EAST1"] call BIS_fnc_addRespawnInventory; 
[east, "EAST2"] call BIS_fnc_addRespawnInventory; 
[east, "EAST3"] call BIS_fnc_addRespawnInventory; 
if (sector1 getVariable 'owner' == WEST) then {[west, "sector1"] call BIS_fnc_addRespawnPosition}; 
// [west, "Sector2"] call BIS_fnc_addRespawnPosition;  
[west,[1685,5640,150]] call BIS_fnc_addRespawnPosition;  
[east,[1685,5640,150]] call BIS_fnc_addRespawnPosition;  
// [getPos player, WEST, 5] call BIS_fnc_spawnGroup; 

[] execVM "cleanup.sqf";  

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  

×