x


Setting arrays equal to each other issue

Hi,

when I want to make tempArr = masterArray, it works. However, when I change the contents of tempArr, the masterArray follows suit.

In my program I want the master array to be the unchanging list of data for the temp array to refer too. In my program I have this:

 for (var i : int = 0; i<masterArray.length; i++){
     tempInt = Mathf.Round(Random.Range(0, tempPosArray.length)); //random index
     tempArrayP[i] = tempPosArray[tempInt]; 
     tempPosArray.RemoveAt(tempInt);
 }
 tempPosArray = masterArray; 

This function makes a new array(tempArrayP) which is the same thing as tempPosArray with a randomized order. In order to make this happen, I pick a random element out of the array, I plug it into the new temp array and remove it from the old one So that there are no duplicates.

The problem: when I remove elements from tempPosArray, those same elements are moved from masterArray as well...and WHY? masterArray is on the left side of the equal sign...I just don't get it. is there a way to make masterArray concrete, unchanging. masterArray is set in the Start (); function.

more ▼

asked May 23, 2012 at 06:20 PM

wknight92 gravatar image

wknight92
45 8 9 13

bumppppppp

May 23, 2012 at 06:32 PM wknight92

You are sure there is no place in you code that says something like

 masterArray=oneOfTheOtherArrays;

Since that will modify the masterArray instead of the other array. You said "masterArray is on the left side of the equal sign" which will modify the masterArray and not the other, mind posting the other lines where masterArray is compared to another array.

May 23, 2012 at 06:37 PM hijinxbassist

oops, I meant 'right side' as shown above where 'tempPosArray = masterArray;'

this function is called through a SendMessage on startup: function GetOriginalPosition (theirRelativePosition : Vector3){ masterArray[iP] = theirRelativePosition; iP++; }

This function is called right before the for loop in my OP. function RefreshValues () { tempPosArray = masterPositionArray; sendIndex = 0;
}

May 23, 2012 at 06:44 PM wknight92

the first function is where masterArray is set, the second function is where it is used(plugged into temp array)

oh by the way, masterArray is the same thing as masterPositionArray

May 23, 2012 at 06:46 PM wknight92

If you are doing the above just to randomize the array, a simpler method is to shuffle the array in place. Just repeatedly swap randomly selected elements a bunch of times.

Mar 15, 2013 at 01:50 AM robertbu
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Using the assignment operator, you are actually just referencing the original Array. To make a copy, instantiate the new array with the same size as the source array, then iterate through the original array and assign the individual values to the same index within the new array.

more ▼

answered May 23, 2012 at 06:59 PM

GamesRUs gravatar image

GamesRUs
45 3 2 6

this makes sense thank you!

May 23, 2012 at 07:17 PM wknight92
(comments are locked)
10|3000 characters needed characters left

You have to understand that arrays are reference types. That means, that they don't actually store values, they only store references to those values. So when you have a line of code like

 tempPosArray = masterArray; // called before  your function as well I suppose

what you actually do is copying a reference to a memory location, meaning that any changes to the memory at that location (including removing elements) will be reflected in both arrays. That was the "why" part. Now to change this, just use

 tempPosArray = Array.copy(masterArray, tempPosArray, masterArray.Length);
more ▼

answered May 23, 2012 at 06:53 PM

hathol gravatar image

hathol
1.7k 5 7 17

what is Array.copy();? I don't see it anywhere in the Unity API.

May 23, 2012 at 07:14 PM wknight92

Array.Copy is a c# function that copies on array into another. http://msdn.microsoft.com/en-us/library/system.array.copy(v=vs.71).aspx

sry, I didn't realize you were using JS. Use the method @GamesRUs proposed instead then :)

May 23, 2012 at 08:11 PM hathol

Yes, I started to say to use Array.Copy until I noticed it was JS. :)

May 23, 2012 at 08:43 PM GamesRUs

it almost works, for me it said Assets/MarchingCubes3/@Scripts/tunnels.js(36,51): BCE0022: Cannot convert 'void' to 'float[]'.

 distances_sort = System.Array.Copy(distances, distances_sort, nodes.Length);
Mar 15, 2013 at 12:14 AM MountDoomTeam

You do not need the 'distanaces_sort = ' portion as the Array.Copy method does not return a value. What it does is takes the array in parameter one (distances) and copies it into the array in parameter two (distances_sort) starting with the first element and stopping with the element index specified in parameter three (nodes.Length in your case). Are you sure that you don't want to use distances.Length instead of nodes.Length though?

In short - the error you received is telling you that Array.Copy returns void and you are trying to assign void to a variable of type float[] (an array of floats which is the distances_sort array).

Mar 15, 2013 at 01:13 PM GamesRUs
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2324
x711
x24
x22
x6

asked: May 23, 2012 at 06:20 PM

Seen: 4126 times

Last Updated: Mar 15, 2013 at 01:14 PM