Jump to content
Sign in to follow this  
FireStormHD

Problem with Random Select

Recommended Posts

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
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

 

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
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

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

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
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
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
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];

 

  • Like 1

Share this post


Link to post
Share on other sites
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 !

  • Like 1

Share this post


Link to post
Share on other sites

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
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

1487760834-init.png

Spoiler

1487760839-20170222115302-1.jpg

Spoiler

1487760840-20170222115318-1.jpg

 

Share this post


Link to post
Share on other sites
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
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

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×