Jump to content

Recommended Posts

MAGAZINES REPACK

 

Hi all,

Here is a simple script for repacking magazines by double-clicking on them in inventory. Just add it in init.sqf or better, in initPlayerLocal.sqf

 

parameters:   NONE

 

Spoiler

 


MGI_fnc_repack = { 
  _idc = ctrlIDC (_this select 0); 
  _selectedIndex = _this select 1; 
  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})]; 
  player setVariable ["MGIselect", true]; 
  false 
}; 
 
0 = [] spawn { 
  while {true} do { 
    waitUntil {uisleep 0.5; !(isnull (finddisplay 602)) && isnil {player getVariable "MGIselect"}}; 
    if !(isNull (findDisplay 602)) then { 
      ((findDisplay 602) displayCtrl 619) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
      ((findDisplay 602) displayCtrl 633) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
      ((findDisplay 602) displayCtrl 638) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
    }; 
    waitUntil {uisleep 0.5; !isnil {player getVariable "MGIselect"} or isNull (findDisplay 602)}; 
    if (!isnil {player getVariable "MGIselect"}) then { 
      private ["_mag","_wpnType","_wpn"]; 
      if (player getVariable ["MGImags",[]] isEqualTo []) then { 
        _mag = ""; 
        _wpn = "" 
      } else { 
        _cnts = 0; 
        _mag = (player getVariable "MGImags") select 0 select 0; 
        _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count"); 
       if (_fullcnt > 0 && _mag in getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines") && currentWeapon player != "") then { 
          { 
            _wpnType = _x select 3; 
            switch (_wpnType) do { 
              case 1: {_wpn = primaryWeapon player}; 
              case 2: {_wpn = handGunWeapon player}; 
              case 4: {_wpn = secondaryWeapon player}; 
              default {_wpn =""}; 
            }; 
            _cnts = _cnts + (_x select 1); 
            player setammo [_wpn,0]; 
          } count (player getVariable ["MGImags",[]]); 
          _remainingShots = _cnts mod _fullcnt; 
          {player removeMagazineGlobal _mag} count  (player getVariable ["MGImags",[]]); 
          player addMagazines [_mag, floor (_cnts/_fullcnt)]; 
          if (_remainingShots >0) then { 
            player addMagazine [_mag,_remainingShots]; 
          }; 
          reload player; 
        }; 
      }; 
    }; 
    player setVariable ["MGIselect", nil]; 
  }; 
};

 

 

 

 

MODIFIED 01/11/19

Have fun!

  • Like 5
  • Thanks 2

Share this post


Link to post
Share on other sites

Thanks for this! With your permission, I would like to add this code to my mod

 

I hope that would be fine? 

Cheers vd

  • Like 2

Share this post


Link to post
Share on other sites

Hi @pierremgi

 

I am messing around with your code, trying to reverse engineer it so it can be used as holdaction. This I attempt as the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

 

I am having a a hard (but fun) time replicating your "MGImags" variable to suit my need as I do not fully understand the following line:

 

  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

I believe I understand that you are gathering the magazines in an array here.

magazinesAmmoFull player = all magazines in the weapon and inventory

_idc = the location (uniform, vest, backpack) referred to by 619,633,638

_selectedIndex = the index position 0,1,2,... in the uniform, vest or backpack

 

from the (magazinesAmmoFull player), you seem to be selecting out only specific information via expression (array select expression).

Is this gathering the mags that are the same type as the one you LB double clicked?

 

I am trying to do the same here, this should filter out any magazines that do not have the same classname as the first magazine in my "magaziesAmmoFull player" array. (once this all works, I ll have to find a way to consider all different magazine types in my inventory).

_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

 

 

Unfortunately I am now stuck at the next part. The below line does not define _wpn and I do not understand why.

I assume it still has to do with how I create my _Magarray, meaning that I do still not replicate your "MGImags" array correctly.

 

Spoiler

{
  _wpnType = _x select 3;
  switch (_wpnType) do {
    case 1: {_wpn = primaryWeapon player};
    case 2: {_wpn = handGunWeapon player};
    case 4: {_wpn = secondaryWeapon player};
    default {_wpn =""; hint str _wpn};
  };
  _cnts = _cnts + (_x select 1);
  player setammo [_wpn,0];
} count (_Magarray);

 

 

This is where I am at with my holdaction version right now.

_wpn is undefined when running this code:

 

Spoiler

