Modifying array of structs of arrays - can you not copy?

So as far as I get it “For Each Loop” gives you a copy of element in the array to work with (rather than a reference).
So let’s say I have a struct “Data” that has an integer “magicNumber” and an array “veryBigExpensiveArray” (holding whatever). I’ll write some pseudo-code to help explain:

struct Data
{
int magicNumber;
Whatever veryBigExpensiveArray;
}

Then I make an array of “Data” called “arrayOfData”.

Data arrayOfData;

Then I want to change the “magicNumber” in the “Data” at the index = 0 of “arrayOfData” to, let’s say 7.

arrayOfData[0].magicNumber = 7;

Since I get a copy in “For Each Loop” I can’t change it directly but I need to make a new “Data”, set “magicNumber” to 7 and copy “veryBigExpensiveArray” over.

Data temp;
temp.magicNumber = 7;
temp.veryBigExpensiveArray = arrayOfData[0].veryBigExpensiveArray .makeCopy();
arrayOfData[0] = temp;

So in the end, the “veryBigExpensiveArray” gets copied twice: once in “For Each Loop” and second time when I replace “arrayOfData” at index 0.

Is there no better way? Not only is this costly but the graph starts getting crowded…

You can try this: Do a Find() on the array using the array index you also get in the loop - IIRC Find() returns a reference, rather than a copy, and you should be able to set magicNumber on the reference you got from the Find().

2 Likes

Find() returns the index (int) but this led me to Get(ref) which works as you described. Thank you @Hellcat !