Jump to content
Oldiron

Problem referencing to variables by arrays

Recommended Posts

Probably i am too dumb for that but maybe someone can help me

i want to change values in variables.

i want to choose these variables from an array.

so

_x1 = 0

_x2 = 0

_array = [_x1,_x2]

_y = _array select 0

_y = _y + 1

_array set [0,_y]

should end up in _x == 1 (value of _x)

but it doesnt

in other words i want to change the variable inside the array (_x1), not the array itself or its values.

i hope u get what i mean

is there a way to call a variable by an array and to change its value?

 

any help apreciated very much

 

Share this post


Link to post
Share on other sites
2 hours ago, Oldiron said:

Probably i am too dumb for that but maybe someone can help me

i want to change values in variables.

i want to choose these variables from an array.

so

_x1 = 0

_x2 = 0

_array = [_x1,_x2]

_y = _array select 0

_y = _y + 1

_array set [0,_y]

should end up in _x == 1 (value of _x)

but it doesnt

in other words i want to change the variable inside the array (_x1), not the array itself or its values.

i hope u get what i mean

is there a way to call a variable by an array and to change its value?

 

any help apreciated very much

 

 

Not sure I understand what you want. Your example is nonsensical at best.

_array should return [1,0], if that's what you mean.

 

_x1 = 0; 
_x2 = 0; 
_array = [_x1,_x2]; 
_y = _array select 0; 
_y = _y + 1; 
_array set [0,_y];
systemchat str _array;//returns [1,0]

 

_x doesn't show up anywhere else and is a reserved magic variable, so using it outside of its designated purpose will open up a can of worms.

 

Cheers

 

 

Share this post


Link to post
Share on other sites
4 hours ago, Oldiron said:

in other words i want to change the variable inside the array (_x1), not the array itself or its values.

_x1 = 0;
_x2 = 0;
_array = ["_x1","_x2"];
[] call compile format[ "%1 = %1 + 1", _array select 0 ];

hint str _x1; //1

About the only way to do this is by call compile. Remember that the initial variable will still need to be in scope from where ever you make the changes to its value, when its a local variable.

 

Just to expand on this a bit, the only data types in sqf that are treated as by reference are arrays. So it would be possible without call compile only if the base variables are themselves arrays. For instance...

_x1 = [ 0 ]; 
_x2 = [ 0 ]; 
_array = [_x1,_x2]; 
_array select 0 set [ 0, ( _array select 0 select 0 ) + 1 ]; 

hint str (_x1 select 0); //1

_x1 set [ 0, 2 ];

hint str ( _array select 0 select 0 ); //2

Keep in mind though that the values in _array are changing when the initial referenced array (_x1) value is changed, as they are a reference to _x1, and vice versa.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
10 hours ago, Grumpy Old Man said:

 

Not sure I understand what you want. Your example is nonsensical at best.

_array should return [1,0], if that's what you mean.

 


_x1 = 0; 
_x2 = 0; 
_array = [_x1,_x2]; 
_y = _array select 0; 
_y = _y + 1; 
_array set [0,_y];
systemchat str _array;//returns [1,0]

 

_x doesn't show up anywhere else and is a reserved magic variable, so using it outside of its designated purpose will open up a can of worms.

 

Cheers

 

 

Sorry, my fault, it was an example, i usually use _tmp01 or something, so that is not the problem, but thx

 

Share this post


Link to post
Share on other sites
9 hours ago, Larrow said:

_x1 = 0;
_x2 = 0;
_array = ["_x1","_x2"];
[] call compile format[ "%1 = %1 + 1", _array select 0 ];

hint str _x1; //1

About the only way to do this is by call compile. Remember that the initial variable will still need to be in scope from where ever you make the changes to its value, when its a local variable.

 

Just to expand on this a bit, the only data types in sqf that are treated as by reference are arrays. So it would be possible without call compile only if the base variables are themselves arrays. For instance...


_x1 = [ 0 ]; 
_x2 = [ 0 ]; 
_array = [_x1,_x2]; 
_array select 0 set [ 0, ( _array select 0 select 0 ) + 1 ]; 

hint str (_x1 select 0); //1

_x1 set [ 0, 2 ];

hint str ( _array select 0 select 0 ); //2

Keep in mind though that the values in _array are changing when the initial referenced array (_x1) value is changed, as they are a reference to _x1, and vice versa.

Thx Larrow, i doublecheck this when i am sober again :) but obviously i have to find a workaround. thx a lot

 

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

×