FireStormHD 1 Posted February 21, 2017 Hello ! I'm trying to create a script that allows you to put random weapons and ammo into crates. I tried to put this in crate's Init but it does not work. AR15= ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; G36= ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; AWM= ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; gun = ["AR15","G36", "AWM"] call BIS_fnc_selectRandom; this addItemCargoGlobal [gun,1]; An idea ? Share this post Link to post Share on other sites
celludriel 79 Posted February 21, 2017 this addWeaponCargoGlobal [gun,1]; You are handling weapons not items. Also since you are handling local variables make sure you prefix them with _ . To avoid problems in the future private them as well eg: private ["_gun"]; Share this post Link to post Share on other sites
FireStormHD 1 Posted February 21, 2017 I am not sure to have understood everything .... So I have to write this _AR15= ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; _G36= ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; _AWM= ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; _gun = ["_AR15","_G36", "_AWM"] call BIS_fnc_selectRandom; this addItemCargoGlobal [gun,1]; Where do I write Quote private ["_gun"]; ? Share this post Link to post Share on other sites
celludriel 79 Posted February 21, 2017 private ["_AR15", "_G36", "_AWM", "_gun"]; _AR15 = ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; _G36 = ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; _AWM = ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; _gun = [_AR15, _G36, _AWM] call BIS_fnc_selectRandom; this addWeaponCargoGlobal [_gun select 0, 1]; this addMagazineCargoGlobal [_gun select 1, 5]; I'm at work so can't test, but try this Share this post Link to post Share on other sites
FireStormHD 1 Posted February 21, 2017 I can't put this in Init, i have a warning message : "Init: Local Variable in global space" Share this post Link to post Share on other sites
celludriel 79 Posted February 21, 2017 argh you are not calling this script but using it in an init, disregard my private remarks then AR15 = ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; G36 = ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; AWM = ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; gun = [AR15, G36, AWM] call BIS_fnc_selectRandom; this addWeaponCargoGlobal [gun select 0, 1]; this addMagazineCargoGlobal [gun select 1, 5]; Share this post Link to post Share on other sites
FireStormHD 1 Posted February 21, 2017 3 hours ago, celludriel said: argh you are not calling this script but using it in an init, disregard my private remarks then AR15 = ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; G36 = ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; AWM = ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; gun = [AR15, G36, AWM] call BIS_fnc_selectRandom; this addWeaponCargoGlobal [gun select 0, 1]; this addMagazineCargoGlobal [gun select 1, 5]; It's Works ! But when i play solo. In MP (2 players) i found double stuff. So 2 weapons and his 5 ammo... Share this post Link to post Share on other sites
celludriel 79 Posted February 21, 2017 this addWeaponCargoGlobal [gun select 0, 1]; this addMagazineCargoGlobal [gun select 1, 5]; Runs globally, each time someone loads the mission enters the map the init method will trigger and so will these methods. You'll have to find a way to trigger it only once. There is only so much you can do with init fields though. Things get way easier if you dive into mission scripting. Then you can make a server function that gets called just once when the server loads. Share this post Link to post Share on other sites
killzone_kid 1330 Posted February 21, 2017 5 minutes ago, FireStormHD said: In MP (2 players) i found double stuff. So 2 weapons and his 5 ammo... if (!local this) exitWith {}; AR15 = ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; G36 = ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; AWM = ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; gun = [AR15, G36, AWM] call BIS_fnc_selectRandom; this addWeaponCargoGlobal [gun select 0, 1]; this addMagazineCargoGlobal [gun select 1, 5]; 1 Share this post Link to post Share on other sites
FireStormHD 1 Posted February 21, 2017 2 hours ago, killzone_kid said: if (!local this) exitWith {}; AR15 = ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"]; G36 = ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"]; AWM = ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]; gun = [AR15, G36, AWM] call BIS_fnc_selectRandom; this addWeaponCargoGlobal [gun select 0, 1]; this addMagazineCargoGlobal [gun select 1, 5]; Omg thx ! It's Works ! <3 Thanks you too Celludriel ! 1 Share this post Link to post Share on other sites
serena 151 Posted February 21, 2017 Look at the selectRandom command: private _randomElement = selectRandom ["A", "B", "C"]; Share this post Link to post Share on other sites
serena 151 Posted February 21, 2017 Based on killzone_kid code: // in init.sqf AddRandomWeaponWithAmmo = { if (local _this) then { if (isNil {WeaponsTable395867}) then { WeaponsTable395867 = [ ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"], ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"], ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]]}; (selectRandom WeaponsTable395867) params ["_weapon", "_ammo"]; _this addWeaponCargoGlobal [_weapon, 1]; _this addMagazineCargoGlobal [_ammo, 5]; }; }; // in cargo box init field this call AddRandomWeaponWithAmmo; * error fixed Share this post Link to post Share on other sites
FireStormHD 1 Posted February 22, 2017 13 hours ago, serena said: Based on killzone_kid code: // in init.sqf AddRandomWeaponWithAmmo = { if (local _this) then { if (isNil {WeaponsTable395867}) then { WeaponsTable395867 = [ ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"], ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"], ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]]}; (selectRandom WeaponsTable395867) params ["_weapon", "_ammo"]; _this addWeaponCargoGlobal [_weapon, 1]; _this addMagazineCargoGlobal [_ammo, 5]; }; }; // in cargo box init field this call AddRandomWeaponWithAmmo; * error fixed It does not work, nothing spawn in my crate :/ You can see on theses screens: Spoiler Spoiler Spoiler Share this post Link to post Share on other sites
serena 151 Posted February 22, 2017 5 hours ago, FireStormHD said: It does not work, nothing spawn in my crate :/ Sorry, my fault. It is because calling code executes before target function get initialized. This code fixes problem: // in init.sqf if (true) then { private _vrn = "AddWeaponWithAmmo"; private _wpd = [ ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"], ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"], ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]]; { if (local _x and {not isNil {_x getVariable _vrn}}) then { selectRandom _wpd params ["_wpn", "_amm"]; _x addWeaponCargoGlobal [_wpn, 1]; _x addMagazineCargoGlobal [_amm, 5]; _x setVariable [_vrn, nil, true] } } forEach entities "" }; // in cargo box init field this setVariable ["AddWeaponWithAmmo", true] * code tested, all works as expected Share this post Link to post Share on other sites
FireStormHD 1 Posted February 22, 2017 Quote Sorry, my fault. It is because calling code executes before target function get initialized. This code fixes problem: Nice ! thanks you a lot ! :D Now i have a question :) If i want put an another stuff in crate, i need to replace: "addWeaponCargoGlobal" by "additemcargoglobal" ? Like this ? // in init.sqf if (true) then { private _vrn = "AddWeaponWithAmmo"; private _wpd = [ ["ItemCompass"], ["ToolKit"], ["Binocular"]]; { if (local _x and {not isNil {_x getVariable _vrn}}) then { selectRandom _wpd params ["_wpn"]; _x additemcargoglobal [_wpn, 1]; _x setVariable [_vrn, nil, true] } } forEach entities "" }; Share this post Link to post Share on other sites
serena 151 Posted February 22, 2017 Not necessary replace, you can also extend: // in init.sqf if (true) then { private _vrn = "AddWeaponWithAmmo"; private _wpd = [ ["hlc_rifle_RU556", "hlc_30rnd_556x45_EPR"], ["hlc_rifle_G36E1", "hlc_30rnd_556x45_EPR_G36"], ["hlc_rifle_awcovert_BL", "hlc_5rnd_300WM_FMJ_AWM"]]; private _itd = [ "ItemCompass", "ToolKit", "Binocular"]; { if (local _x and {not isNil {_x getVariable _vrn}}) then { selectRandom _wpd params ["_wpn", "_amm"]; _x addWeaponCargoGlobal [_wpn, 1]; _x addMagazineCargoGlobal [_amm, 5]; _x addItemCargoGlobal [selectRandom _itd, 1]; _x setVariable [_vrn, nil, true] } } forEach entities "" }; * code fixed Share this post Link to post Share on other sites
FireStormHD 1 Posted February 22, 2017 Thanks you again ! :) Share this post Link to post Share on other sites
serena 151 Posted February 22, 2017 Code above fixed Share this post Link to post Share on other sites