Jump to content
M1ke_SK

[problem] take weapon from container

Recommended Posts

There is weapon and magazines for weapon in container.

player addEventHandler [
	"Take",
	{
		params ["_unit", "_container", "_item"];

		diag_log format ["_item: %1", _item];
	}
];

When I take weapon, it shows up in log (_item variable).

 

Problem: With weapon I receive also magazines for this weapon.

 

Question: How to prevent to pick up these additional magazines?

 

Idea: I want to have control of what I can take from container. I want only take weapon without magazines.

 

 

Share this post


Link to post
Share on other sites

I am thinking if should I check items of unit before eventHandler and compare it with items after take, but that seems like overkill.

Share this post


Link to post
Share on other sites

Yes, this is a little code I wrote. Hope this help. Any improvement welcome.

 

player addEventHandler ["inventoryOpened", {
  MGI_EHopen = true; 
  (_this select 1) spawn {
    while {MGI_EHopen} do { 
      MGI_crateLoadout = magazineCargo _this}; 
    }; 
    if (isNil "MGI_EHTake") then {
      MGI_EHTake = player addEventHandler ["Take", { 
        params ["_unit", "_container", "_item"];
        if (_container isKindOf "ReammoBox_F") then {
        if (getNumber (configFile >> "cfgWeapons" >> _item >> "type") in [1,2,4]) then { 
          _new = magazineCargo _container; 
          _loaded = MGI_crateLoadout - _new;
           
          {player removeMagazines _x} forEach _loaded;

          player setAmmo [_item,0];
          clearMagazineCargoGlobal _container;
          {_container addMagazineCargoGlobal [_x,1]} forEach MGI_crateLoadout;
        }};
      }];
    }; 
}]; 
player addEventHandler ["InventoryClosed", {  
  player removeEventHandler ["Take",MGI_EHTake];
  MGI_EHTake = nil;  
  MGI_EHopen = false  
}];

 

  • Like 1

Share this post


Link to post
Share on other sites
14 hours ago, pierremgi said:

Yes, this is a little code I wrote. Hope this help. Any improvement welcome.

 


player addEventHandler ["inventoryOpened", {
  MGI_EHopen = true; 
  (_this select 1) spawn {
    while {MGI_EHopen} do { 
      MGI_crateLoadout = magazineCargo _this}; 
    }; 
    if (isNil "MGI_EHTake") then {
      MGI_EHTake = player addEventHandler ["Take", { 
        params ["_unit", "_container", "_item"];
        if (_container isKindOf "ReammoBox_F") then {
        if (getNumber (configFile >> "cfgWeapons" >> _item >> "type") in [1,2,4]) then { 
          _new = magazineCargo _container; 
          _loaded = MGI_crateLoadout - _new;
           
          {player removeMagazines _x} forEach _loaded;

          player setAmmo [_item,0];
          clearMagazineCargoGlobal _container;
          {_container addMagazineCargoGlobal [_x,1]} forEach MGI_crateLoadout;
        }};
      }];
    }; 
}]; 
player addEventHandler ["InventoryClosed", {  
  player removeEventHandler ["Take",MGI_EHTake];
  MGI_EHTake = nil;  
  MGI_EHopen = false  
}];

 

 

I tried it and this only remove bullets from gun. But ammo is in weapon. And it also add magazines to vest for weapon. I will try to fiddle with it.

Share this post


Link to post
Share on other sites
2 minutes ago, M1ke_SK said:

 

I tried it and this only remove bullets from gun. But ammo is in weapon. And it also add magazines to vest for weapon. I will try to fiddle with it.

I tried to make some code working with any crate (reammoBox_F family). You can extend to ReammoBox family for ground weapon holder.

When you pick any gun/handgun/launcher, you have it empty (with empty mag in fact) and the mags count stays unchanged in box. I thought you wanted that. You can add any mag to vest or backpack if you want, but not automatically. If your player already had some mags in vest, these are not removed, of course.

So, If you had some different behaviors, please describe the crate, unit, and start config. then, tell me what's wrong and what you want exactly in usual cases (pick weapon from crate, corpse, ground)...

Share this post


Link to post
Share on other sites
15 minutes ago, pierremgi said:

I tried to make some code working with any crate (reammoBox_F family). You can extend to ReammoBox family for ground weapon holder.