[
player,
"Repack Magazines",
"",
"",
"true",
"true",
{
},
{},
{
          _cnts = 0;
          _magtype1 = magazinesAmmoFull player select 0 select 0;
          private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

          _mag = _Magarray select 0 select 0;
          _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
          if (_fullcnt > 0) then {
            {
              _wpnType = _x select 3;
              switch (_wpnType) do {
                case 1: {_wpn = primaryWeapon player};
                case 2: {_wpn = handGunWeapon player};
                case 4: {_wpn = secondaryWeapon player};
                default {_wpn =""; hint str _wpn};
              };
                _cnts = _cnts + (_x select 1);
            player setammo [_wpn,0];
            } count (_Magarray);
            _remainingShots = _cnts mod _fullcnt;
            {player removeMagazineGlobal _mag} count (_Magarray);
            player addMagazines [_mag, floor (_cnts/_fullcnt)];
            if (_remainingShots >0) then {
              player addMagazine [_mag,_remainingShots];
            };

            reload player;

          };
},
{
},
[],
2,
0,
false,
false
]call BIS_fnc_holdActionAdd;

 

 

I am using this with a contact dlc AKM and a couple of extra mags in the inventory.

 

I think if you could elaborate what...

  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

..does different than..

 

magazinesAmmoFull player

...and...

 

_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

...that would already greatly help me to proceed.

 

Thanks!

VD

 

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, Vandeanson said:

I am messing around with your code, trying to reverse engineer it so it can be used as holdaction. This I attempt as the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

 

I am having a a hard (but fun) time replicating your "MGImags" variable to suit my need as I do not fully understand the following line:

 


  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

I believe I understand that you are gathering the magazines in an array here.

magazinesAmmoFull player = all magazines in the weapon and inventory

_idc = the location (uniform, vest, backpack) referred to by 619,633,638

_selectedIndex = the index position 0,1,2,... in the uniform, vest or backpack

 

from the (magazinesAmmoFull player), you seem to be selecting out only specific information via expression (array select expression).

Is this gathering the mags that are the same type as the one you LB double clicked?

 

 

Yes but that works with inventory (double click). Nothing to do with hold action.

 

2 hours ago, Vandeanson said:

I am trying to do the same here, this should filter out any magazines that do not have the same classname as the first magazine in my "magaziesAmmoFull player" array. (once this all works, I ll have to find a way to consider all different magazine types in my inventory).


_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

 

 

