How to get a reference of a struct array?

I want to make an array of struct like in this working blueprint script.

What I am doing wrong in my code?

void AMyGameMode::RandomRow(ST_MyStruct& Item)
{
  TArray<ST_MyStruct> Arr[100];
  TArray<ST_MyStruct> &GetByRef = Arr;
  GetByRef = Arr[FMath::RandRange(0, Arr.Num() - 1)];
  FCustomThunkTemplates::Array_Shuffle(Arr);
  Item = GetByRef;
}

Error on compile time:

GetByRef’: ‘TArray<ST_MyStruct,FDefaultAllocator> &’ differs in levels of indirection from 'ST_MyStruct

1 Like

heya Alexa! try this

TArray<ST_MyStruct> Arr[100];

ST_MyStruct  &GetByRef = Arr[FMath::RandRange(0, Arr.Num() - 1)];
FCustomThunkTemplates::Array_Shuffle(Arr);
Item = GetByRef;

Hope it Helps
cheers!

2 Likes

Now I got the error Array index out of bounds: 0 from an array of size 0 UE4Editor-Win64-DebugGame.exe has triggered a breakpoint. :tired_face:

your array is empty, you are trying to randomize an empty array. Set the maximum range before you randomize it.

Arr.SetNum(100);

hope it helps
cheers!

2 Likes

Thank You very very much for solving this issue, appreciated very much :slight_smile: