Jump to content
Sign in to follow this  
BEAKSBY

Help my MP money script

Recommended Posts

Hi Anyone who can help,

I'm trying to get this script to work for a MP game.

A unit from one side (AI or player) who kills a unit from opposite side is suppoed to get money equivalent to half the original cost of the killed unit.

I've added this this script to the initServer.sqf but I think there's a problem with the // aray of original cost of units and do loop?

Also, how do I get the hint message to display to only the client that earned the money?

initServer.sqf

{ 

//--- Add killed EH to unit
_handle = _x addEventHandler [ "Killed", {

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

	//--- Check if opposite side is killed by killer
	if (side group _killer != side group _unit) then {

		//--- Determine money value of _unit based on unit class 
		_unitType = typeOf _unit;

			// aray of original cost of units and do loop
			_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"],[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"]];				
			for "_i" from  0 to (count _veh)-1  do {
  				if _unitType = ((_veh select _i) select 1) then {_unitValue = (((_veh select _i) select 1)/2)};	// _unitValue will be half the original price of the unit
			};

		//--- Add money to the client
		[ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;
		// hint format [ "You have gained $%1", _unitValue ]; // THIS ONLY WORKS ON SERVER SIDE

	};

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

} ];

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

} forEach allUnits;

Thanks in advance...if you can help me.

Share this post


Link to post
Share on other sites

This line:

if _unitType = ((_veh select _i) select 1) then {_unitValue = (((_veh select _i) select 1)/2)};    // _unitValue will be half the original price of the unit

looks like it should be:

if _unitType = ((_veh select _i) select 1) then {_unitValue = (((_veh select _i) select 0)/2)};    // _unitValue will be half the original price of the unit

Notice that you were selecing index 1, the vehicle classname, instead of the money amount in index 0, and then dividing by two.

To display a hint to just the killer, try makeing a function:

DNA_fnc_hint =
{
 hint _this;
};

And then call it with:

[format ["You earned: $%1", _unitValue], "DNA_fnc_hint", _killer] call BIS_fnc_MP;

Share this post


Link to post
Share on other sites

To display a hint to just the killer, try makeing a function:

DNA_fnc_hint =
{
 hint _this;
};

And then call it with:

[format ["You earned: $%1", _unitValue], "DNA_fnc_hint", _killer] call BIS_fnc_MP;

Thanks albertfish.

It works but also had to correct the condition in the if statement to:

 if (_unitType == ((_veh select _i) select 1)) then {unitValue = (((_veh select _i) select 0)/2)};

However the hint prompt is not prompting. I added the function you created above

DNA_fnc_hint.sqf to the mission>>functions>>money folder.

I placed hint format [["You earned: $%1", unitValue], "DNA_fnc_hint", _killer] call BIS_fnc_MP; in the initServer.sqf

also. I thought it would work there for everytime a unit is "killed" using the eventhandler?

initSever.sqf

