TL;DR: How do you return a TArray of FVector2Ds created in a C++ function to Blueprint?
I’ve been trying to return a TArray of FVector2D, but whenever I call the function in Blueprint and/or want to use the result, all it does is crash. How do I properly return a TArray of FVector2D?
This is my current header and implementation:
/** Generates sample points in 2D space using Poisson Disc distribution. **/
UFUNCTION(BlueprintCallable, meta = (FriendlyName = "2D Distribute (Poisson Disc)", Keywords = "Random Poisson Disc Distribution 2D"), Category = "Random|Distribution")
static TArray<FVector2D> Generate2D(float fWidth = 1.0f, float fHeight = 1.0f, float fSampleDistance = 0.5f, int32 iMaxNewPoints = 30);
TArray<FVector2D> UPoissonDisc2D::Generate2D(float fWidth, float fHeight, float fSampleDistance, int iMaxNewPoints) {
unsigned int iCells, iCellsX, iCellsY; double dCellSize;
unsigned int iCell, iCellX, iCellY;
FVector2D* v2DSample; FVector2D** v2DSamples;
TDoubleLinkedList<FVector2D*>* v2DList;
UE_LOG(LogTemp, Log, TEXT("PoissonDisc2D: Starting generation with parameters: %f, %f, %f, %d."), fWidth, fHeight, fSampleDistance, iMaxNewPoints);
// Calculate cell size, cell count and sample count.
dCellSize = fSampleDistance / sqrt(2.0); iCellsX = ceil(fWidth / dCellSize); iCellsY = ceil(fHeight / dCellSize); iCells = iCellsX * iCellsY;
UE_LOG(LogTemp, Log, TEXT("PoissonDisc2D: Calculated Grid of size %d, %d using cell size %f (Total cells: %d)."), iCellsX, iCellsY, dCellSize, iCells);
// Create Arrays containing our elements temporarily.
v2DSamples = new FVector2D*[iCells];
v2DList = new TDoubleLinkedList<FVector2D*>();
for (unsigned int n = 0; n < iCells; ++n)
v2DSamples[n] = NULL;
// Generate starting point at random position. and insert it into the data structure.
v2DSample = new FVector2D(((float)rand() / (float)RAND_MAX) * fWidth, ((float)rand() / (float)RAND_MAX) * fHeight);
iCellX = (int)(v2DSample->X / dCellSize); iCellY = (int)(v2DSample->Y / dCellSize); iCell = iCellX + (iCellY * iCellsX);
v2DSamples[iCell] = v2DSample; v2DList->AddTail(v2DSample);
UE_LOG(LogTemp, Log, TEXT("PoissonDisc2D: Created sample at %f, %f in cell %d, %d."), v2DSample->X, v2DSample->Y, iCellX, iCellY);
// Lots of commented out code.
// Create output array of samples and fill it.
TArray<FVector2D>* v2DOutputArray = new TArray<FVector2D>();
for (unsigned int n = 0; n < iCells; ++n) {
if (v2DSamples[n] != NULL)
v2DOutputArray->Add(*v2DSamples[n]);
}
// Destroy temporary arrays.
delete v2DSamples; delete v2DList;
// Return created output array.
return *(v2DOutputArray);
}