Jump to content
  • 0

Static Crates Random loot


Cyrus

Question

Im looking for an easy, effective way to randomize loot (weapons, ammo, toolbelt items, food, meds and building supplies) in static crate. I have a placed crate already and populated with stuff but i would like to have this random on spawn. Also i would like to know how to have this crate stay for 2 hours then destroy and respawn 15 min later. This is what my customLootCrate.sqf looks like :

Spoiler
Quote

if (isServer) then {
        _vehicle_9090 = objNull;
        if (true) then
        {
          _this = createVehicle ["USSpecialWeaponsBox", [4434.3223, 10632.069, 0], [], 0, "CAN_COLLIDE"];
          _vehicle_9090 = _this;
          _this setDir -182.5;
                  _vehicle_9090 setVariable ["ObjectID","1",true];
                  _vehicle_9090 setVariable ["permaLoot",true];

          clearWeaponCargoGlobal _this;
          clearMagazineCargoGlobal _this;

        

          _this addWeaponCargoGlobal ["BAF_LRR_scoped",2]; //
        
          _this addWeaponCargoGlobal ["DMR_DZ",1];
          _this addWeaponCargoGlobal ["[M24_des_EP1",2];
          _this addWeaponCargoGlobal ["[FNFAL_DZ",2];
          _this addWeaponCargoGlobal ["[Mk48_CCO_DZ",2];
          _this addWeaponCargoGlobal ["[M240_DZ",2];
          _this addWeaponCargoGlobal ["[G17_FL_DZ",2];
          _this addWeaponCargoGlobal ["[M9_SD_DZ",2];
          
          _this addMagazineCargoGlobal ["20Rnd_762x51_DMR",20];
          _this addMagazineCargoGlobal ["5Rnd_762x51_M24",20];
          _this addMagazineCargoGlobal ["20Rnd_762x51_FNFAL",20];
          _this addMagazineCargoGlobal ["100Rnd_762x54_PK",20];
          _this addMagazineCargoGlobal ["100Rnd_762x51_M240",20];
          _this addMagazineCargoGlobal ["17Rnd_9x19_glock17",20];
          _this addMagazineCargoGlobal ["15Rnd_9x19_M9SD",20];

      
          _this addWeaponCargoGlobal ["ItemToolbox",5];
          _this addWeaponCargoGlobal ["ItemCrowbar",5];
          _this addWeaponCargoGlobal ["ItemEtool",5];
          _this addWeaponCargoGlobal ["Binocular_Vector",5];
          _this addWeaponCargoGlobal ["ItemGPS",5];
          _this addWeaponCargoGlobal ["ItemHatchet",5];
          _this addWeaponCargoGlobal ["NVGoggles",5];
          
          _this addMagazineCargoGlobal ["ItemLockbox",2];
          _this addMagazineCargoGlobal ["ItemPole",20];
          _this addMagazineCargoGlobal ["ItemComboLock",2];
          _this addMagazineCargoGlobal ["ItemGenerator",3];
          _this addMagazineCargoGlobal ["ItemVault",2];
          _this addMagazineCargoGlobal ["ItemCorrugated",20];
          _this addMagazineCargoGlobal ["plot_pole_kit",2];
          _this addMagazineCargoGlobal ["ItemWire",10];
          _this addMagazineCargoGlobal ["ItemTankTrap",10];
          _this addMagazineCargoGlobal ["ItemSandbag",10];
          _this addMagazineCargoGlobal ["CinderBlocks",40];
          _this addMagazineCargoGlobal ["MortarBucket",20];
  
          
          _this addMagazineCargoGlobal ["ItemBloodbag",5];
          _this addMagazineCargoGlobal ["ItemEpinephrine",5];
          _this addMagazineCargoGlobal ["ItemMorphine",5];
          _this addMagazineCargoGlobal ["ItemPainkiller",5];
          _this addMagazineCargoGlobal ["ItemAntibiotic",5];
          _this addMagazineCargoGlobal ["ItemBandage",5];
          _this addMagazineCargoGlobal ["ItemSeaBassCooked",10];
          _this addMagazineCargoGlobal ["ItemTunaCooked",10];
          _this addMagazineCargoGlobal ["ItemSodaR4z0r",5];
          _this addMagazineCargoGlobal ["ItemSodaMdew",5];
          _this addMagazineCargoGlobal ["ItemSodaOrangeSherbet",5];
          _this addMagazineCargoGlobal ["ItemSodaRbull",5];
          _this addMagazineCargoGlobal ["PartEngine",5];
          _this addMagazineCargoGlobal ["PartVRotor",5];
          _this addMagazineCargoGlobal ["PartGlass",5];
          _this addMagazineCargoGlobal ["PartWheel",5];
          _this addMagazineCargoGlobal ["PartGeneric",5];
          _this addMagazineCargoGlobal ["ItemJerrycan",5];
          
          _this setPos [4434.3223, 10632.069, 0];
 
        };
};

