Jump to content
Sign in to follow this  
tpw

Grenade keybinding

Recommended Posts

OK after one too many times being killed after !@#$%ing up weapon selection with "F" under pressure, I decided to have a go at a simple weapon keybinding script similar to ACEs:

Shift-F1 - primary weapon

Shift-F2 - grenade launcher

Shift-F3 - secondary weapon

Shift-F4 - grenade

Here's the script, It works for me, feel free to change it to your tastes.

//Select weapons with shift-F1 to F4 
// TPW 20120103

if (!isServer) exitWith {};

waitUntil {!isNull(findDisplay 46)};

(findDisplay 46) displayAddEventHandler ["keyDown", "_this call tpw_keypress"];

tpw_keypress = 
{
private["_shift","_dik"];
_dik = _this select 1;
_shift = _this select 2;
tpw_kp = false;

if _shift then {
switch _dik do {
case 59 : {[0] call wepselect; tpw_kp = true}; //rifle
case 60 : {[1] call wepselect; tpw_kp = true}; //launcher
case 61 : {player selectweapon (secondaryweapon player); tpw_kp = true}; //secondary
case 62 : {player selectweapon "handgrenademuzzle"; tpw_kp = true}; //grenade
};
};
tpw_kp;
};

wepselect = {
private ["_muzzles","_pw","_weapon","_slc"]; 
_muzzles = [];
_slc = _this select 0;
_pw = (primaryWeapon player);
if (_pw != "") then {
_muzzles = getArray(configFile>>"cfgWeapons" >> _pw >> "muzzles");
_m1 = _muzzles select 0; if (_m1 == "this") then {_muzzles set[0,_pw]}; 
_weapon = _muzzles select _slc; 
player selectweapon _weapon;
}; 
};

EDIT: So after a few problems as outlined in posts below, I finally got the script functioning well. The code above is the final working version as of 20120103.

I also created a signed PBO version for those who want to use it without scripting it in:

http://www.gamefront.com/files/21147568/simple_weapon_select_100.zip

This version requires CBA.

It works in SP, I assume it will work in MP but haven't tried. Please let me know if there are any issues

Edited by tpw

Share this post


Link to post
Share on other sites

Thanks guys, I didn't think anyone would give a rats.

I'm working on a few improvements and fine tunes and will put them up shortly.

Share this post


Link to post
Share on other sites

The current grenade/smoke selection method in the game is just plain shite. My real life AR-15 does not have "grenades" or "smoke" on the fire selector. In RL I wouldn't have to keep going in circles to select and throw grenades.

I think your script will certainly solve this for a lot of us that don't like the current setup.

Share this post


Link to post
Share on other sites

Thanks twirly. I've updated the code slightly, please give it a whirl (twirl?) and let me know if it works for you.

Share this post


Link to post
Share on other sites

I'm getting strange results. The switching is different for different guys.

For a soldier with a Rifle and Grenades I can't seem to switch back to the Rifle once I switch to Grenades.

For a soldier with a Rifle, Launcher and Grenades I can switch between the Launcher and the Grenades no worries... but never to the Rifle.

Something weird is going on. I don't have much time these days for scripting... but will have a think and a fiddle as soon as I can.

Share this post


Link to post
Share on other sites

OK I added a bit of debugging code and had a look at it.

The problem is this code:

_muzzles = getArray(configFile>>"cfgWeapons" >> _pw >> "muzzles");

_muzzles should be an array containing ["rifle","launcher","grenade throw"]

What seems to be happening is that depending on how you equip your player, the code returns an array ["this","launcher","grenade throw"]

The item "this" does bugger all in terms of allowing you to select a weapon. You can still switch between launcher and grenades though.

I've no idea why this happens and am looking to try to circumvent it!

---------- Post added at 16:36 ---------- Previous post was at 14:56 ----------

OK so this is very weird. The script as shown below behaves perfectly, as long as the player does not exchange weapons (from an ammobox, dead unit, or via script). If you do, the wheels fall off and the shift-F1 will not switch back to the rifle. If however, you switch back to your original rifle, the shift-F1 will work as advertised.

The script is written so that element 0 in the _muzzles array is replaced with the name of the rifle + muzzle. If your rifle is RH_HK416acog then the script inserts RH_HK416acogmuzzle. This prevents "this" from being used to selectweapon.

I'm at a loss here, so if anyone has any ideas I'd be grateful.

(findDisplay 46) displayAddEventHandler ["keyDown", "_this call tpw_keypress"];

tpw_keypress = 
{
private["_shift","_dik"];
_dik = _this select 1;
_shift = _this select 2;
tpw_kp = false;

if _shift then {
switch _dik do {
case 59 : {[0] call wepselect; tpw_kp = true}; //rifle
case 60 : {[1] call wepselect; tpw_kp = true}; //launcher
case 61 : {player selectweapon (secondaryweapon player); tpw_kp = true}; //secondary
case 62 : {[2] call wepselect; tpw_kp = true}; //grenade
};
};
tpw_kp;
};

wepselect = {
private ["_muzzles","_pw","_pwm","_weapon","_slc"]; 
_slc = _this select 0;
_pw = (primaryWeapon player);
_pwm = format ["%1muzzle",_pw];
_muzzles = getArray(configFile>>"cfgWeapons" >> (primaryWeapon player) >> "muzzles");
_muzzles set[0,_pwm]; 
_weapon = _muzzles select _slc; 
player selectweapon _weapon; 
};

---------- Post added at 17:23 ---------- Previous post was at 16:36 ----------

OK, finally sorted it out!

"this" is returned when the muzzlename is the same as the weapon name. In which case I just needed to replace "this" with the name of the weapon (no "muzzle") in the _muzzles array.

Final working code below:

(findDisplay 46) displayAddEventHandler ["keyDown", "_this call tpw_keypress"];

tpw_keypress = 
{
private["_shift","_dik"];
_dik = _this select 1;
_shift = _this select 2;
tpw_kp = false;

if _shift then {
switch _dik do {
case 59 : {[0] call wepselect; tpw_kp = true}; //rifle
case 60 : {[1] call wepselect; tpw_kp = true}; //launcher
case 61 : {player selectweapon (secondaryweapon player); tpw_kp = true}; //secondary
case 62 : {player selectweapon "handgrenademuzzle"; tpw_kp = true}; //grenade
};
};
tpw_kp;
};

wepselect = {
private ["_muzzles","_pw","_weapon","_slc"]; 
_muzzles = [];
_slc = _this select 0;
_pw = (primaryWeapon player);
_muzzles = getArray(configFile>>"cfgWeapons" >> _pw >> "muzzles");
[b]_m1 = _muzzles select 0; if (_m1 == "this") then {_muzzles set[0,_pw]}; [/b]
_weapon = _muzzles select _slc; 
player selectweapon _weapon; 
};

Edited by tpw

Share this post


Link to post
Share on other sites

Well done. Seems to work flawlessly now. I'll be using this from now on.

Thanks for doing this...it's been something on the back burner for me for quite a while.... but now I'll just use yours :)

Share this post


Link to post
Share on other sites
Where is the script place as I'm getting nothing.

When you mean getting nothing, do you mean it's not working for you?

If you save the last (fixed) code block above into your mission directory as wepselect.sqf, and then call it from your init.sqf with nul = [] execvm "wepselect.sqf" it should work just fine. You can just paste nul = [] execvm "wepselect.sqf" into the init field of the player if you don't want/have an init.sqf.

Drop me a PM if you still can't get it to work.

Share this post


Link to post
Share on other sites

Still not working , if I place a hint after display line at the top of the script it shows up but if I place it within the tpw_keypress = { function I get nothing.

I'll try later when I reboot PC as I'm thinking something is messed up.

Share this post


Link to post
Share on other sites

Had the same problem but just got it sorted. The script needs to wait until the display is active. If you change the init file to this, it will work:

waitUntil {!isNull(findDisplay 46)};

nul = [] execVM "wepselect.sqf";

Good script by the way - thanks :)

Share this post


Link to post
Share on other sites
Had the same problem but just got it sorted. The script needs to wait until the display is active. If you change the init file to this, it will work:

waitUntil {!isNull(findDisplay 46)};

nul = [] execVM "wepselect.sqf";

Good script by the way - thanks :)

Thanks Das Attorney - you've been a lot of help with my scripting abominations in the past and are still helping!

Share this post


Link to post
Share on other sites

Great idea !

But is there a way to change firemode (Single -> Full) via just clicking SHIFT+F1 again ?

Currently you have to use the F key to switch between them.

Oh, and i think this is MP (respawn) and JIP compatible, correct ?

Edited by Wiggum

Share this post


Link to post
Share on other sites

After adding the wait that allowed the script to run but I still couldn't select the grenade until I changed to a unit that has a launcher as well.

Share this post


Link to post
Share on other sites
After adding the wait that allowed the script to run but I still couldn't select the grenade until I changed to a unit that has a launcher as well.

Wow, you're really not having very good luck!

Just to be sure, you really are using the final "working" version of the code:

if (!isServer) exitWith {};

waitUntil {!isNull(findDisplay 46)};

(findDisplay 46) displayAddEventHandler ["keyDown", "_this call tpw_keypress"];

tpw_keypress = 
{
private["_shift","_dik"];
_dik = _this select 1;
_shift = _this select 2;
tpw_kp = false;

if _shift then {
switch _dik do {
case 59 : {[0] call wepselect; tpw_kp = true}; //rifle
case 60 : {[1] call wepselect; tpw_kp = true}; //launcher
case 61 : {player selectweapon (secondaryweapon player); tpw_kp = true}; //secondary
case 62 : {player selectweapon "handgrenademuzzle"; tpw_kp = true}; //grenade
};
};
tpw_kp;
};

wepselect = {
private ["_muzzles","_pw","_weapon","_slc"]; 
_muzzles = [];
_slc = _this select 0;
_pw = (primaryWeapon player);
_muzzles = getArray(configFile>>"cfgWeapons" >> _pw >> "muzzles");
_m1 = _muzzles select 0; if (_m1 == "this") then {_muzzles set[0,_pw]}; 
_weapon = _muzzles select _slc; 
player selectweapon _weapon; 
};

I just tried this with a number of different weapons, with and without grenade launchers, and it works as advertised. One of my previous incarnations of the script used a different command for switching to the handgrenades, which only worked for rifles with grenade launchers.

I've got a few more bugs to iron out and will try to make a PBO addon version, for all 6 of you who are interested in the system :)

Share this post


Link to post
Share on other sites

Cheers, I could have sworn I had the last version but I must have screwed up.

Thanks for your help.

Share this post


Link to post
Share on other sites

For those interested, here's a signed PBO version. http://www.gamefront.com/files/21147568/simple_weapon_select_100.zip

I'm not much chop at making PBOs so I adapted an earlier Dynamic View Distance PBO of mine which Das Attorney helped me put together. As such, it requires CBA, but I'm sure there's a way around this.

Share this post


Link to post
Share on other sites

Wrong thread... :)

Edited by Rydygier

Share this post


Link to post
Share on other sites

tpw, your dl link for the PBO version does not work, no file found...please make a new one, I would pretty much like to try this in non-ACE SP missions...

Share this post


Link to post
Share on other sites

Hi tpw, the 2nd download link seems not work either, would you please upload it to armaholic or where so ever that provide stable download service?

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  

×