When you pick any gun/handgun/launcher, you have it empty (with empty mag in fact) and the mags count stays unchanged in box. I thought you wanted that. You can add any mag to vest or backpack if you want, but not automatically. If your player already had some mags in vest, these are not removed, of course.

So, If you had some different behaviors, please describe the crate, unit, and start config. then, tell me what's wrong and what you want exactly in usual cases (pick weapon from crate, corpse, ground)...

 

Yes, great. Is there possibility to REMOVE this magazine from weapon? Even when it is empty it should not be in weapon.

Share this post


Link to post
Share on other sites
1 minute ago, M1ke_SK said:

 

Yes, great. Is there possibility to REMOVE this magazine from weapon? Even when it is empty it should not be in weapon.

Didn't find that at this time.

Share this post


Link to post
Share on other sites
22 minutes ago, pierremgi said:

Didn't find that at this time.

 

Maybe this?

 

{ if ( _x in primaryWeaponMagazine _unit ) then { _unit removeMagazine _x } } forEach magazines _unit;

 

Share this post


Link to post
Share on other sites

To remove a magazine from a weapon you can use removePrimaryWeaponItem:

// Primary weapon, rifle
{
	_unit removePrimaryWeaponItem _x;
} forEach primaryWeaponMagazine _unit;
// Secondary weapon, launcher
{
	_unit removeSecondaryWeaponItem  _x;
} forEach secondaryWeaponMagazine _unit;
// Handgun, pistol
{
	_unit removeHandgunItem _x;
} forEach handgunMagazine _unit;

 

  • Like 2

Share this post


Link to post
Share on other sites
@pierremgi When you already have some magazines in unit, and you will take weapon witch use these magazines, it will remove them too.
 
_arrayA = [1,2,3,2,4];
_arrayB = [2,3];

_arrayC = _arrayA - _arrayB;

_arrayC => [1,4]

This will remove all main magazines used by weapon

_loaded = MGI_crateLoadout - _new;

Share this post


Link to post
Share on other sites
1 hour ago, M1ke_SK said:
@pierremgi When you already have some magazines in unit, and you will take weapon witch use these magazines, it will remove them too.
 

_arrayA = [1,2,3,2,4];
_arrayB = [2,3];

_arrayC = _arrayA - _arrayB;

_arrayC => [1,4]

This will remove all main magazines used by weapon


_loaded = MGI_crateLoadout - _new;

Yes I can do better. I wake up.

Share this post


Link to post
Share on other sites
player addEventHandler ["inventoryOpened", {
  MGI_EHopen = true;
  (_this select 1) spawn {
    while {MGI_EHopen} do {
      MGI_crateLoadout = magazineCargo _this};
    };
    if (isNil "MGI_EHTake") then {
      MGI_EHTake = player addEventHandler ["Take", {
        params ["_unit", "_container", "_item"];
        private _wpnType = getNumber (configFile >> "cfgWeapons" >> _item >> "type");
        if (_container isKindOf "ReammoBox_F") then {
        if  (_wpnType in [1,2,4]) then {
          _new = magazineCargo _container;
          _loaded = +MGI_crateLoadout;
          {_loaded deleteAt (_loaded find _x)} forEach _new;
          {player removeMagazines _x} forEach _loaded;
          clearMagazineCargoGlobal _container;
          call {
            if (_wpnType == 1) exitWith {{_unit removePrimaryWeaponItem  _x} forEach (primaryWeaponMagazine _unit)};
            if (_wpnType == 2) exitWith {{_unit removeHandgunItem _x} forEach (handgunMagazine _unit)};
            if (_wpnType == 4) exitWith {{_unit removeSecondaryWeaponItem _x} forEach (secondaryWeaponMagazine _unit)};
        };
          {_container addMagazineCargoGlobal [_x,1]} forEach MGI_crateLoadout;
        }};
      }];
    };
}];
player addEventHandler ["InventoryClosed", {
  player removeEventHandler ["Take",MGI_EHTake];
  MGI_EHTake = nil;
  MGI_EHopen = false
}];

 

Share this post


Link to post
Share on other sites

Maybe little simplier would be:

//clear all magazines player
{ _unit removeMagazine _x; } forEach magazines _unit;

//restore old magazines before take
{ _unit addMagazine [_x, 1]; } forEach _old_unit_magazines_array;

//clear all magazines from container
clearMagazineCargoGlobal _container;

//restore old magazines before take
{ _container addMagazineCargoGlobal [_x, 1]; } forEach _old_container_magazines_array;

 

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

×