Jump to content
Alert23

switch through array without selectRandom

Recommended Posts

hi all,

 

i have a problem right now im trying to create a script which will switch trough an array:

KeyUp.sqf:

SelectGear = (findDisplay 46) displayAddEventHandler ["KeyUp", "if ((_this select 1) == 57) then {nul = [] execVM 'scripts\SelectLoadOut.sqf'};"]; 

SelectLoadOut:

LoadOut = selectRandom ["Gear1","Gear2","Gear3","Gear4","Gear5"];

switch (LoadOut) do {
            

		case "Gear1": {[] execVM "scripts\Gear1.sqf";};
            	case "Gear2": {[] execVM "scripts\Gear2.sqf";};
            	case "Gear3": {[] execVM "scripts\Gear3.sqf";};
            	case "Gear4": {[] execVM "scripts\Gear4.sqf";};
            	case "Gear5": {[] execVM "scripts\Gear5.sqf";};
	};

this will switch Randomly through LoadOut array,

how can i make it so that it does not selectRandom but switch from Gear1-Gear5.

Share this post


Link to post
Share on other sites
5 minutes ago, Alert23 said:

how can i make it so that it does not selectRandom but switch from Gear1-Gear5.

 

if its client side you can just use global variable that's the index:


 

curGear = 1; // Init somewhere

switch(curGear) do
{
 case 1: {};
case 2: {};

// Etc
};

curGear = curGear + 1; // Goto next index

 

EDIT: In fact you don't need a switch, just do:

 

[] execVM format["scripts\Gear%1.sqf", curGear];

 

Edited by gc8
No switch needed
  • Like 1

Share this post


Link to post
Share on other sites
1 minute ago, gc8 said:

 

if its client side you can just use global variable that's the index:


 


curGear = 1; // Init somewhere

switch(curGear) do
{
 case 1: {};
case 2: {};

// Etc
};

curGear = curGear + 1; // Goto next index

 

will try when im hoem thank you!

Share this post


Link to post
Share on other sites
16 hours ago, Alert23 said:

will try when im hoem thank you!

Alternatively you can use this:

//init.sqf or wherever it may fit
TAG_fnc_loadouts = ["Gear1","Gear2","Gear3","Gear4","Gear5"];

GOM_fnc_cycleArr = {
	_current = TAG_fnc_loadouts#0;
	TAG_fnc_loadouts pushBack _current;
	TAG_fnc_loadouts deleteAt 0;
	_current
};

//cycle through loadouts
_gear = [] call GOM_fnc_cycleArr;

This will cycle gear1-gear5 infinitely, every time you call the cycleArr function, _gear holds the currently selected loadout.

 

Cheers

  • Like 3

Share this post


Link to post
Share on other sites

@Grumpy Old Man

I guess this

... TAG_fnc_loadouts#0;

is the same as that

... TAG_fnc_loadouts select 0;

I saw similar usage in a post of @Larrow

 

could someone tell me where the #-"operator" is documented? I didn't find anything about it.

  • Like 2

Share this post


Link to post
Share on other sites
34 minutes ago, sarogahtyp said:

@Grumpy Old Man

I guess this


... TAG_fnc_loadouts#0;

is the same as that


... TAG_fnc_loadouts select 0;

I saw similar usage in a post of @Larrow

 

could someone tell me where the #-"operator" is documented? I didn't find anything about it.

Here you go.

It's similar, the hash doesn't support the alternate syntax of select, only selects an index from an array, easier to read if you ask me.

 

Cheers

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
19 hours ago, gc8 said:

 

if its client side you can just use global variable that's the index:


 


curGear = 1; // Init somewhere

switch(curGear) do
{
 case 1: {};
case 2: {};

// Etc
};

curGear = curGear + 1; // Goto next index

 

EDIT: In fact you don't need a switch, just do:

 


[] execVM format["scripts\Gear%1.sqf", curGear];

 

 

1 hour ago, Grumpy Old Man said:

Here you go.

It's similar, the hash doesn't support the alternate syntax of select, only selects an index from an array, easier to read if you ask me.

 

Cheers

thank you both for your help, but i cant get both of it working,

maybe i explain what im doing:

 

up on pressing the SPACE key:

SelectGear = (findDisplay 46) displayAddEventHandler ["KeyUp", "if ((_this select 1) == 57) then {nul = [] execVM 'scripts\SelectLoadOut.sqf'};"];

SelectLoadOut:

 

LoadOut = selectRandom ["Gear1","Gear2","Gear3","Gear4","Gear5"];

switch (LoadOut) do {
            

		case "Gear1": {[] execVM "scripts\Gear1.sqf";};
            	case "Gear2": {[] execVM "scripts\Gear2.sqf";};
            	case "Gear3": {[] execVM "scripts\Gear3.sqf";};
            	case "Gear4": {[] execVM "scripts\Gear4.sqf";};
            	case "Gear5": {[] execVM "scripts\Gear5.sqf";};
	};

