Jump to content
Sign in to follow this  
HJohnson

Need help with tactical reload script

Recommended Posts

After doing some research for a couple weeks I've started to put together a script that should (in theory should) work. However I am not exactly experienced in writing my own Arma code from scratch and I wanted to see if someone could help me proof read the code to see if it will work properly.

Right now the code will look to see if the weapon has the ability to tacreload (to exclude things like launchers), how much ammo the magazine of the weapon can hold at maximum, and a variable to tell whether or not it has one in the chamber..

Right now this is assuming that it is for a weapon that has a 30 round magazine that has been modified to have 31 (because setammo can't add ammo) and when you reload without one in the chamber it sets it down one.

 ////////////////////// The Script/////////////////////
//(6)http://community.bistudio.com/wiki/ammo
//(5)http://forums.bistudio.com/showthread.php?122831-Unit-to-reload-weapon-auto-saving
//(4)http://community.bistudio.com/wiki/setAmmo
//(3)http://community.bistudio.com/wiki/ListOfKeyCodes
//(2)http://forums.bistudio.com/showthread.php?130863-Run-script-when-button-is-pressed

_unit = _this select 0; // the player
_weapon = primaryWeapon player ; // the weapon the player has
_mag = _this select 2; // the magazine type

_oneinthechamber = 0; //chamber variable

///////////////INCREASE MX'S DEFAULT MAG TO +1 DUE TO (4)!////////////////////////////////////////////////////////////////////////////////
_magcount = getNumber (configFile / "CfgMagazines" / _mag / "count"); //Maximum magazine amount for the magazine
_hjtacticalreload = getNumber (configFile / "CfgWeapons" / _weapon / "hj_tacticalreload"); //Whether or not the weapon can tactical reload

_currentmagazine = _unit ammo _weapon; //Current ammo in current magazine.
/////One in the Chamber
//No Smaller than 0;

if (_oneinthechamber < 0) then {
_oneinthechamber=0;
};
//No larger than 1;

if (_oneinthechamber > 1) then {
_oneinthechamber=1;
};
//////
//////HJtacreloadvariable	
//No Smaller than 0;

if (_hjtacticalreload < 0) then {
_hjtacticalreload=0;
};
//No larger than 1;

if (_hjtacticalreload > 1) then {
_hjtacticalreload=1;
};
////////
// if the magazine has bullets in it, but less than the maximum amount, and it can tac-reload, set one in the chamber to 1;
if ( _currentmagazine > 0 && _currentmagazine < _magcount && _hjtacticalreload = 1) then
{
	_oneinthechamber=1;
	hint format["One in the chamber is %1",_oneinthechamber];
	sleep .5

	_tacmag = currentmagazine player;
	player removemagazine _tacmag;
	player addmagazine _tacmag;
	reload player;
	sleep .5;
	_oneinthechamber=0;
	hint format["One in the chamber is %1",_oneinthechamber];
};
// if the magazine has no bullets in it, set one in the chamber to 0. Can't use setammo to increase ammo amount. This will set ammo count to 30 ideally.
if ( _currentmagazine = 0 && _currentmagazine < _magcount && _hjtacticalreload = 1) then
{
	_oneinthechamber=0;
	hint format["One in the chamber is %1",_oneinthechamber];
	sleep .5
	_tacmag = currentmagazine player;
	player removemagazine _tacmag;
	player addmagazine _tacmag;
	reload player;
	player setammo [_weapon, (_magcount - 1)];
}; 

And here is the code I am using for the key press, right now it is set to Numpad 5.



MY_KEYDOWN_FNC = {
   switch (_this) do {

               //Numpad 5
       case 76: {
           nul = [] execVM "\userconfig\tacticalreload.sqf";
       };


   };
};  

//Add an EventHandler to the main display...
waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown","_this select 1 call MY_KEYDOWN_FNC;false;"];   

Eventually I want to have this set to use the same key as reload so that you can still reload normal things in a normal way but tac-reload otherwise. I might need to uncommit "R" from the in game reload.

TLDR:

  • Working on tactical reload script.
  • Need help proofing and making it arma ready?
  • Is it possible to make it use the in game reload key?

Any help would be appreciated! Thank you.

Share this post


Link to post
Share on other sites

I seem to have gotten it to work for the most part. However I am having difficulties with if then statements.

How do you do more than one if then statement?

Share this post


Link to post
Share on other sites
I seem to have gotten it to work for the most part. However I am having difficulties with if then statements.

How do you do more than one if then statement?

You either want to use && (or AND) to add further details to the condition or use '||' (OR) to add an extra condition.

There's also 'switch'

As for the reload key, http://community.bistudio.com/wiki/actionKeys should help. As then you can retrieve the bind the player has assigned for reloading. You will want the script to return 'false' (the last thing in the script) if you want to override the engine execution of the key (so to stop the regular reload from happening)

Edited by Kerc Kasha

Share this post


Link to post
Share on other sites
You either want to use && (or AND) to add further details to the condition or use '||' (OR) to add an extra condition.

There's also 'switch'

As for the reload key, http://community.bistudio.com/wiki/actionKeys should help. As then you can retrieve the bind the player has assigned for reloading. You will want the script to return 'false' (the last thing in the script) if you want to override the engine execution of the key (so to stop the regular reload from happening)

Thanks for the reply, I've tried using the &&, however it still isn't doing what I want it to do. Maybe my programming knowledge is off but this is essentially what I want to have happen. Not the real deal, I don't have the script with me right now.

private [ blah blah blah all the variables ];
_hjoneinthechamber = true; \\ CFGweapon variable to allow weapon to tac reload to exclude launchers
_oneinthechamber;  \\ Does the weapon have one in the chamber
_magcapacity = getconfig >> CFGmagazines >> "count";
_unit = (vehicle player);
_weapon = currentweapon player;
_currentmagazine = _unit ammo _weapon;


//If reload is pressed and the weapon is empty, and none in the chamber
if (_hjoneinthechamber = false && _oneinthechamber = 0 && _currentmagazine < _magcapacity) then { //do empty reload stuff};

//If reload is pressed and the weapon is empty, but has one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine < _magcapacity) then { //do empty tac reload};

//If reload is pressed and the weapon is partially full, will have one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine < _magcapacity && _currentmagazine > 0) then { //do partial tac reload};

//If reload is pressed and the weapon is full, has one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine = _magcapacity) then { //do normal reload};

For whatever reason this does not work.

Share this post


Link to post
Share on other sites
Thanks for the reply, I've tried using the &&, however it still isn't doing what I want it to do. Maybe my programming knowledge is off but this is essentially what I want to have happen. Not the real deal, I don't have the script with me right now.

private [ blah blah blah all the variables ];
_hjoneinthechamber = true; \\ CFGweapon variable to allow weapon to tac reload to exclude launchers
_oneinthechamber;  \\ Does the weapon have one in the chamber
_magcapacity = getconfig >> CFGmagazines >> "count";
_unit = (vehicle player);
_weapon = currentweapon player;
_currentmagazine = _unit ammo _weapon;


//If reload is pressed and the weapon is empty, and none in the chamber
if (_hjoneinthechamber = false && _oneinthechamber = 0 && _currentmagazine < _magcapacity) then { //do empty reload stuff};

//If reload is pressed and the weapon is empty, but has one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine < _magcapacity) then { //do empty tac reload};

//If reload is pressed and the weapon is partially full, will have one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine < _magcapacity && _currentmagazine > 0) then { //do partial tac reload};

//If reload is pressed and the weapon is full, has one in the chamber
if (_hjoneinthechamber = true && _oneinthechamber = 1 && _currentmagazine = _magcapacity) then { //do normal reload};

For whatever reason this does not work.

In if statements/conditions you don't want to use '_oneinthechamber = true' as for one, '=' is defining the variable, what you were looking for is '==' which compares the variable, but you don't want that either as what you're checking is a bool(true or false) so all you need is if (_oneinthechamber) then

as _oneinthechamber will be either true or false and therefore doesn't need to be compared.

Share this post


Link to post
Share on other sites

I believe this is the reload key.

actionKeys "ReloadMagazine";

edit

I should of read everyone's posts instead of just the main one .... :/

Edited by Lala14

Share this post


Link to post
Share on other sites

also the issue that I have encountered is when using setAmmo you cannot go above magazine's max!

e.g.

6.5 30rnd mag trying to set it to 31 will not work. Unless that is you have a way ......

Share this post


Link to post
Share on other sites

A "Fired" EH should be enough to add the remaining bullet after the first is shot. You can't go to 31, but you can get back to 30 from 29.

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  

×