Bug on for each?

Hello, i have the following variable:

TArray<FTest> Tests;

Where FTest is the following struct:

USTRUCT()
 struct FTest
 {
     GENERATED_USTRUCT_BODY()
 
     float Var;
 
     UPROPERTY(EditDefaultsOnly, Category = "My category")
     float MaxVar;
 
     FTest() {}
 };

If i do the following:

for(FTest Test : Tests)
      Test.Var = 25;

It will not change the values. Is the for each supposed to give copies of the actual values or is this a bug? If i use the regular for it works fine.

for(uint8 i = 0 ; i < Tests.Num() ; i++)
      Tests[i].Var = 25;

Thank you for clarifying that, i’m used to the java for each so that was probably what created the confusion :slight_smile:

So theorically you will never want to use without &, otherwise you will be working with a copy and that’s not really helpful :stuck_out_tongue:

You’re taking a copy in the loop and then changing that, you’ll want to use a reference instead.

for(FTest& Test : Tests)
    Test.Var = 25;

You may sometimes have a legitimate reason to want to perform a copy, but yeah, you’ll typically see one of the following:

for(FTest& Test : Tests)
    Test.Var = 25;

for(const FTest& Test : Tests)
    Func(Test.Var);