Jump to content
Sign in to follow this  
ShinShin

Select & Give Player Random Weapon, Mags & Items Script. Need Help please!

Recommended Posts

Okay, So i'm working on this Script for Combined Operations to Randomly Select the Sort of weapon...

(Eg: Rifle, Sniper, MG, Handgun) and AFTER that... to THEN randomly select the weapon within that category to give to the player along with and random amount of the magazines for that weapon.

It's been 3 Days now since i've been searching for any further help/refrences via google && here. I'm getting frustrated because i'm not too sure how the "case: 1" and all that other randomizing shit works.

Any help... Please, Would be greatly appreciated.

I'm not looking for anything similar that's already made... Just this one (Mine) please & thank you in advanced.

Script:

R_Weapons.sqf

/*
[color="#B22222"]RANDOM WEAPONS[/color]
[color="#8B4513"]BY: INDIANA[/color]
*/

if (! isServer) exitwith {};

private	["_Primary","_Secondary","_Ammo","_Rifles","_Snipers","_Pistols","_RifleMags","_SniperMags","_PistolMags","_Items","_RND","_I"];

_Player	=	_this select 0;

[color="#008000"]//Primary Weapons Array[/color]
case 1: 		{
_Rifles	=		[
				[color="#008000"]//BLUFOR[/color]
				"M16A2", "m16a4",
				"m16a4_acg", "M4A1",
				"M4A1_AIM", "M4A1_AIM_CAMO",
				"M4A1_AIM_SD_CAMO", "m8_Carbine",
				"m8_Compact", "G36A",
				"G36K", "G36C",
				"MP5A5", "MP5SD",
				[color="#008000"]//OPFOR[/color]
				"AK_47_S", "AK_74",
				"AKS_74_Kobra", "AKS_74_PSO",
				"AKS_74_U", "AKS_74_UN_Kobra",
				"AK_47_M", "AK_47_S",
				"AK_107_Kobra", "AK_107_PSO",
				"bizon", "Bizon_Silenced"
			];
		};

case 2:			{
_Snipers =		[
				[color="#008000"]//BLUFOR[/color]
				"M4SPR", "m8_Sharpshooter",
				"DMR", "M24",
				"M40A3", "m107",
				[color="#008000"]//OPFOR[/color]
				"SVD", "huntingrifle",
				"SVD_Camo", "ksvk",
				"VSS_Vintorez"
			];
		};

[color="#008000"]//Secondary Weapons Array[/color]
case 3:			{
_Pistols =		[
				[color="#008000"]//BLUFOR[/color]
				"Colt1911", "M9",
				"M9SD", 
				[color="#008000"]//OPFOR[/color]
				"Makarov",
				"MakarovSD"
			];
		};

[color="#008000"]//Magazines Array (Must Macth Order of Weapons)[/color]
_RifleMags =			[
					[color="#008000"]//BLUFOR[/color]
					"30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag",
					"30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag",
					"30Rnd_556x45_Stanag", "30Rnd_556x45_Stanag",
					"30Rnd_556x45_StanagSD", "30Rnd_556x45_Stanag",
					"30Rnd_556x45_Stanag", "30Rnd_556x45_G36",
					"30Rnd_556x45_G36", "30Rnd_556x45_G36",
					"30rnd_9x19_MP5", "30rnd_9x19_MP5SD",
					[color="#008000"]//OPFOR[/color]
					"30Rnd_762x39_AK47", "30Rnd_545x39_AK",
					"30Rnd_545x39_AK", "30Rnd_545x39_AK",
					"30Rnd_545x39_AK", "30Rnd_545x39_AKSD",
					"30Rnd_762x39_AK47", "30Rnd_762x39_AK47",
					"30Rnd_545x39_AK", "30Rnd_545x39_AK",
					"64Rnd_9x19_Bizon", "64Rnd_9x19_SD_Bizon"
				];

