Jump to content
Sign in to follow this  
redarmy

How to go about modifying this script?

Recommended Posts

Im currently using an on hit eventhandler to remove a players vest or/and backpack randomly.

 


player addEventHandler ["HitPart", 
{ 
 (_this select 0) params ["_unit", "_shooter", "_bullet", "_pos", "_vel", "_hitLoc"]; 
 if ((vest _unit) != "") then 
 { 
  { 
   if (_x == "pelvis"   && 10 > random 100) then 
   { 
    removeVest _unit; 
   }; 
  } forEach _hitLoc; 
 }; 
}];    player addEventHandler ["HitPart", 
{ 
 (_this select 0) params ["_unit", "_shooter", "_bullet", "_pos", "_vel", "_hitLoc"]; 
 if ((backpack _unit) != "") then 
 { 
  { 
   if (_x == "pelvis"   && 5 > random 100) then 
   { 
    removeBackpack _unit; 
   }; 
  } forEach _hitLoc; 
 }; 
}];          

I however want to modify it to instead of removing the vest,or backpack that it removes a random item from either vest,uniform or backpack,and play a hint telling the player"you lost an item etc".

 

The hard way is to rewrite this syntax,replacing vest/backpack with individual classnames like "medkit", "radio" etc. But that will take an eternity as theres alot of classnames to factor in.

 

Is there an easier way to randomly pull any type of item that can go into inventory,wether it be magazine,grenade,vanilla or custom item?

Share this post


Link to post
Share on other sites
Quote

I however want to modify it to instead of removing the vest,or backpack that it removes a random item from either vest,uniform or backpack,and play a hint telling the player"you lost an item etc".

If I understand you correctly, you want to rewrite the code you provided in a way to randomly remove an item from the player's inventory when they are hit.? If so, then maybe something like the following code ?

// Event handler for when the player is hit on a specific body part
player addEventHandler ["HitPart", {
    private ["_unit", "_shooter", "_bullet", "_pos", "_vel", "_hitLoc", "_item"];

    // Assign parameters to variables in the Array
    _unit = _this select 0; 
    _hitLoc = _this select 5;

    // Check if the unit has any items in the inventory
    if (count (items _unit) > 0) then {
        // Select a random item from the inventory
        _item = (items _unit) select (floor (random (count (items _unit))));

        // Remove the selected item
        _unit removeItem _item;

        // Display a hint to inform the player about the lost item
        hint format ["You lost %1", displayName _item];
    };
}];

Keep in mind that I am a bit new to SQF Scripting, so you can try and test it to see if it works correctly. I hope that helps.

Share this post


Link to post
Share on other sites
1 hour ago, Rhaelphens said:

If I understand you correctly, you want to rewrite the code you provided in a way to randomly remove an item from the player's inventory when they are hit.? If so, then maybe something like the following code ?


// Event handler for when the player is hit on a specific body part
player addEventHandler ["HitPart", {
    private ["_unit", "_shooter", "_bullet", "_pos", "_vel", "_hitLoc", "_item"];

    // Assign parameters to variables in the Array
    _unit = _this select 0; 
    _hitLoc = _this select 5;

    // Check if the unit has any items in the inventory
    if (count (items _unit) > 0) then {
        // Select a random item from the inventory
        _item = (items _unit) select (floor (random (count (items _unit))));

        // Remove the selected item
        _unit removeItem _item;

        // Display a hint to inform the player about the lost item
        hint format ["You lost %1", displayName _item];
    };
}];

Keep in mind that I am a bit new to SQF Scripting, so you can try and test it to see if it works correctly. I hope that helps.

Thanks im going to this after work,but reading it,isnt this just removing the item and placing it on floor next to player?  I am trying to delete the item completely. Sort of to simulate it being destroyed

Share this post


Link to post
Share on other sites

You could probably try to add // deleteVehicle _item // after removeItem:

 

        // Remove the selected item
        _unit removeItem _item;
		// Delete it right after removing
		deleteVehicle _item;
        // Display a hint to inform the player about the lost item
        hint format ["You lost %1", displayName _item];

 I think you have to remove it before delete, or it will remain on the player.

Share this post


Link to post
Share on other sites
4 hours ago, JCataclisma said:

You could probably try to add // deleteVehicle _item // after removeItem:

 


        // Remove the selected item
        _unit removeItem _item;
		// Delete it right after removing
		deleteVehicle _item;
        // Display a hint to inform the player about the lost item
        hint format ["You lost %1", displayName _item];

 I think you have to remove it before delete, or it will remain on the player.

I tested it and syntax wouldnt be accepted intil i removed the hint part. But when i was taking hits i got an error about "count items". I see what your doing there,and i think it should work but it isnt.

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  

×