{ 

   //--- Add killed EH to unit
   _handle = _x addEventHandler [ "Killed", {

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

       //--- Check if opposite side is killed by killer
       if (side group _killer != side group _unit) then {

           //--- Determine money value of _unit based on unit class 
           _unitType = typeOf _unit;

               // aray of original cost of units and do loop
               _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"],[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"], [25, "B_Soldier_F"]];                
               for "_i" from  0 to (count _veh)-1  do {
                  if (_unitType == ((_veh select _i) select 1)) then {unitValue = (((_veh select _i) select 0)/2)};    // unitValue will be half the original price of the unit
               };

           //--- Add money to the client
           [ unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;
           // hint format [ "You have gained $%1", unitValue ]; // THIS ONLY WORKS ON SERVER SIDE
		hint format [["You earned: $%1", unitValue], "DNA_fnc_hint", _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;  

Share this post


Link to post
Share on other sites

The syntax is slightly off.

[format ["You earned: $%1", _unitValue], "DNA_fnc_hint", _killer] call BIS_fnc_MP;

This will call the DNA_fnc_hint function on the client for which _killer is local. So, that should display for the killer only. The parameter that is passed in the the function, the first element in that array, is the text that you want to display.

Share this post


Link to post
Share on other sites

I changed all the _unitValue local variables to global unitValue, this works to register the money for the kills...but

...the DNA_fnc_hint function is not hinting after every unit that is killed.

Share this post


Link to post
Share on other sites

I just did a quick test and it seemed to work properly. What does your script look like now that you made the changes?

Share this post


Link to post
Share on other sites

my initServer.sqf

{ 

   //--- Add killed EH to unit
   _handle = _x addEventHandler [ "Killed", {

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

       //--- Check if opposite side is killed by killer
       if (side group _killer != side group _unit) then {

           //--- Determine money value of _unit based on unit class 
           _unitType = typeOf _unit;

               // aray of original cost of units and do loop
               _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"],[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"]];                
               for "_i" from  0 to (count _veh)-1  do {
                  if _unitType = ((_veh select _i) select 1) then {unitValue = (((_veh select _i) select 1)/2)};    // unitValue will be half the original price of the unit
               };

           //--- Add money to the client
           [ unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;
           // hint format [ "You have gained $%1", unitValue ]; // THIS ONLY WORKS ON SERVER SIDE
		[format ["You earned: $%1", unitValue], "DNA_fnc_hint", _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; 

Thanks for testing it on your side.

Share this post


Link to post
Share on other sites

You said you changed the scope of _unitValue from local to global. Was this because the money was not registering? If you want to keep the variable with a local scope you have to have the variable defined outside the if statement. For example:

_unit = _this select 0; 
       _killer = _this select 1; 
       _handle = _unit getVariable [ "DNA_EH_Killed_Money", -1 ]; 
       _unitValue = 0;
<snip>
               for "_i" from  0 to (count _veh)-1  do { 
                  if _unitType = ((_veh select _i) select 1) then {_unitValue = (((_veh select _i) select 1)/2)};    // unitValue will be half the original price of the unit 
               }; 
           [_unitValue, "DNA_fnc_addMoney", _killer] call BIS_fnc_MP;

If you define it before hand like that you should be able to access it.

Also do you see any hints at all?

Share this post


Link to post
Share on other sites

Sorry,

I did change it to global, I posted an earlier version when I was experimenting.

I get it now, you have to initialize the variable before the if statement otherwise it will be lost.

when I tried it using global variables the money was registering, although not correct amounts for other units than the basic soldiers and the hintnwas not displaying after each kill.

I am also using an initPlayerLocal.sqf, would that have an impact?

Share this post


Link to post
Share on other sites

I realized that perhaps the reason addEventHandler [ "Killed", {... was not working except on soldiers is because this event handler does not fire on vehicles.

Instead I willuse addEventHandler [ "HandleDamage", {... and get the damage: Number - Resulting level of damage for the selection. If this is 1 then the vehicle is destroyed and the money will then go to the unit's side that destroyed the vehicle.

Can this also be used for soldiers? I will rewrite my script and test.

Edited by BEAKSBY

Share this post


Link to post
Share on other sites

The killed event handler seems to trigger for both vehicles with units inside of them and empty vehicles. However, it seems that you are not adding the eventhandlers to the vehicles themselves. The allUnits command will return a list of people, so this does not include vehicles.

If you want to gain money only for destroying a vehicle with units inside of it, you can keep using allUnits but then get the vehicle before checking the type.

 //--- Determine money value of _unit based on unit class  
           _unitType = typeOf vehicle _unit; 

Keep in mind that vehicle _unit will return the unit itself if the unit is not in a vehicle, but it will return the vehicle if it is in a vehicle. Note that if there are multiple people in one vehicle the event will be fired for each person. so, you may have to keep track of if you have already acquired money from a vehicle or not.

If you want to gain points for unmanned vehicles, look into using the vehicles command, which will return a list of all vehicles.

Share this post


Link to post
Share on other sites

Thanks again albertfish...and your patience!

It works for either forEach vehicles or forEach allUnits, BUT does not work for both forEach [vehicles, allUnits]; OR perhaps I need to concatenate both arrays?

I would like the eventhandler to fire and collect money for the _killer when the _unit, either a soldier or vehicle (empty or occupied) of the opposite side is killed.

...also, for some reason [format ["You earned: $%1", _unitValue], "DNA_fnc_hint", _killer] call BIS_fnc_MP; is also not working.

initServer.sqf

{ 

   //--- Add killed EH to unit
   _handle = _x addEventHandler [ "Killed", {

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

      //--- Check if opposite side is killed by _killer
      //        if (side group _killer != side group _unit) then {
       if (side group _killer != side group _unit) then {

           //--- Determine money value of _unit based on unit class 
           _unitType = typeOf _unit;

               // array of original cost of units and do loop
               _veh = [[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"]];                
               for "_i" from  0 to (count _veh)-1  do {
                  if (_unitType == ((_veh select _i) select 1)) then {_unitValue = (((_veh select _i) select 0)/2)};    // _unitValue will be half the original price of the unit
               };

           //--- Add money to the client
           [ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;
           [format ["You earned: $%1", _unitValue], "DNA_fnc_hint", _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, allUnits]; // THIS DOES NOT WORK FOR BOTH
//} forEach allUnits; // THIS WORKS        
//} forEach vehicles; // THIS WORKS        

OK, this works

} forEach vehicles + allUnits; // THIS WORKS FOR BOTH

The only problem remaining is the hint message is not executing for some reason still.

Edited by BEAKSBY
UPDATE 2 HOURS LATER

Share this post


Link to post
Share on other sites

I see now that you said you made the hint function a separate script. Did you the compile the script? This function will need to be defined on all the clients, so put it in the client init.

fnc_hint =
{
   hint _this;
};

If you saved it as a separate file, you will need to execute the file in the client's init code.

Edited by albertfish

Share this post


Link to post
Share on other sites
The killed event handler seems to trigger for both vehicles with units inside of them and empty vehicles. However, it seems that you are not adding the eventhandlers to the vehicles themselves. The allUnits command will return a list of people, so this does not include vehicles.

If you want to gain money only for destroying a vehicle with units inside of it, you can keep using allUnits but then get the vehicle before checking the type.

 //--- Determine money value of _unit based on unit class  
           _unitType = typeOf vehicle _unit; 

Keep in mind that vehicle _unit will return the unit itself if the unit is not in a vehicle, but it will return the vehicle if it is in a vehicle. Note that if there are multiple people in one vehicle the event will be fired for each person. so, you may have to keep track of if you have already acquired money from a vehicle or not.

If you want to gain points for unmanned vehicles, look into using the vehicles command, which will return a list of all vehicles.

Based on your feedback from post #11

Keep in mind that vehicle _unit will return the unit itself if the unit is not in a vehicle, but it will return the vehicle if it is in a vehicle. Note that if there are multiple people in one vehicle the event will be fired for each person. so, you may have to keep track of if you have already acquired money from a vehicle or not.

I am now exeperiencing the problem you mentioned and not sure how to account for it.

My money script for vehicles with units inside is running forEach time there is a unit "killed" inside as well as the vehicle because of my forEach vehicles + allUnits; loop.

Shouls I just run two separate "Killed" addEventHandlers, one for vehicles and the other for units?

Or can you suggest another solution?

Here's my initServer.sqf that over multiplies the eventHandler.

initServer.sqf

{  

   //--- Add killed EH to unit 
   _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 = "";

      //--- Check if opposite side is killed by _killer 
   if (side group _killer != side group _unit) then { 

           //--- Determine money value of _unit based on unit class  
           _unitType = typeOf vehicle _unit; 
		_unitTypeA = typeOf  _unit; 
hint format["typeOf vehicle _unit: %1 \n  typeOf  _unit; : %2", _unitType, _unitTypeA];
               // array of original price of units  
               _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"]];                 

       // Checks whether the object is (a subtype) of the given type. Example: vehicle player isKindOf "LandVehicle";  https://community.bistudio.com/wiki/isKindOf
        if (_unitType isKindOf "LandVehicle") then {  // hint format["_unitType %1",_unitType] JUST A TEST  // obtain count and type of units killed inside as well as the vehicle killed

           // _unitArrayInVehicle = allUnits in units _unitType;   // array of units in vehicle
		_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); 

               for "_i" from  0 to (count _veh)-1  do {   // check value for each unit in vehicle
                    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
                }; 
           }; 

           for "_i" from  0 to (count _veh)-1  do {   // also add the value of vehicle killed 
                           if (_unitType == ((_veh select _i) select 1)) then {_unitValue = (((_veh select _i) select 0)/2)};   // each unit value is half original price listed above
                }; 

           _unitValue = _unitValue + _unitsInVehicleSumValue;   // total value of vehicle and units killed inside

       } else {   //need to get type of unit killed id not inside a vehicle 
/

            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 vehicles + 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

You can check if the unit is a person by using

if (_unit isKindOf "Man") then
{
   // Unit is a man
}
else
{
   // Unit is a vehicle
};

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  

×