I’m having trouble identifying the problem with my code and/or BP. I have 2 arrays that hold AGridTile* objects. One array is used to hold the visible ones for the user while the other is just for pooling. When I “Destroy” a tile I just move it to the pool and hide it in the game so I can reuse it later.
The issue is when I pull tiles out of the pool, I’m also setting their new RelativeLocation. I see the new location in the PIE for a blink of an eye before it sends it right back to its prior relative location before it was originally “Destroyed”. I’ve checked my code to see if I’m moving it anywhere else and I’ve not found anything…
Is there something obvious that I’m just not seeing related to the code below? Any other debugging tips would be greatly appreciated! =)
// Called when the grid requests a new tile to create
AGridTile* AGrid::CreateTile(UTileTemplate* Template, int32 X, int32 Y, int32 YOffsetLocation )
{
AGridTile* Tile = NULL;
// pull from pool if we can
for (auto p = 0; p < Pool.Num(); p++)
if (Pool[p]->Template->ID == Template->ID)
{
Tile = Pool[p];
Pool.RemoveAtSwap(p);
break;
}
FVector RelativeLocation = CellLocation(X, Y + YOffsetLocation);
// create if it wasn't pooled
if (Tile == NULL)
{
auto* const World = GetWorld();
// Set the spawn parameters.
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
Tile = World->SpawnActor<AGridTile>(Template->TileClass, RelativeLocation, FRotator::ZeroRotator, SpawnParams);
Tile->AttachRootComponentToActor(this);
Tile->Template = Template;
}
else
{
// from pool to reset some properties that were
// effected from pooling
Tile->SetActorScale3D( FVector(1.0f, 1.0f, 1.0f) );
Tile->SetActorRelativeLocation(RelativeLocation);
Tile->SetActorEnableCollision(true);
Tile->SetActorHiddenInGame(false);
}
// store it in the correct tile location
Tile->Cell = FVector2D(X, Y);
Tiles[CellIndex(Tile->Cell)] = Tile;
// return
return Tile;
}
void AGrid::DestroyTile(AGridTile* Tile)
{
Tiles[CellIndex(Tile->Cell)] = NULL;
Tile->SetActorHiddenInGame(true);
Tile->SetActorEnableCollision(false);
Tile->SetActorRelativeLocation(FVector(0.0f, 9999999.0f, 0.0f));
Pool.Add(Tile);
}