_SniperMags =			[
					[color="#008000"]//BLUFOR[/color]
					"20Rnd_556x45_Stanag", "20Rnd_556x45_Stanag",
					"20Rnd_762x51_DMR", "5Rnd_762x51_M24",
					"5Rnd_762x51_M24", "10Rnd_127x99_M107",
					[color="#008000"]//OPFOR[/color]
					"10Rnd_762x54_SVD", "5x_22_LR_17_HMR",
					"10Rnd_762x54_SVD", "5Rnd_127x108_KSVK",
					"10Rnd_9x39_SP5_VSS"
				];

_PistolMags =			[
					[color="#008000"]//BLUFOR[/color]
					"7Rnd_45ACP_1911", "15Rnd_9x19_M9",
					"15Rnd_9x19_M9SD",
					[color="#008000"]//OPFOR[/color]
					"8Rnd_9x18_Makarov", "8Rnd_9x18_MakarovSD"
				];

[color="#008000"]//Items Array[/color]
_Items =		[
				[color="#008000"]//Equipment[/color]
				"Binocular", "ItemCompass",
				"ItemGPS", "ItemMap",
				"ItemRadio", "ItemWatch",
				"NVGoggles"
			];

Share this post


Link to post
Share on other sites

Randomized weapons makes Baby Jeebus cry. :)

switch is like this:

switch (_weaponType) do {
    case "rifles": { // do rifle stuff here 
                      };
    case "pistols": { // do pistol stuff here 
                        };
};

How you format it exactly depends on what all your inputs are, which isn't clear from your code.

However, I feel you're going about this entirely wrong. You should look into reading cfgWeapons where you'll be able to pick out any randomly available weapon by type and it's appropriate ammo. That way you don't have a humongous hard coded list of ammo types that need to be in the same order as pistol types and all that. Now, if you allow multiple addons that not everyone might have some people will have invisible guns perhaps, but yeah.

Share this post


Link to post
Share on other sites

Case: 1 , has nothing to do with random. It is part of the syntax for a switch statement. That is one of the control structures in .sqf as well as other programing lang.

http://community.bistudio.com/wiki/Control_Structures

Here is the random command:

http://community.bistudio.com/wiki/random

To be honest, your going to need to scrap everything except your arrays of weps. I'm on my phone right now so I can't give examples but, you need to rewrite it to select a random index in your main wep array then use that same index to select yor mags for your primary, you can then generate a random count for said mags. The repeat the process for your sec. And tir. Items

---------- Post added at 12:36 PM ---------- Previous post was at 12:30 PM ----------

Lol your right Kylania it does bring a tear to his eye.

I also think this would be better suited as a server function that returned a list of gear that you could just add to the player. If you ran it on the server you could just pull from its config and that would take care of the invis weapon issue.

Share this post


Link to post
Share on other sites

Alright, So basicly. I Assume it would just be easier to Include all the Primary weapons into ONE Array & Work from there. then Samething with Sidearms (Secondarys)?

Share this post


Link to post
Share on other sites
If you ran it on the server you could just pull from its config and that would take care of the invis weapon issue.

My friend has some missions and he hates weapons boxes, so he just pulls from the client config to add every available weapon to a single box. At first this wasn't too bad, but when you're talking two games, three DLC, like eight RH weapon addons, various island weapon addons the list gets insane! I keep forgetting to add RH weapons and see my friends shooting nothing. :P

Share this post


Link to post
Share on other sites
My friend has some missions and he hates weapons boxes, so he just pulls from the client config to add every available weapon to a single box. At first this wasn't too bad, but when you're talking two games, three DLC, like eight RH weapon addons, various island weapon addons the list gets insane! I keep forgetting to add RH weapons and see my friends shooting nothing. :P

Lol yea, I worked on this exact same problem, your right in some of my tests I was parsing over 8000 items in the config. With ace and co,acre etc...

I have a working example where I just parse the config of the of the server and add it to a box, I have been working on it to try and get the speed and net work performance to acceptable levels. I want to try and limit it further and get it to run a little smoother.

But by just running it on the server and using the addweapongloable commands it solves the issues about other players having diff addons. As long as the mission is set up with the right addon requirements

Share this post


Link to post
Share on other sites

Instead of storing weapons and mags in separate arrays, better store them like this

weaponList = [
["Weapon1", "Mag1"],
["Weapon2", "Mag2"]
];

