Jump to content
Sign in to follow this  
dreadedentity

[CODE SNIPPET] Using actionKeys && prevent players from reloading if magazine is full

Recommended Posts

Hello fellow content creators,

As all of us know, BI gives us pretty much complete freedom in how we can mod their game. Now, everyone that's been doing this for a while knows that as you allow more freedom, things become overall more complicated. The reason why is pretty simple, as you give more freedom, you have to have to add more commands to the game and the wiki. Eventually commands will get lost in the deep web and become extremely obscure.

So, I'm here again with another code snippet, and maybe a much longer post. The reason I've embarked on this quest was because a few days ago someone was asking about how to disable players from accessing the map. I came up with 2 solutions but both were ugly, and barely working. So I dug deep into the wiki, and I'm back now and going to do a full write-up. Okay now, first, I'm going to show off some code and then go through it (possibly line-by-line), then finish off with some closing notes.

Prevent players from reloading if they have a full magazine:

_doNotReload = (findDisplay 46) displayAddEventHandler ["KeyDown",
{
_handled = false;
if ((_this select 1) in (actionKeys "ReloadMagazine")) then
{
	_magazine = [];
	{
		if ((_x select 2) && ((_x select 4) == currentMuzzle player)) then
		{
			_magazine = _x;
		};
	}forEach (magazinesAmmoFull player);
	if (getNumber (configfile >> "CfgMagazines" >> (_magazine select 0) >> "count") == (_magazine select 1)) then
	{
		_handled = true;
		hint "Your magazine is full, don't reload";
	};
};
if (_handled) then
{
	[]spawn
	{
		sleep 1;
		hintSilent "";
	};
};
_handled
}];

Okay, first off:

_doNotReload = (findDisplay 46) displayAddEventHandler ["KeyDown",

This adds a display event handler with the ID stored in a variable called _doNotReload, which is necessary if you ever want to remove the event handler. findDisplay will return the actual "display" value when you give the command a display IDD, using this you can add your event handler, 46 is the default, main game display. Seriously, click on that link and read it for yourself, it's hard to explain. "KeyDown" is the event handler we'd like to add, click on that link for a full list.

DO NOTE: In my code I have "KeyDown" instead of "onKeyDown". I have tested "onKeyDown", "onKeyUp", "onMouseButtonDown", and "onMouseButtonUp" and none of them work unless you drop 'on' in front of them, suggesting that all of them on that list will work if you don't put 'on' in the beginning (but I only tested those 4).

Actually, explaining all this sucks. Read over the code and if you have questions feel free to leave a comment and I'll answer to the best of my ability.

A few reference pages from the wiki.

actionKeys, DIK KeyCodes, A2 CfgDefaultKeysMapping

Now, that last page is pretty much for just Arma 2, but while writing this post I found out how to get to the Arma 3 default keys.

Go to the configViewer (cog symbol at the top of the editor), scroll to CfgDefaultKeysPresets -> Arma 3 -> Mappings. From there you can look at all of the actions in the game.

By the way, here is that completed code that disables a player's map (and the GPS if they are bound to the same key, like the default keys are set up).

disableMap = (findDisplay 46) displayAddEventHandler ["KeyDown",
{
_handled = false;
if ((_this select 1) in (actionKeys "ShowMap")) then
{
	hint "You cannot open the map";
	_handled = true;
};
if (_handled) then
{
	[]spawn //this spawn clears the hint after 1 second, in case anyone was wondering and couldn't figure it out
	{
		sleep 1;
		hintSilent "";
	};
};
_handled
}];

Hope that helps guys, enjoy.

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

Nice script man. You could have saved youself a lot of bother though if you used the "ammo" command:

_ret = (findDisplay 46) displayAddEventHandler ["KeyDown", {
_overRide = false;
if ((_this select 1) in (actionKeys "ReloadMagazine")) then {
	_maxAmmo = getNumber(configFile/"CfgMagazines"/currentMagazine player/"count");
	if (player ammo currentWeapon player == _maxAmmo) then {
		player groupChat "there's no need to reload you silly cunt";
		_overRide = true;
	};
};
_overRide
}];

Share this post


Link to post
Share on other sites
You could have saved youself a lot of bother though if you used the "ammo" command:

Maybe I secretly like to do things the complicated way like everyone else ;)

Seriously though, I forgot about that command lol

Thanks for the feedback guys

Share this post


Link to post
Share on other sites
Maybe I secretly like to do things the complicated way like everyone else

Your script was fine - it works :)

TBH - Who gives a shit about efficiency? Get it working and then improve it is my mantra. Too many people on this forum make really pretty code and then find they have to defile it a few months later much to their disgust. Much easier to make a pig-bastard of code and then improve/rewrite as I go along according to how things in the game change.

You should see some of the code abortions I've spewed up onto this forum in the (very recent) past. In fact, if you check through my BI forum history, then you will find some massive stinkers I can tell you for a fact! :)

  • Like 1

Share this post


Link to post
Share on other sites

^^ this. I think nearly every snippet or project posted can be improved in one way or another. There's just so many ways to do things. It's all about preference and capabilities. Which isn't necessarily a bad thing :). All's that matters is that you understand what you're writing and it achieves the desired goal.

Share this post


Link to post
Share on other sites

Nice script, though I personally don't have much use for this particular one.

As far as I am concerned, a player can reload their full magazines as often as he wants (sometimes you also do that to switch to a different type of ammunition).

However, the way you did it, is still interesting and could easily be adapted for something else.

Share this post


Link to post
Share on other sites
However, the way you did it, is still interesting and could easily be adapted for something else.

Exactly! Using the actionKeys method, you'll even be able to capture the input if the player remaps his keys (no doubt some players do)

Share this post


Link to post
Share on other sites

just a smal question, i ve been searching a lot and did not found what i want, i have a small script displaying help screen on a SP mission,

i would like to use something like :

 

waituntil... (esc key pressed)  then countinue the script.

 

have any idea?

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  

×