Thanks for any help in advance.

 

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Setup arrays and pick a random item from it. You can use the BIS_fnc_selectRandom function to do that: https://community.bistudio.com/wiki/BIS_fnc_selectRandom

_someWeaponArray = [
	"DMR_DZ",
	"M24_des_EP1",
	"FNFAL_DZ",
	"Mk48_CCO_DZ",
	"M240_DZ",
	"G17_FL_DZ",
	"M9_SD_DZ"
];

//Pick 1 random item from it
_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;

//Optionally remove it from the array afterwards
_someWeaponArray = _someWeaponArray - [_randomWeapon];

 

Link to comment
Share on other sites

  • 0
25 minutes ago, DAmNRelentless said:

Setup arrays and pick a random item from it. You can use the BIS_fnc_selectRandom function to do that: https://community.bistudio.com/wiki/BIS_fnc_selectRandom


_someWeaponArray = [
	"DMR_DZ",
	"M24_des_EP1",
	"FNFAL_DZ",
	"Mk48_CCO_DZ",
	"M240_DZ",
	"G17_FL_DZ",
	"M9_SD_DZ"
];

//Pick 1 random item from it
_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;

//Optionally remove it from the array afterwards
_someWeaponArray = _someWeaponArray - [_randomWeapon];

 

@DAmNRelentless Thanks for the reply. That makes sense. for arguments sake if i want to populate the crate with multiple random items from the same array to i just specify the amount like this? :

_randomWeapon = [_someWeaponArray,10] call BIS_fnc_selectRandom;

Essentially requesting 10 random weapons from the array?

Link to comment
Share on other sites

  • 0

@Cyrus I don't think that would work since it's not matching the function's syntax anymore. But due to the fact that different amounts of items are stored in a array as well, you could pick a random array.

