I don’t think you can have TArray<AActor&> Actors;
However, you can have TArray<AActor*>& Actors; You just have to Initialize it when declaring it. A Reference cannot be null.
TArray<AActor*> A;
TArray<AActor*>& B = A;
Essentially, what this does is store A within B, NOT A COPY of A. so if you remove an element from B. it will also be removed from A. I hope that makes sense. I’m trying my best to explain it. References have to be initialized when it is declared. I can think of one time they don’t have to be but you will never deal with that use case within unreal anyway.
You would access a reference like you would any other none pointer variable with the Dot(.); now if it is a reference to a pointer then you use the Arrow( ->).
A reference to a pointer looks like this AActor*& Owner = GetOwner();
you use reference’s cause you don’t want to copy values all the time. It takes up cpu power and memory allocation. Using reference’s is the best way to pass data around. I can think of a few times where you would want to “pass by value”(make a copy) of data, but its rare.
Think of it like this, lets say an AActor* is 8 bytes long. I’m not sure how big it is that’s just a number I’m throwing out there. Now lets say you have an array of 100 AActor*s. so your array is over 800 bytes long at this point. now if you assign your array to a new variable like:
TArray<AActor*> Actors2 = Actors1;
Then you just copied all them bytes when you didn’t have to. You could have just as easily
TArray<AActor*>& Actors2 = Actors1;
Think of a Reference as a Variable that holds another Variable.
your solution:
the range loop is not coping the pointer into the platform var on each iteration. It is “passing by reference”.
for (ATriggerPlatform*& Platform : Platforms)
{
Platform->Activate(true);
}