now i want to give the player the option to choose which Gear he wants to use before mission start:

Spoiler

V21Cgrlk.jpg

everytime the player presses SPACE the Gear will change, for now i use selectRandom with Switch do

which selects random Gear from the array but i try to make it so that everytime SPACE is pressed it will jump to the next Gear.

Share this post


Link to post
Share on other sites
15 minutes ago, Alert23 said:

everytime the player presses SPACE the Gear will change, for now i use selectRandom with Switch do

which selects random Gear from the array but i try to make it so that everytime SPACE is pressed it will jump to the next Gear.

That's exactly what my snippet does, of course you can adapt the array contents to hold the .sqf filename which should be executed, instead of placeholders like "Gear1", "Gear2" etc.:

 

//init.sqf or wherever it may fit
TAG_fnc_loadouts = ["scripts\Gear1.sqf", "scripts\Gear2.sqf", "scripts\Gear3.sqf", "scripts\Gear4.sqf", "scripts\Gear5.sqf"];

GOM_fnc_cycleArr = {
	_current = TAG_fnc_loadouts#0;
	TAG_fnc_loadouts pushBack _current;
	TAG_fnc_loadouts deleteAt 0;
	_current
};

//cycle through loadouts
SelectGear = (findDisplay 46) displayAddEventHandler ["KeyUp",
	"if ((_this select 1) == 57) then {
		_script = [] call GOM_fnc_cycleArr;
		nul = [] execVM _script;
	};"
];

Cheers

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites
4 minutes ago, Grumpy Old Man said:

That's exactly what my snippet does, of course you can adapt the array contents to hold the .sqf filename which should be executed, instead of placeholders like "Gear1", "Gear2" etc.:

 


//init.sqf or wherever it may fit
TAG_fnc_loadouts = ["scripts\Gear1.sqf", "scripts\Gear2.sqf", "scripts\Gear3.sqf", "scripts\Gear4.sqf", "scripts\Gear5.sqf"];

GOM_fnc_cycleArr = {
	_current = TAG_fnc_loadouts#0;
	TAG_fnc_loadouts pushBack _current;
	TAG_fnc_loadouts deleteAt 0;
	_current
};

//cycle through loadouts
SelectGear = (findDisplay 46) displayAddEventHandler ["KeyUp",
	"if ((_this select 1) == 57) then {
		_script = [] call GOM_fnc_cycleArr;
		nul = [] execVM _script;
	};"
];

Cheers

your the best Grumpy!

  • Like 1

Share this post


Link to post
Share on other sites
57 minutes ago, Grumpy Old Man said:

_current = TAG_fnc_loadouts#0;

TAG_fnc_loadouts pushBack _current;

TAG_fnc_loadouts deleteAt 0;

deleteAt returns the deleted element

_current = TAG_fnc_loadouts deleteAt 0;
TAG_fnc_loadouts pushBack _current;
_current

or if you wanna get crazy

TAG_fnc_loadouts#(TAG_fnc_loadouts pushBack (TAG_fnc_loadouts deleteAt 0))

 

 

57 minutes ago, Grumpy Old Man said:

nul = [] execVM _script;

What is that "nul = " for? I see it often used to work around to the script box bug in 3DEN that doesn't accept scripts unless you put that there, but that bug isn't in play here.

 

  • Like 1

Share this post


Link to post
Share on other sites
18 minutes ago, Dedmen said:

What is that "nul = " for? I see it often used to work around to the script box bug in 3DEN that doesn't accept scripts unless you put that there, but that bug isn't in play here.

I guess it prevents to have the script handle as return value for the EH.

 

EDIT but should not be necessary...

Share this post


Link to post
Share on other sites
19 hours ago, sarogahtyp said:

I guess it prevents to have the script handle as return value for the EH.

In this case nul will hold the return value.

It's just odd practice since more than 90% of all Arma scripting guides use nul/null/0 in their examples.

.sqf equivalent to foo bar I guess.

 

19 hours ago, Dedmen said:

deleteAt returns the deleted element

Indeed, my grumpy old eyes are failing me, gonna have to revisit some scripts of mine, heh.

 

Edit:

Implemented the more efficient and pretty funky solution provided by @Dedmen:

//init.sqf or wherever it may fit, for MP use initServer.sqf
TAG_fnc_loadouts = ["scripts\Gear1.sqf", "scripts\Gear2.sqf", "scripts\Gear3.sqf", "scripts\Gear4.sqf", "scripts\Gear5.sqf"];

//initPlayerLocal.sqf
SelectGear = (findDisplay 46) displayAddEventHandler ["KeyUp",
	"if ((_this select 1) == 57) then {
		nul = [] execVM TAG_fnc_loadouts#(TAG_fnc_loadouts pushBack (TAG_fnc_loadouts deleteAt 0));
	};"
];

Just a quick explanation:

deleteAt returns the deleted element, as stated in his post above,

pushBack returns the index of the pushed element.

 

Cheers

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

×