Download

Mathematical functions to load world around player?

Hi

TLDR: I have players, I need the game to keep track of areas around each player, add new ones and remove old ones as they move around without too much of performance hit

I have this function which takes TArray<APlayerController*> Players which contains all players in game and then double-for loops each of them to get square areas around each.


 for (int i = 0; i < Players.Num(); i++)
{
FVector position = Players*->GetFocalLocation();
int loadDistance = 3;
int chunkSize = 20;

for (int x = 0; x < loadDistance; x++)
{
for (int z = 0; z < loadDistance; z++)
{
FVector chunk = FVector((position.X / chunkSize) - (loadDistance / 2) + x, 0, (position.Z / chunkSize) - (loadDistance / 2) + z);
FVector flooredChunk = FVector(FMath::Floor(chunk.X), 0, FMath::Floor(chunk.Z));

if (!ActiveChunks.Contains(flooredChunk))
{
ActiveChunks.Add(flooredChunk);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Chunk %s activated!"), *flooredChunk.ToString()));
}
}
}

}
}

It sucks because performance. This is merely the first part of the loading, actually getting the area so this is a no-go as it is right now. Any ideas to improve it/replace it?

Once again, what I need is function that would divide world into squares of certain size and get all of them around each player/remove those who are far away. If anything is unclear just write. Thanks

So I slightly modified it myself and its ok for now