Yes, same as   +((magazinesAmmoFull player) select {_x#0 == (magazinesAmmoFull player select 0 select 0 )})
 

2 hours ago, Vandeanson said:

This is where I am at with my holdaction version right now.

_wpn is undefined when running this code:

 

 

When a local variable is undefined, define it. I did it in my code.

Don't reverse engine this kind of code for a hold action. Just think about what you want to do with it. For example, repacking the current magazine of the player. Mixing hold action and inventory controls is probably a waste of time.

 

I can understand how:  the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

In fact, double-clicking on a magazine like chemlight (and probably your case with drink) will not repack the current mags but reload your weapon (with an existing mag).

Looking for better feature.

 

EDITED: DONE.

Now, you can repack only the mags of your current weapon. So, the other mags like chemlights, drinks or foods should not interfere.

 

 

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

hi @pierremgi

 

Thanks for looking into it.

 

I have ended up going into a different direction as I wanted the script to be based on holdaction and some other variations.

 

Spoiler

fnc_VA_AA_magRep =
        {

            AA_VA_magRep = [
                              player,
                              VA_MagRepAddATxt,
                              "",
                              "",
                              "true",
                              "true",
                              {
                                player playActionNow "Medic";
                              },
                              {},
                              {
                                      _mag = VA_MagTypes select 0;
                                        VA_MagTypes deleteat 0;

                                        _cnts = 0;
                                        private _Magarray = +((magazinesAmmoFull player) select {_mag in _x});
                                        _gunmag = (magazinesAmmoFull player) select {true in _x};
                                        _Magarray = _Magarray - _gunmag;
                                        _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
                                        if (_fullcnt > 0) then {
                                          {

                                              _cnts = _cnts + (_x select 1);

                                          } count (_Magarray);
                                          _remainingShots = _cnts mod _fullcnt;
                                          {player removeMagazineGlobal _mag} count (_Magarray);
                                          player addMagazines [_mag, floor (_cnts/_fullcnt)];
                                          if (_remainingShots >0) then {
                                            player addMagazine [_mag,_remainingShots];
                                          };
                                          systemchat "Magazines repacked";
                                          player removeaction AA_VA_magRep;



                                          if (!(VA_MagTypes isequalto [])) then {
                                            _mag = VA_MagTypes select 0;
                                            _magDN = gettext (configfile >> "CfgMagazines" >> _mag >> "displayName");
                                            VA_MagRepAddATxt = str formatText [ "Repack %1", _magDN];
                                            [] spawn fnc_VA_AA_magRep};
                                          };
                              },
                              {    player switchaction "";
                              },
                              [],
                              6,
                              0,
                              false,
                              false
                            ]call BIS_fnc_holdActionAdd;
        };

player addEventHandler ["InventoryClosed",
                      {
                        VA_MagTypes = [];
                        private _allmagArrs1 = [];
                        private _allmagArrsCN = [];
                         _allmagArrs1 = magazinesAmmoCargo uniformContainer player;
                        _allmagArrs2 = magazinesAmmoCargo vestContainer player;
                        _allmagArrs3 = magazinesAmmoCargo backpackContainer player;

                        _allmagArrs1 append _allmagArrs2;
                        _allmagArrs1 append _allmagArrs3;


                            {
                                _mag = _x select 0;
                                _rounds = _x select 1;
                                _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
                                if (_rounds isequalto _fullcnt || _fullcnt isequalto 1) then {_allmagArrs1 = _allmagArrs1 - [_x]};
                              }foreach _allmagArrs1;

                              if (_allmagArrs1 isequalto []) exitwith{};

                        {_cn = _x select 0; _allmagArrsCN pushback _cn}foreach _allmagArrs1;

                              if (_allmagArrsCN isequalto []) exitwith{};

                        for "_i" from 1 to count _allmagArrsCN do {if (({_x == (_allmagArrsCN select 0)} count _allmagArrsCN) == 1) then {_allmagArrsCN deleteat 0} else {_allmagArrsCN deleteat 0; _allmagArrsCN pushback (_allmagArrsCN select 0)};};

                              if (_allmagArrsCN isequalto []) exitwith{};
                        {VA_MagTypes pushBackUnique _x}foreach _allmagArrsCN;

                        _mag = VA_MagTypes select 0;
                        _magDN = gettext (configfile >> "CfgMagazines" >> _mag >> "displayName");
                        VA_MagRepAddATxt = str formatText [ "Repack %1", _magDN];
                        [] spawn fnc_VA_AA_magRep;

                      }];



player addEventHandler ["InventoryOpened", {
                                            VA_MagTypes = [];
                                              }];

 

 

will need some more fine-tuning and optimization probably. I will also try to find a better matching animation.

 

At InventoryClose Eventhandler, a Holdaction to repack the first group of available mags is created.

 

If more types of mags (from other guns) can be repacked, an updated Holdaction is added while the old one is removed after completion (of the Holdaction).

 

The script will gather all magazines that are of interest in an array:

- from uniform, vest and backpack

- magazines with below 100% rounds of full capacity

- removes "fake" magazine items (filters out magazines or grenades with only 1 full capacity)

- ignores magazines that are loaded into the gun (i want to force players to take out the magazine)

- ignores magazines if only 1 magazine with below 100% rounds per type of magazine is present

 

this must be called in initplayerlocal.sqf and onplayerrespawn.qsf (or in a respawn eventhandler).

 

PS: it was great fun to work on this - thanks for the inspiration and  code provided!

 

cheers vd

  • Like 2

Share this post


Link to post
Share on other sites
On 11/4/2019 at 1:11 AM, Vandeanson said:

_cnts = 0;

 

private _Magarray = +((magazinesAmmoFull player) select {_mag in _x});

 

_gunmag = (magazinesAmmoFull player) select {true in _x};

 

_Magarray = _Magarray - _gunmag;

 

_fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");

 

if (_fullcnt > 0) then {

{ _cnts = _cnts + (_x select 1); } count (_Magarray);

 

_remainingShots = _cnts mod _fullcnt;

 

{player removeMagazineGlobal _mag} count (_Magarray);

 

player addMagazines [_mag, floor (_cnts/_fullcnt)];

 

if (_remainingShots >0) then {player addMagazine [_mag,_remainingShots];};

 

};

 

Anyone else having the issue if you have 2 not full magazines and 1 full in inventory it won't removeMagazinesGlobal or addMagazines but will still add one player addMagazine [_mag,_remainingShots];

It's works when having 2 not full magazines only in inventory or more then > 3.

 

Share this post


Link to post
Share on other sites

Roughly, what i did in my module (based on my script for double-clicking in inventory):

- remove all mags (of this kind)

- add one mag

- reload

- add calculated (full) mags minus one (already in weapon)

- add a mag with remaining shots if any (the incomplete mag)

Share this post


Link to post
Share on other sites

Hi @pierremgi

 

when i repack my mags they are all put inside my uniform and not inside my west.

are there anything you can do about that?

 

or is that just how the script are.

 

// Play3r

Share this post


Link to post
Share on other sites
On 3/19/2022 at 11:35 AM, Play3r said:

Hi @pierremgi

 

when i repack my mags they are all put inside my uniform and not inside my west.

are there anything you can do about that?

 

or is that just how the script are.

 

// Play3r

 

Test:


 

MGI_fnc_repack = {
  _idc = ctrlIDC (_this select 0);
  _selectedIndex = _this select 1;
  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];
  player setVariable ["MGIselect", true];
  false
};
 
[] spawn {
  while {true} do {
    waitUntil {uisleep 0.5; !(isnull (finddisplay 602)) && isnil {player getVariable "MGIselect"}};
      ((findDisplay 602) displayCtrl 619) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"];
      ((findDisplay 602) displayCtrl 633) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"];
      ((findDisplay 602) displayCtrl 638) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"];

    waitUntil {uisleep 0.5; !isnil {player getVariable "MGIselect"} or isNull (findDisplay 602)};
    if (!isnil {player getVariable "MGIselect"}) then {
      private ["_mag","_wpnType","_wpn"];
      if (player getVariable ["MGImags",[]] isEqualTo []) then {
        _mag = "";
        _wpn = ""
      } else {
        _cnts = 0;
        _mag = (player getVariable "MGImags") select 0 select 0;
        _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
       if (_fullcnt > 0 && _mag in getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines") && currentWeapon player != "") then {
          {
            _wpnType = _x select 3;
            switch (_wpnType) do {
              case 1: {_wpn = primaryWeapon player};
              case 2: {_wpn = handGunWeapon player};
              case 4: {_wpn = secondaryWeapon player};
              default {_wpn =""};
            };
            _cnts = _cnts + (_x select 1);
            player setammo [_wpn,0];
          } count (player getVariable ["MGImags",[]]);
          _remainingShots = _cnts mod _fullcnt;
          {player removeMagazineGlobal _mag} count  (player getVariable ["MGImags",[]]);
          private _stockMags = floor (_cnts/_fullcnt);
          private _magCount = 0;
          if (_remainingShots >0) then {
            player addMagazine [_mag,_remainingShots];
            reload player;  
          };
          while {player canAddItemToBackpack [_mag,1] && _magCount < _stockMags} do {
            player addItemToBackpack _mag;
            _magCount = _magCount + 1;
          };
          while {player canAddItemToVest [_mag,1] && _magCount < _stockMags} do {
            player addItemToVest _mag;
            _magCount = _magCount + 1;
          };
          while {_magCount < _stockMags} do {
            player addItemToUniform _mag;
            _magCount = _magCount + 1;
          };
        };
      };
      player setVariable ["MGIselect", nil];
    };
  };
};

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Hi Pierre

i have tried to put the script inside my Initplayerlocal.sqf.

i'm trying for the first time to do a MP game, but i can't get it to work. 

i have saved the misison in the MPmissions, closed the game and started it up again, the went to server brovser and Host server then found my mission run it but if i dobble tap with my mouse nothing happens ingame.

i get no error or anything.

any chance you can help me out ?

 

Share this post


Link to post
Share on other sites

Works fine here, though I swapped the canAddItemX / addItemX sections so the vest gets filled first. Maybe obvious, but you do have some spent mags in your inventory?

 

As a side note:

3 hours ago, Play3r said:

the went to server brovser and Host server then found my mission

 

When you host a server like this, you are actually running it from the MP editor, which is where you should be building your MP missions in the first place.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
11 hours ago, Play3r said:

Hi Pierre

i have tried to put the script inside my Initplayerlocal.sqf.

i'm trying for the first time to do a MP game, but i can't get it to work. 

i have saved the misison in the MPmissions, closed the game and started it up again, the went to server brovser and Host server then found my mission run it but if i dobble tap with my mouse nothing happens ingame.

i get no error or anything.

any chance you can help me out ?

 

 

Difficult to say. This script was update in an addon (MGI modules), and I'm focused on several asked improvements for the modules. If I'm right, the script is working in vanilla and you can so a simple test on it, SP then MP (that shouldn't change anything). Then, try your loadout, then your other mods (avoid double runs with my script + my module).  I can't do that for you.

  • Thanks 1

Share this post


Link to post
Share on other sites
21 hours ago, Harzach said:

Works fine here, though I swapped the canAddItemX / addItemX sections so the vest gets filled first. Maybe obvious, but you do have some spent mags in your inventory?

 

As a side note:

 

When you host a server like this, you are actually running it from the MP editor, which is where you should be building your MP missions in the first place.

 

That i did not know, i have alway used the 3den editor for my scenarios, but i will do that in the furture and start over with my scenario, thanks for the info.

Share this post


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

 

Difficult to say. This script was update in an addon (MGI modules), and I'm focused on several asked improvements for the modules. If I'm right, the script is working in vanilla and you can so a simple test on it, SP then MP (that shouldn't change anything). Then, try your loadout, then your other mods (avoid double runs with my script + my module).  I can't do that for you.

 

it is a simple vanilla game only the map is Pulau, and i use the Hermes air services. will try the way that Harzach told me maybe it works then.

Share this post


Link to post
Share on other sites

It shouldn't matter where you build the mission in this regard, that was just an aside. It's still the 3den editor, you just launch directly into a hosted server from it and save directly to the mpmissions folder.

 

Make a simple repro mission (no mods or other scripts) and share it here.

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

×