Jump to content
  • 0

ForEach Help


- VJ -

Question

Can i get some help with a this bit of code.

 

Say I have.

 

_ver1 = x;

_ver2 = y;

_ver3 = z;

_ver4 = 1;

_ver5 = 2;

 

{some code}forEach[_ver1,_ver2,_ver3,_ver4,_ver5] // <--- Rather than having to type out all the _ver,  is there a way to set it up like

 

{some code}forEach[_ver%1, _x] // <-- like so

 

I hope someone understands what a am asking and if it is possible.

 

Thanks

 

- VJ - 

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

not sure why you would want the variables to change (since it usually means a major security flaw) and im not sure if its even possible with a private variable.

 

why not use the first example? i dont see the problem in that.

I dont want to change the variable i want to cycle through them in the forEach "loop" instead of typing each one out.

 

and too much extra code.

Link to comment
Share on other sites

  • 0

I dont want to change the variable i want to cycle through them in the forEach "loop" instead of typing each one out.

 

and too much extra code.

 

well you would basicly be changing the variable from what you are explaining ... either way you should just use the first example for security reasons, especially if there is no reason to do it otherwise.

 

if you want to streamline/optimize your code, you should wait and do it when you are done writing the script, there is no such thing as too much code, however optimizations can usually be done when functionallity is complete.

 

if there is a specific reason for you to do it this way, you will have to explain this, else nobody can really help you.

Link to comment
Share on other sites

  • 0

OK so can you explain the security risk of doing this.

 

  1. you cant whitelist a variable that you do not know the name of before using it
  2. global variables can be easily changed
  3. if allowing all random variables hacking will be extremely easy

you should always use easily reconizable variables that can be whitelisted if needed, using random or changing variables should be kept on the server if needed at all.

 

in the case you show, the best way to streamline it would prob be something like this:

_vers = [x,y,z,1,2];

{some code}forEach _vers;
Link to comment
Share on other sites

  • 0

lol ok im not being clear. I want to I have _VERx (x = a number) I would like to auto increase each turn or will this work

 

_vers = [[x,y],[z,1],[2,3]];

{some code}forEach _vers;

 

and if so would i call them like

 

{

_value1 =  ((_x select 0) select 0);

_value2 =  ((_x select 0) select 1);

}forEach _vers;

Link to comment
Share on other sites

  • 0

lol ok im not being clear. I want to I have _VERx (x = a number) I would like to auto increase each turn or will this work

 

_vers = [[x,y],[z,1],[2,3]];

{some code}forEach _vers;

 

and if so would i call them like

 

{

_value1 =  ((_x select 0) select 0);

_value2 =  ((_x select 0) select 1);

}forEach _vers;

 

im still unsure what you are trying to do with this, but it seems fine to me like this ...

Link to comment
Share on other sites

  • 0

Plenty of ways, sky's the limit. There's generally 4 ways to declare variable, one you know - by simply doing it, i'll show you 2 more, but fourth one i'll keep to myself for reasons.

 

Now then:

_arr = ["x","y","z",1,2,3];

for "_i" from 0 to (count _arr -1) do {
	missionNamespace setVariable [(format["value%1",_i]),_arr select _i]; //value1 = x, value2 = y etc...
};

{
	missionNamespace setVariable [(format["value%1",_forEachIndex]),_arr select _forEachIndex]; //value1 = x, value2 = y etc...
} forEach _arr; 

_cnt = -1;
{
	_cnt = _cnt +1;
	missionNamespace setVariable [(format["value%1",_cnt]),_x]; //value1 = x, value2 = y etc...
} count _arr; 

_cnt = -1;
{
	_cnt = _cnt +1;
	call compile format ["_var%1 = %2",_cnt,_x]; //_var1 = x, _var2 = y etc...
} count _arr;

The last part is an interesting one, what it does is creates a string, then converts it into {code} using compile, then removes these code brackets by calling it. This in return allows you to declare local vars ofc.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...