Jump to content
Sign in to follow this  
jacmac

Bizarre Array Behavior

Recommended Posts

So I'm trying to build an array of positions. I start with a marker position and use set commands to alter the position in an array variable. I then add the position to an array of positions:

_originpos = getMarkerPos _marker;
_pos = _originpos;
diag_log  ("wp _pos " + str _pos);
_pos set [0,((_originpos select 0)+25)];
diag_log  ("wp _pos " + str _pos);
_pos set [1,((_originpos select 1)-25)];
diag_log  ("wp _pos " + str _pos);
_wppos = [_pos];
diag_log  ("wp _wppos " + str _wppos);
_pos set [0,((_originpos select 0)+25)];
diag_log  ("wp _pos " + str _pos);
_pos set [1,((_originpos select 1)+30)];
diag_log  ("wp _pos " + str _pos);
_wppos = _wppos + [_pos];
diag_log  ("wp _wppos " + str _wppos);
_pos set [0,((_originpos select 0)-30)];
diag_log  ("wp _pos " + str _pos);
_pos set [1,((_originpos select 1)+30)];
diag_log  ("wp _pos " + str _pos);
_wppos = _wppos + [_pos];
diag_log  ("wp _wppos " + str _wppos);
_pos set [0,((_originpos select 0)-30)];
diag_log  ("wp _pos " + str _pos);
_pos set [1,((_originpos select 1)-30)];
diag_log  ("wp _pos " + str _pos);
_wppos = _wppos + [_pos];
diag_log  ("wp _wppos " + str _wppos);

The code above produces some rather bizarre results:

"   _pos [5344.89,5240.91,0]" Origin
"   _pos [5369.89,5240.91,0]" Element 0 altered
"   _pos [5369.89,5215.91,0]" Element 1 altered
"_wppos [[5369.89,5215.91,0]]" _pos added to _wppos
"   _pos [5394.89,5215.91,0]" Element 0 altered
"   _pos [5394.89,5245.91,0]" Element 1 altered
"_wppos [[5394.89,5245.91,0],[5394.89,5245.91,0]]" _pos added to _wppos, but wait! WTF???!?!? What happened to [5369.89,5215.91,0]???
"   _pos [5364.89,5245.91,0]" Element 0 altered
"   _pos [5364.89,5275.91,0]" Element 1 altered
"_wppos [[5364.89,5275.91,0],[5364.89,5275.91,0],[5364.89,5275.91,0]]" WHAT???
"   _pos [5334.89,5275.91,0]" Element 0 altered
"   _pos [5334.89,5245.91,0]" Element 1 altered
"_wppos [[5334.89,5245.91,0],[5334.89,5245.91,0],[5334.89,5245.91,0],[5334.89,5245.91,0]]" OMG FAIL!

Why do all of the elements in _wppos get overwritten when _pos is added to the array? It is like _wppos = _wppos + [_pos]; means add [_pos] as a new element to the array and copy it into every existing element in the array.

Share this post


Link to post
Share on other sites

Biki entry on Array data types

Note that arrays are treated in a similar way to objects - the variable points to the data, so declaring an array containing another array does not copy the original array, just references it.

This means that changing _pos after _wppos = [_pos], will affect _wppos.

Share this post


Link to post
Share on other sites
Biki entry on Array data types

Note that arrays are treated in a similar way to objects - the variable points to the data, so declaring an array containing another array does not copy the original array, just references it.

This means that changing _pos after _wppos = [_pos], will affect _wppos.

That does explain the behavior. Nutty as it seems, this function manages to dereference the array elements and add an array into an array:

_func_AddArrayIntoArray =
{
_temp = [];
_addlength = count (_this select 1);
for "_i" from 0 to (_addlength - 1) do
{
	_temp set [_i, ((_this select 1) select _i)]
};
_return = ((_this select 0) + [_temp]);
_return
};

I have to say though, this array behavior is pretty much mentally bankrupt!

Share this post


Link to post
Share on other sites

what mentally bankrupt is the example function above since you can just add arrays a = a + with no consequence it is set function you have to be careful with

Share this post


Link to post
Share on other sites
what mentally bankrupt is the example function above since you can just add arrays a = a + with no consequence it is set function you have to be careful with

Sorry, I can't understand what you're trying to say; your sentence has no breaks in it.

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  

×