For selecting random value from array go with:

_weapon = weaponList select floor(random(count weaponList));

And with switch() you can go with

//This should be here since we will initialize _weapon in switch() scope instead of main scope
//where _weapon will be used, otherwise _weapon will be "invisible" outside of switch()
private ["_weapon"];

weaponListRifles = [
["Weapon1", "Mag1"],
["Weapon2", "Mag2"]
];
weaponListSnipers = [
["Weapon3", "Mag3"],
["Weapon4", "Mag4"]
];

switch(round(random 1)) do { //Selects either 0 or 1
case 0: {
	_weapon = weaponListRifles select floor(random(count weaponListRifles));
};
case 1: {
	_weapon = weaponListSnipers select floor(random(count weaponListSnipers));
};
}

// Give weapon here
player addWeapon (_weapon select 0);

// Give 4 mags (_i will be 0, 1, 2, 3 which is 4 times in total)
for "_i" from 0 to 3 do {
player addMagazine (_weapon select 1);
};

Didn't test the code but that's the basic idea.

Edited by SaMatra

Share this post


Link to post
Share on other sites

Thanks Guys!

SaMatra, I Managed to work it out with your approach. Thanks alot!

Share this post


Link to post
Share on other sites

I've been searching for days, and also last year too. I've been trying to mess with all kinds of other scripts including this one. But I cannot seem to be able to randomly spawn weapons into an ammo box.

And I was just really hoping if you would be able to help me.

I also did not want to start a new thread since this is pretty similar to what I want to do.

Instead of storing weapons and mags in separate arrays, better store them like this

weaponList = [
["Weapon1", "Mag1"],
["Weapon2", "Mag2"]
];

For selecting random value from array go with:

_weapon = weaponList select floor(random(count weaponList));

And with switch() you can go with

//This should be here since we will initialize _weapon in switch() scope instead of main scope
//where _weapon will be used, otherwise _weapon will be "invisible" outside of switch()
private ["_weapon"];

weaponListRifles = [
["Weapon1", "Mag1"],
["Weapon2", "Mag2"]
];
weaponListSnipers = [
["Weapon3", "Mag3"],
["Weapon4", "Mag4"]
];

switch(round(random 1)) do { //Selects either 0 or 1
case 0: {
	_weapon = weaponListRifles select floor(random(count weaponListRifles));
};
case 1: {
	_weapon = weaponListSnipers select floor(random(count weaponListSnipers));
};
}

// Give weapon here
player addWeapon (_weapon select 0);

// Give 4 mags (_i will be 0, 1, 2, 3 which is 4 times in total)
for "_i" from 0 to 3 do {
player addMagazine (_weapon select 1);
};

Didn't test the code but that's the basic idea.

Share this post


Link to post
Share on other sites

Hi guys I am also in need of something like this and I have filled in what I understand but I am still unsure how to actually activate it in-game on a player? I assume I make it a sqf and then a ini with the ExecVM for whateveer the name is? Here is the script except with the stuff I need and just need to check if it is correct?

weaponListPistols = [

["ACE_MP7", "ACE_40Rnd_B_46x30_MP7"],

["UZI_EP1", "ACE_30Rnd_9x19_S_UZI"],

["ACE_Glock18", "ACE_33Rnd_9x19_G18"]

];

weaponListRifles = [

["ACE_M4A1_C", "30Rnd_556x45_Stanag"],

["ACE_MP5A5", "ACE_30Rnd_9x19_S_MP5"],

["ACE_UMP45", "ACE_25Rnd_1143x23_B_UMP45"],

["ACE_KAC_PDW", "ACE_30Rnd_6x35_B_PDW"]

];

switch(round(random 1)) do { //Selects either 0 or 1

case 0: {

_weapon = weaponListRifles select floor(random(count weaponListRifles));

};

case 1: {

_weapon = weaponListPistols select floor(random(count weaponListPistols));

};

}

// Give weapon here

player addWeapon (_weapon select 0);

// Give 4 mags (_i will be 0, 1, 2, 3 which is 4 times in total)

for "_i" from 3 to 4 do {

player addMagazine (_weapon select 1);

};

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  

×