TheGeneral2899 19 Posted October 20, 2017 So after about 3-4 days of messing around with this, I've come to the conclusion that I have no idea how to make it work / I suspect the "selectRandomWeighted" function doesnt work as it should. Script: It creates boxes all over the map and then should populate the boxes with gear according to weighted arrays. What Works: 1) Boxes spawn 2) Pistols and Rifles get added to the boxes no problem. Issues: 1) The weights of the selectRandomWeighted do not properly reflect what I have written in the arrays. Not even close. Went according to the wiki for it (https://community.bistudio.com/wiki/selectRandomWeighted) 2) Occasionally (on line 138) with the Debug Hint function, I am getting the word "Any" appearing for one of the gear results print outs. What I am unclear about is why this is happening? I suspect its due to something with the way I constructed this whole thing? 3) In certain arrays ("_rifles") for instance there is a nested array with the gun and magazine class name, however I have certain arrays (such as "grenades") that only have one element in the array. How can I check up to see if there is no 2nd element and bypass it? I have attempted this at around line 143. 4) Backpacks will not get created in the "AddItemCargo" no matter what I try, despite me seeing their class name and all via the debug hint. I really hope I commented my code well enough so that ppl can understand. Also, if any more experienced coders look at what I did and want to facepalm, please any and all advice would be great! Thank you in advance! // By TheGeneral // Script to spawn crates and a random weighted type of gear / ammo in each one. // Array of all scrate spawn positions marked as pencils //_prefix = "cratespawn_"; _prefix = "test_"; _len = count _prefix; _crateLocs = allMissionObjects "" select { vehicleVarName _x select [ 0, _len ] == _prefix }; // Get Number of Available pen locations for while loop _numOfCrateLocs = count _crateLocs; // Declaring which items are in each gear type and their relevant weights /////////////////// // GEAR TYPE # 1 // /////////////////// _rifles = [ ["rhs_weap_m16a4","30Rnd_556x45_Stanag"], 0.18, ["rhs_weap_m4", "30Rnd_556x45_Stanag"], 0.16, ["rhs_weap_ak74n_npz","rhs_30Rnd_545x39_AK_green"] ,0.19, ["rhs_weap_m24sws","rhsusf_5Rnd_762x51_m62_Mag"], 0.05, ["rhs_weap_sr25_d", "rhsusf_20Rnd_762x51_m62_Mag"] ,0.07, ["rhs_weap_ak74mr","rhs_30Rnd_762x39mm"], 0.17, ["rhs_weap_svdp", "rhs_10Rnd_762x54mmR_7N1"], 0.08, ["rhs_weap_aks74u", "rhs_45Rnd_545X39_7N10_AK"], 0.23, ["rhs_weap_m4a1_blockII_d", "30Rnd_556x45_Stanag"], 0.14 ]; _mgs = [ ["rhsusf_weap_MP7A2_desert","rhsusf_mag_40Rnd_46x30_FMJ"] ,0.27, ["rhs_weap_pp2000", "rhs_mag_9x19mm_7n21_20"], 0.28, ["rhs_weap_m590_5RD", "rhsusf_5Rnd_00Buck"], 0.18, ["rhs_weap_m249", "rhsusf_100Rnd_556x45_M855_soft_pouch"], 0.08, ["smg_02_f", "30Rnd_9x21_Mag_SMG_02"], 0.23 ]; _vests = [ ["V_EOD_olive_F"] , 0.09, ["V_EOD_coyote_F"] , 0.09, ["V_Press_F"] , 0.15, ["V_TaCVest_brn"] , 0.2, ["V_Chestrig_khk"] , 0.25, ["V_Chestrig_oli"] , 0.25, ["V_PlateCarrier_Kerry"] , 0.12 ]; /////////////////// // GEAR TYPE # 2 // /////////////////// _backpacks = [ ["Rhsusf_assault_eagleaiii_ucp"] , 0.2, ["rhsusf_falconii_mc"] , 0.2, ["B_FieldPack_cbr"] , 0.1, ["B_LegStrapBag_black_F"] , 0.4 ]; _helmets = [ ["rhsusf_ach_bare_tan"] , 0.12, ["rhsusf_ach_helmet_ocp"] , 0.12, ["rhsusf_ach_helmet_camo_ocp"] , 0.12, ["H_PASGT_basic_white_f"] , 0.12, ["rhsusf_hgu56p_visor_mask_Empire_black"] , 0.12, ["H_HelmetB_light_desert"] , 0.12, ["rhsusf_mich_bar_alt_semi"] , 0.12, ["H_PASGT_neckprot_blue_press_F"] , 0.07, ["H_ShemagOpen_khk"] , 0.2, ["H_ShemagOpen_olive"] , 0.2, ["H_ShemagOpen_tan"] , 0.2, ["H_Booniehat_tan"] , 0.2, ["rhs_booniehat2_marpatd"] , 0.2 ]; _grenades = [ ["rhs_magazine_rhs_mag_m67"] , 0.15, ["SmokeShell"] , 0.15, ["SmokeShellRed"] , 0.15, ["SmokeShellGreen"] , 0.15, ["SmokeShellBlue"] , 0.15, ["SmokeShellYellow"] , 0.15 ]; /////////////////// // GEAR TYPE # 3 // /////////////////// _pistols = [ ["rhsusf_weap_m9","rhsusf_mag_15Rnd_9x19_FMJ"], 0.33, ["hgun_Pistol_heavy_02_F", "6Rnd_45ACP_Cylinder"], 0.19, ["hgun_Pistol_heavy_01_F","11Rnd_45ACP_Mag"] ,0.19, ["hgun_Rook40_F","16Rnd_9x21_Mag"], 0.19 ]; _medical = [ ["ACE_morphineItem"] , 0.25, ["ACE_fieldDressing"] , 0.4, ["ACE_epinephrine"] , 0.15 ]; _accessories = [ ["rhsusf_acc_m8541_low_wd"] , 0.07, ["rhsusf_acc_EOTECH"] , 0.22, ["rhsusf_acc_ACOG"] , 0.16, ["rhs_acc_pso1m21"] , 0.1, ["optic_aco"] , 0.35, ["optic_SOS"] , 0.12 ]; // ARRAYS FOR CATEGORIES OF GEAR TO BE DECIDED AND THEIR WEIGHTS. THE CODE WILL SELECT 2 OF THE 3 "GEARTYPES" TO BE SELECTED AND USED TO ADD TO CRATES _gearType_1 = [_rifles, 0.18, _mgs, 0.24, _vests, 0.25]; _gearType_2 = [_backpacks, 0.10, _helmets, 0.10, _grenades, 0.95]; _gearType_3 = [_pistols, 0.15, _medical, 0.15, _accessories, 0.10]; _gearTypes = [_gearType_1, _gearType_2,_gearType_3]; // STARTING LOOP TO CREATE CRATES AND DO CHECKS while {_numOfCrateLocs > 0} do { // Selects random marker pos _equipmentCrate = _crateLocs call BIS_fnc_selectRandom;; _equipmentCratePos = getPosATL _equipmentCrate; // Created Ammo crate at the marker location _crate = createVehicle ["Box_NATO_Ammo_F", _equipmentCratePos, [], 0, "CAN_COLLIDE"]; clearMagazineCargo _crate; clearWeaponCargo _crate; // Choose which 2 gear types are going to be used: _gearRandom_All = _gearTypes; _gearRandom_1 = selectRandom _gearRandom_All; _gearRandom_All = _gearRandom_All - [_gearRandom_1]; _gearRandom_2 = selectRandom _gearRandom_All; // INTENTION IS TO SELECT THE SPECIFIC GEAR FROM THE RANDOMED GEAR TYPE _gearResult_1 = selectRandomWeighted (_gearRandom_1 select 0); _gearResult_2 = selectRandomWeighted (_gearRandom_2 select 0); hint format ["GEAR RESULT 1: %1 . GEAR RESULT 2: %2", _gearResult_1, _gearResult_2]; // VARIABLE TO DETERMING RANDOM AMOUNT OF MAGS TO ADD IF ITEM IS A GUN / NADE / MEDIC GEAR _rndMagCount = selectRandom [2,3,4]; if ([_medical] in _gearRandom_1 || [_grenades] in _gearRandom_1) then { _crate addItemCargo [(_gearResult_1 select 0),_rndMagCount]; } else { _crate addItemCargo [(_gearResult_1 select 0),1]; }; if (isNil {_gearResult_1 select 1}) then { } else { _crate addItemCargo [(_gearResult_1 select 1),_rndMagCount]; }; if ([_medical] in _gearRandom_2 || [_grenades] in _gearRandom_2) then { _crate addItemCargo [(_gearResult_2 select 0),_rndMagCount]; } else { _crate addItemCargo [(_gearResult_2 select 0),1]; }; if (isNil {_gearResult_2 select 1}) then { } else { _crate addItemCargo [(_gearResult_2 select 1),_rndMagCount]; }; // OLD CODE KEEPING HERE FOR TESTING // _result = selectRandomWeighted _rifles; // _crate addItemCargo [(_result select 1),1]; // _crate addItemCargo [(_result select 0),1]; // REMOVE CRATE POSITION ALREADY DONE FROM ARRAY _crateLocs = _crateLocs - [_equipmentCrate]; _numOfCrateLocs = _numOfCrateLocs - 1; sleep 4; }; Share this post Link to post Share on other sites
xjoker_ 25 Posted October 20, 2017 For the backpack : https://community.bistudio.com/wiki/addBackpackCargo Share this post Link to post Share on other sites
Larrow 2821 Posted October 20, 2017 You selectRandomWeighted of only (_gearRandom_1/_2 select 0) which means you will only ever get _rifles, _backpacks or _pistols. As mentioned above you do not use addBackpackCargo for backpacks, addItemCargo only works for weapons, magazines and items You do not use addItemCargoGlobal which could cause you problems if this is for MP // By TheGeneral // Script to spawn crates and a random weighted type of gear / ammo in each one. //******* //NO CHANGES TO GEAR DATA //******* // ARRAYS FOR CATEGORIES OF GEAR TO BE DECIDED AND THEIR WEIGHTS. THE CODE WILL SELECT 2 OF THE 3 "GEARTYPES" TO BE SELECTED AND USED TO ADD TO CRATES _gearType_1 = [_rifles, 0.18, _mgs, 0.24, _vests, 0.25]; _gearType_2 = [_backpacks, 0.10, _helmets, 0.10, _grenades, 0.95]; _gearType_3 = [_pistols, 0.15, _medical, 0.15, _accessories, 0.10]; _gearTypes = [_gearType_1, _gearType_2,_gearType_3]; // Array of all scrate spawn positions marked as pencils //_prefix = "cratespawn_"; _prefix = "test_"; _crateLocs = allMissionObjects "" select { vehicleVarName _x select [ 0, count _prefix ] == _prefix }; // STARTING LOOP TO CREATE CRATES AND DO CHECKS { // Selects random pos _equipmentCratePos = getPosATL ( _crateLocs deleteAt ( floor( random count _crateLocs ))); // Created Ammo crate at the marker location _crate = createVehicle ["Box_NATO_Ammo_F", _equipmentCratePos, [], 0, "CAN_COLLIDE"]; clearBackpackCargoGlobal _crate; clearItemCargoGlobal _crate; clearMagazineCargo _crate; clearWeaponCargo _crate; // Choose which 2 gear types are going to be used: _gearRandom_1 = selectRandom _gearTypes; _gearRandom_2 = selectRandom ( _gearTypes - [ _gearRandom_1 ] ); { { _x call BIS_fnc_itemType params[ "_itemType", "_itemSubType" ]; switch ( toUpper _itemType ) do { case "MINE"; case "MAGAZINE" : { _crate addMagazineCargoGlobal [ _x, selectRandom [2,3,4] ]; }; case "ITEM" : { if ( [ _x ] in _medical ) then { _crate addItemCargoGlobal [ _x, selectRandom [2,3,4] ]; }else{ _crate addItemCargoGlobal [ _x, 1 ]; }; }; case "WEAPON" : { _crate addWeaponCargoGlobal [ _x, 1 ]; }; case "EQUIPMENT" : { if ( _itemSubType == "backpack" ) then { _crate addBackpackCargoGlobal [ _x, 1 ]; }else{ _crate addItemCargoGlobal [ _x, 1 ]; }; }; }; }forEach _x; //[ _item, _mag( possible ) ] }forEach [ // INTENTION IS TO SELECT THE SPECIFIC GEAR FROM THE RANDOMED GEAR TYPE //select weighted ( _rifles, _mgs, _vests ) then again select weighted of result ( _rifles ) selectRandomWeighted selectRandomWeighted _gearRandom_1, selectRandomWeighted selectRandomWeighted _gearRandom_2 ]; }forEach +_crateLocs; 1 Share this post Link to post Share on other sites
TheGeneral2899 19 Posted October 21, 2017 @Larrow - Duuuuuuddeeee! First of all, thank you so much for taking the time to look and help. Second, appreciate you as a more experienced coder seeing and correcting with explanations. 1) I completely overlooked the forEach and used a while loop. Appreciate that. 2) I will be bisecting each line to understand as I have never used (more misunderstood) the BIS_fnc_itemType function. Again thank you, tested and works. EDIT: Full script here so others can see how it was done: // By TheGeneral // Script to spawn crates and a random weighted type of gear / ammo in each one. // Declaring which items are in each gear type and their relevant weights /////////////////// // GEAR TYPE # 1 // /////////////////// _rifles = [ ["rhs_weap_m16a4","30Rnd_556x45_Stanag"], 0.18, ["rhs_weap_m4", "30Rnd_556x45_Stanag"], 0.16, ["rhs_weap_ak74n_npz","rhs_30Rnd_545x39_AK_green"] ,0.19, ["rhs_weap_m24sws","rhsusf_5Rnd_762x51_m62_Mag"], 0.05, ["rhs_weap_sr25_d", "rhsusf_20Rnd_762x51_m62_Mag"] ,0.07, ["rhs_weap_ak74mr","rhs_30Rnd_762x39mm"], 0.17, ["rhs_weap_svdp", "rhs_10Rnd_762x54mmR_7N1"], 0.08, ["rhs_weap_aks74u", "rhs_45Rnd_545X39_7N10_AK"], 0.23, ["rhs_weap_m4a1_blockII_d", "30Rnd_556x45_Stanag"], 0.14 ]; _mgs = [ ["rhsusf_weap_MP7A2_desert","rhsusf_mag_40Rnd_46x30_FMJ"] ,0.27, ["rhs_weap_pp2000", "rhs_mag_9x19mm_7n21_20"], 0.28, ["rhs_weap_m590_5RD", "rhsusf_5Rnd_00Buck"], 0.18, ["rhs_weap_m249", "rhsusf_100Rnd_556x45_M855_soft_pouch"], 0.08, ["smg_02_f", "30Rnd_9x21_Mag_SMG_02"], 0.23 ]; _vests = [ ["V_EOD_olive_F"] , 0.09, ["V_EOD_coyote_F"] , 0.09, ["V_Press_F"] , 0.15, ["V_TaCVest_brn"] , 0.2, ["V_Chestrig_khk"] , 0.25, ["V_Chestrig_oli"] , 0.25, ["V_PlateCarrier_Kerry"] , 0.12 ]; /////////////////// // GEAR TYPE # 2 // /////////////////// _backpacks = [ ["Rhsusf_assault_eagleaiii_ucp"] , 0.2, ["rhsusf_falconii_mc"] , 0.2, ["B_FieldPack_cbr"] , 0.1, ["B_LegStrapBag_black_F"] , 0.4 ]; _helmets = [ ["rhsusf_ach_bare_tan"] , 0.12, ["rhsusf_ach_helmet_ocp"] , 0.12, ["rhsusf_ach_helmet_camo_ocp"] , 0.12, ["H_PASGT_basic_white_f"] , 0.12, ["rhsusf_hgu56p_visor_mask_Empire_black"] , 0.12, ["H_HelmetB_light_desert"] , 0.12, ["rhsusf_mich_bar_alt_semi"] , 0.12, ["H_PASGT_neckprot_blue_press_F"] , 0.07, ["H_ShemagOpen_khk"] , 0.2, ["H_ShemagOpen_olive"] , 0.2, ["H_ShemagOpen_tan"] , 0.2, ["H_Booniehat_tan"] , 0.2, ["rhs_booniehat2_marpatd"] , 0.2 ]; _grenades = [ ["rhs_magazine_rhs_mag_m67"] , 0.10, ["SmokeShell"] , 0.15, ["SmokeShellRed"] , 0.16, ["SmokeShellGreen"] , 0.17, ["SmokeShellBlue"] , 0.18, ["SmokeShellYellow"] , 0.19 ]; /////////////////// // GEAR TYPE # 3 // /////////////////// _pistols = [ ["rhsusf_weap_m9","rhsusf_mag_15Rnd_9x19_FMJ"], 0.33, ["hgun_Pistol_heavy_02_F", "6Rnd_45ACP_Cylinder"], 0.19, ["hgun_Pistol_heavy_01_F","11Rnd_45ACP_Mag"] ,0.19, ["hgun_Rook40_F","16Rnd_9x21_Mag"], 0.19 ]; _medical = [ ["ACE_morphineItem"] , 0.25, ["ACE_fieldDressing"] , 0.4, ["ACE_epinephrine"] , 0.15 ]; _accessories = [ ["rhsusf_acc_m8541_low_wd"] , 0.07, ["rhsusf_acc_EOTECH"] , 0.22, ["rhsusf_acc_ACOG"] , 0.16, ["rhs_acc_pso1m21"] , 0.1, ["optic_aco"] , 0.35, ["optic_SOS"] , 0.12 ]; // ARRAYS FOR CATEGORIES OF GEAR TO BE DECIDED AND THEIR WEIGHTS. THE CODE WILL SELECT 2 OF THE 3 "GEARTYPES" TO BE SELECTED AND USED TO ADD TO CRATES _gearType_1 = [_rifles, 0.18, _mgs, 0.24, _vests, 0.25]; _gearType_2 = [_backpacks, 0.10, _helmets, 0.10, _grenades, 0.95]; _gearType_3 = [_pistols, 0.15, _medical, 0.15, _accessories, 0.10]; _gearTypes = [_gearType_1, _gearType_2,_gearType_3]; // Array of all scrate spawn positions marked as pencils //_prefix = "cratespawn_"; _prefix = "test_"; _crateLocs = allMissionObjects "" select { vehicleVarName _x select [ 0, count _prefix ] == _prefix }; // Get Number of Available pen locations for while loop _numOfCrateLocs = count _crateLocs; // STARTING LOOP TO CREATE CRATES AND DO CHECKS { // Selects random pos _equipmentCratePos = getPosATL ( _crateLocs deleteAt ( floor( random count _crateLocs ))); // Created Ammo crate at the marker location _crate = createVehicle ["Box_NATO_Ammo_F", _equipmentCratePos, [], 0, "CAN_COLLIDE"]; clearBackpackCargoGlobal _crate; clearItemCargoGlobal _crate; clearMagazineCargo _crate; clearWeaponCargo _crate; // Choose which 2 gear types are going to be used: _gearRandom_1 = selectRandom _gearTypes; _gearRandom_2 = selectRandom ( _gearTypes - [ _gearRandom_1 ] ); { { _x call BIS_fnc_itemType params[ "_itemType", "_itemSubType" ]; switch ( toUpper _itemType ) do { case "MINE"; case "MAGAZINE" : { _crate addMagazineCargoGlobal [ _x, selectRandom [2,3,4] ]; }; case "ITEM" : { if ( [ _x ] in _medical ) then { _crate addItemCargoGlobal [ _x, selectRandom [2,3,4] ]; }else{ _crate addItemCargoGlobal [ _x, 1 ]; }; }; case "WEAPON" : { _crate addWeaponCargoGlobal [ _x, 1 ]; }; case "EQUIPMENT" : { if ( _itemSubType == "backpack" ) then { _crate addBackpackCargoGlobal [ _x, 1 ]; }else{ _crate addItemCargoGlobal [ _x, 1 ]; }; }; }; }forEach _x; //[ _item, _mag( possible ) ] }forEach [ // INTENTION IS TO SELECT THE SPECIFIC GEAR FROM THE RANDOMED GEAR TYPE //select weighted ( _rifles, _mgs, _vests ) then again select weighted of result ( _rifles ) selectRandomWeighted selectRandomWeighted _gearRandom_1, selectRandomWeighted selectRandomWeighted _gearRandom_2 ]; }forEach +_crateLocs; Share this post Link to post Share on other sites