_someWeaponArray = [
	["DMR_DZ",2],
	["M24_des_EP1,4]",
	["FNFAL_DZ",2],
	["Mk48_CCO_DZ",1],
	["M240_DZ,1"],
	["G17_FL_DZ",5],
	["M9_SD_DZ",5]
];

//Pick 1 random item array from it format: ["weapon",amount]
//Keep in mind that it's saved as array this time
_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;

//Optionally remove it from the array afterwards
//You don't need to set it as array now. Since it is already an array, you can just do
_someWeaponArray = _someWeaponArray - _randomWeapon;

//Adding the item to the crate if it's already set up
//the variable is enough because it's already an array
_this addWeaponCargoGlobal _randomWeapon;

Now to not do this everytime, you could specify how many items from which arrays are picked. There can't be duplicate ones since the code is already removing the _randomWeapon from the origin array.

Spoiler

_someWeaponArray = [
	["DMR_DZ",2],
	["M24_des_EP1,4]",
	["FNFAL_DZ",2],
	["Mk48_CCO_DZ",1],
	["M240_DZ,1"],
	["G17_FL_DZ",5],
	["M9_SD_DZ",5]
];

//Always pick 3 items from an array
_itemCounter = 0;
while {_itemCounter < 3} do {
	_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;
	_someWeaponArray = _someWeaponArray - _randomWeapon;
	_crate addWeaponCargoGlobal _randomWeapon; //store you crate in a variable, don't know if _this could also be the itemCounter cause it's in an if-statement
	_itemCounter = _itemCounter + 1;
};

//Always pick an amount depending on the array size
//If you array has 5 items, it will pick 1 from it, if it has 10 items it will pick 2 for it (adjustable by changing the 5)
_itemCounter = 0;
while {_itemCounter < (round(count _someWeaponArray / 5))} do {
	_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;
	_someWeaponArray = _someWeaponArray - _randomWeapon;
	_crate addWeaponCargoGlobal _randomWeapon;
	_itemCounter = _itemCounter + 1;
};

 

 

Link to comment
Share on other sites

  • 0
1 hour ago, DAmNRelentless said:

@Cyrus I don't think that would work since it's not matching the function's syntax anymore. But due to the fact that different amounts of items are stored in a array as well, you could pick a random array.


_someWeaponArray = [
	["DMR_DZ",2],
	["M24_des_EP1,4]",
	["FNFAL_DZ",2],
	["Mk48_CCO_DZ",1],
	["M240_DZ,1"],
	["G17_FL_DZ",5],
	["M9_SD_DZ",5]
];

//Pick 1 random item array from it format: ["weapon",amount]
//Keep in mind that it's saved as array this time
_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;

//Optionally remove it from the array afterwards
//You don't need to set it as array now. Since it is already an array, you can just do
_someWeaponArray = _someWeaponArray - _randomWeapon;

//Adding the item to the crate if it's already set up
//the variable is enough because it's already an array
_this addWeaponCargoGlobal _randomWeapon;

Now to not do this everytime, you could specify how many items from which arrays are picked. There can't be duplicate ones since the code is already removing the _randomWeapon from the origin array.

  Reveal hidden contents


_someWeaponArray = [
	["DMR_DZ",2],
	["M24_des_EP1,4]",
	["FNFAL_DZ",2],
	["Mk48_CCO_DZ",1],
	["M240_DZ,1"],
	["G17_FL_DZ",5],
	["M9_SD_DZ",5]
];

//Always pick 3 items from an array
_itemCounter = 0;
while {_itemCounter < 3} do {
	_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;
	_someWeaponArray = _someWeaponArray - _randomWeapon;
	_crate addWeaponCargoGlobal _randomWeapon; //store you crate in a variable, don't know if _this could also be the itemCounter cause it's in an if-statement
	_itemCounter = _itemCounter + 1;
};

//Always pick an amount depending on the array size
//If you array has 5 items, it will pick 1 from it, if it has 10 items it will pick 2 for it (adjustable by changing the 5)
_itemCounter = 0;
while {_itemCounter < (round(count _someWeaponArray / 5))} do {
	_randomWeapon = _someWeaponArray call BIS_fnc_selectRandom;
	_someWeaponArray = _someWeaponArray - _randomWeapon;
	_crate addWeaponCargoGlobal _randomWeapon;
	_itemCounter = _itemCounter + 1;
};

 

 

@DAmNRelentless Thank you, logically that makes a lot more sense. I will play around with this method and see what i come up with.

 

EDIT : for the sake of argument, pairing weapons with their respective ammo would not work seeing as the _this addWeaponCargoGlobal would require a weapon and _this addMagazineCargoGlobal would require a magazine? Something like this? 

_weaponAmmoArray =
		  [
		  [["BAF_LRR_scoped",2],["5Rnd_86x70_L115A1", 20]],
		  [["DMR_DZ",1],["20Rnd_762x51_DMR",20]],
		  [["FN_FAL_ANPVS4_DZE",2],["20Rnd_762x51_FNFAL",20]],
		  [["Mk48_CCO_DZ",2],["100Rnd_762x54_PK",20]],
		  [["M240_DZ",2],["100Rnd_762x51_M240",20]],
		  [["G17_FL_DZ",2],["17Rnd_9x19_glock17",20]],
		  [["M9_SD_DZ",2],["15Rnd_9x19_M9SD",20]]
		  ];
		  
		  _rngAmmoWeaponPairSelect    = _weaponAmmoArray call BIS_fnc_selectRandom;
		  _this addWeaponCargoGlobal _rngAmmoWeaponPairSelect;

The aim is to return a random weapon, with a random amount of its respective ammo?

Link to comment
Share on other sites

  • 0

That's fairly easy to do aswell. Returning a weapon works just like I mentioned before. To get its respective ammo, you can random select with a multidimensional array and store it into different variables. I guess by "random amount of its respective ammo" you mean an amount between 0 and the amount you set in your array.

Spoiler

_weaponAmmoArray = [
	[["BAF_LRR_scoped",2],["5Rnd_86x70_L115A1", 20]],
	[["DMR_DZ",1],["20Rnd_762x51_DMR",20]],
	[["FN_FAL_ANPVS4_DZE",2],["20Rnd_762x51_FNFAL",20]],
	[["Mk48_CCO_DZ",2],["100Rnd_762x54_PK",20]],
	[["M240_DZ",2],["100Rnd_762x51_M240",20]],
	[["G17_FL_DZ",2],["17Rnd_9x19_glock17",20]],
	[["M9_SD_DZ",2],["15Rnd_9x19_M9SD",20]]
];


//Pick random weapon with ammo
_weaponWithAmmo = _weaponAmmoArray call BIS_fnc_selectRandom;


//Optionally remove it from array to prevent double pick
_weaponAmmoArray = _weaponAmmoArray - _weaponWithAmmo;


//Storing weapon and ammo into different variables
_weapon = _weaponWithAmmo select 0;
_ammo	= _weaponWithAmmo select 1;


//Random amount of respective ammo
_randomAmmo = round(random(_ammo select 1));


//Replacing maximum ammo amount with random ammo amount
_ammo set[1,_randomAmmo];


//Fill crate
_crate addWeaponCargoGlobal _weapon;
_crate addWeaponCargoGlobal _ammo;

 

 

Link to comment
Share on other sites

  • 0
5 minutes ago, DAmNRelentless said:

That's fairly easy to do aswell. Returning a weapon works just like I mentioned before. To get its respective ammo, you can random select with a multidimensional array and store it into different variables. I guess by "random amount of its respective ammo" you mean an amount between 0 and the amount you set in your array.

  Hide contents


_weaponAmmoArray = [
	[["BAF_LRR_scoped",2],["5Rnd_86x70_L115A1", 20]],
	[["DMR_DZ",1],["20Rnd_762x51_DMR",20]],
	[["FN_FAL_ANPVS4_DZE",2],["20Rnd_762x51_FNFAL",20]],
	[["Mk48_CCO_DZ",2],["100Rnd_762x54_PK",20]],
	[["M240_DZ",2],["100Rnd_762x51_M240",20]],
	[["G17_FL_DZ",2],["17Rnd_9x19_glock17",20]],
	[["M9_SD_DZ",2],["15Rnd_9x19_M9SD",20]]
];


//Pick random weapon with ammo
_weaponWithAmmo = _weaponAmmoArray call BIS_fnc_selectRandom;


//Optionally remove it from array to prevent double pick
_weaponAmmoArray = _weaponAmmoArray - _weaponWithAmmo;


//Storing weapon and ammo into different variables
_weapon = _weaponWithAmmo select 0;
_ammo	= _weaponWithAmmo select 1;


//Random amount of respective ammo
_randomAmmo = round(random(_ammo select 1));


//Replacing maximum ammo amount with random ammo amount
_ammo set[1,_randomAmmo];


//Fill crate
_crate addWeaponCargoGlobal _weapon;
_crate addWeaponCargoGlobal _ammo;

 

 

Very nice, I learn more everyday. this scripting is really versatile. BTW this is what i was looking at in the past couple of min. Would this work ? Instead of multiple iterations i use a loop with a counter?

_weaponsArray = [["BAF_LRR_scoped",2],["DMR_DZ",1],["FN_FAL_ANPVS4_DZE",2],["Mk48_CCO_DZ",2],["M240_DZ",2],["G17_FL_DZ",2],["M9_SD_DZ",2]]
		  _ammoArray	= [["20Rnd_762x51_DMR",20],["5Rnd_86x70_L115A1", 20],["20Rnd_762x51_FNFAL",20],["100Rnd_762x54_PK",20],["100Rnd_762x51_M240",20],["17Rnd_9x19_glock17",20],["15Rnd_9x19_M9SD",20]]
		  
		  
		  
		  _rngWeaponSelect = _weaponsArray call BIS_fnc_selectRandom;
		  
		  _rngAmmoSelec    = _ammoArray call BIS_fnc_selectRandom;
		  
		  _wepCount = 0;
		  
		  _ammoCount = 0;
		  
		  //Adding weapons to the crate with a loop statement
		  while {_wepCount != 7} do
			{
				_this addWeaponCargoGlobal _rngWeaponSelect;
				_wepCount = _Wepcount + 1
			}
		  
		  while {_ammoCount != 7} do
		  {
				_this addMagazineCargoGlobal _rngAmmoSelect;
				_ammoCount = _ammoCount + 1;
		  }

 

Link to comment
Share on other sites

  • 0

@DAmNRelentless I will definitely be using your code, i was just curious at the possibilities of generating randoms from arrays. I more familiar with python and C#. Probably why i find SQF interesting. With practice im learning to change my mindset in terms of more effecting coding essentially improving across the board. I really appreciate the help that you are offering though

Link to comment
Share on other sites

  • 0

Quick question.. When creating the crate this is the default script for it :

_this = createVehicle ["USSpecialWeaponsBox", [4434.3223, 10632.069, 0], [], 0, "CAN_COLLIDE"];

Can i change it to this, to tie in with your method?

_ammoCrate = createVehicle ["USSpecialWeaponsBox", [4434.3223, 10632.069, 0], [], 0, "CAN_COLLIDE"];

and then fill _ammoCrate by calling globals like this?

 


_buildArray   = [["ItemLockbox",2],["ItemPole",20],["ItemComboLock",2],["ItemGenerator",3],["ItemVault",2],["ItemCorrugated",20],["plot_pole_kit",2],["ItemWire",10,["ItemTankTrap",10],["ItemSandbag",10],["CinderBlocks",40],["MortarBucket",20]];

_rngBuildSelect  = _buildArray call BIS_fnc_selectRandom;

_ammoCrate addMagazineCargoGlobal _rngBuildSelect;	

 

Link to comment
Share on other sites

  • 0

Will you please have a look and see if this will do what I am trying to achieve? And if the syntax is correct? I'm only applying what I have learned from the info you have given me.

Spoiler

if (isServer) then {

_vehicle_9090 = objNull;
     if (true) then {
          _ammoCrate = createVehicle ["USSpecialWeaponsBox", [4434.3223, 10632.069, 0], [], 0, "CAN_COLLIDE"];
          _vehicle_9090 = _ammoCrate;
          _ammoCrate setDir -182.5;
                  _vehicle_9090 setVariable ["ObjectID","1",true];
                  _vehicle_9090 setVariable ["permaLoot",true];

          clearWeaponCargoGlobal _ammoCrate;
          clearMagazineCargoGlobal _ammoCrate;


//Classed Arrays	  
		  _toolArray       = [["ItemToolbox",5],["ItemCrowbar",5],["ItemEtool",5],["Binocular_Vector",5],["ItemGPS",5],["ItemHatchet",5],["NVGoggles",5]];
		  _buildArray      = [["ItemLockbox",2],["ItemPole",20],["ItemComboLock",2],["ItemGenerator",3],["ItemVault",2],["ItemCorrugated",20],["plot_pole_kit",2],["ItemWire",10],["ItemTankTrap",10],["ItemSandbag",10],["CinderBlocks",40],["MortarBucket",20]];
		  _medArray        = [["ItemBloodbag",5],["ItemEpinephrine",5],["ItemMorphine",5],["ItemPainkiller",5],["ItemAntibiotic",5],["ItemBandage",5]];
		  _foodArray	   = [["ItemSeaBassCooked",10],["ItemTunaCooked",10],["ItemSodaR4z0r",5],["ItemSodaMdew",5],["ItemSodaOrangeSherbet",5],["ItemSodaRbull",5]];
		  _vehprtArray     = [["PartEngine",5],["PartVRotor",5],["PartGlass",5],["PartWheel",5],["PartGeneric",5]["ItemJerrycan",5]];
 		  _weaponAmmoArray = [[["BAF_LRR_scoped",2],["5Rnd_86x70_L115A1", 20]],[["DMR_DZ",1],["20Rnd_762x51_DMR",20]],[["FN_FAL_ANPVS4_DZE",2],["20Rnd_762x51_FNFAL",20]],[["Mk48_CCO_DZ",2],["100Rnd_762x54_PK",20]],[["M240_DZ",2],["100Rnd_762x51_M240",20]],[["G17_FL_DZ",2],["17Rnd_9x19_glock17",20]],[["M9_SD_DZ",2],["15Rnd_9x19_M9SD",20]]];
		 
		  
//Random Item selection weapons and ammo in a multidimentional array		  
		 _rngWepAmmoSelect = _weaponAmmoArray call BIS_fnc_selectRandom;
		
//Filling the crate		
		//Weapons
		_wepCounter = 0;
		while {_wepCounter < 7} do {
            _weapon = _rngWepAmmoSelect select 0;
            _wepSelect = _wepSelect - _weapon;
            _ammoCrate addWeaponCargoGlobal _weapon;
			_wepCounter = _wepCounter + 1;
		};
                               
		//Ammo
		_ammoCounter = 0;
		while {_ammoCounter < 7} do {
			_ammo	= _rngWepAmmoSelect select 1;
			//Random amount of respective ammo
			_rngAmmo = round(random(_ammo select 1));
			//Replace max ammo amount with random amount
			_ammo set[1,_rngAmmo];
			_ammoSelect = _ammoSelect - _ammo;
			_ammoCrate addMagazineCargoGlobal _ammo;
			_ammoCounter = _ammoCounter + 1;
		};

		//Toolbelt items. 7 total yields 3 random items
		_toolCounter = 0;
		while {_toolCounter < (round(count _toolArray / 2))} do {
			_rngToolSelect    = _toolArray call BIS_fnc_selectRandom;
			_toolSelect 	  = _toolSelect - _rngToolSelect;
			_ammoCrate addWeaponCargoGlobal _rngToolSelect;
			_toolCounter      = _toolCounter + 1;                                    
		};
                                          
		//Medical Supplies. 6 total yields 3 random items
		_medCounter = 0;
		while {_medCounter < (round(count _medArray / 2))} do {
			_rngMedSelect     = _medArray call BIS_fnc_selectRandom;
			_medSelect		  = _medSelect - _rngMedSelect;
			_ammoCrate addMagazineCargoGlobal _rngMedSelect;
			_toolCounter	  = _toolCounter + 1;
		};
		//Building Materials. 12 total yields 6 random items.
		_buildCounter = 0;
		while {_buildCounter < (round(count _buildArray /2))} do {
			_rngBuildSelect   = _buildArray call BIS_fnc_selectRandom;
			_buildSelect 	  = _buildSelect - _rngBuildSelect;
			_ammoCrate addMagazineCargoGlobal _rngBuildSelect;
			_buildCounter 	  = _buildCounter + 1;
		};
		
		//Food Supplies. 6 total yields 3 random items.
		_foodCounter = 0;
		while {_foodCounter < (round(count _foodArray /2))} do {
			_rngFoodSelect	   = _foodArray call BIS_fnc_selectRandom;
			_foodSelect		   = _foodSelect - _rngFoodSelect;
			_ammoCrate addMagazineCargoGlobal _rngFoodSelect;
			_foodCounter 	   = _foodCounter + 1;
			};
			
		//Vehicle Parts. 6 total yields 3 random items.
		_vehprtCounter = 0;
		while {_vehprtCounter < (round(count _vehprtArray /2))} do {
			_rngVehprtSelect  = _vehprtArray call BIS_fnc_selectRandom;
			_vehprtSelect	  = _vehprtSelect - _rngVehprtSelect;
			_ammoCrate addMagazineCargoGlobal _rngVehprtSelect;
			_vehprtCounter	  = _vehprtCounter + 1;
		};
		
		
    
	_ammoCrate setPos [4434.3223, 10632.069, 0];
		  
     };
};	

 

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Discord

×
×
  • Create New...