I have a spawner object, whose job is to regularly spawn fence objects.
Originally, my idea was to have one fence object, whose mesh and material could be assigned at runtime. This approach proved futile, as the fences were always invisible.
I have shifted to an idea where I can use the fence code to create multiple blueprints, each with its own assigned mesh (which accounts for the different possible heights.)
However, I want to know if this is a viable strategy before I roll with it. i.e.
// Called every frame
void AObstacleSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
switch (state) {
case SPAWN_PLAY:
//Get a reference to the game world so we can spawn new objects as needed
UWorld* World = GetWorld();
if (World) {
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
//Increment/Decrement timers, spawn objects
if (SpawnTimer <= 0.0f) {
//Spawn a new fence obstacle with appropriate height
AFenceObstacle* NewFence = World->SpawnActor<Fence_BP1>(SpawnParams); //<--- This is the code I want to validate
FVector SpawnLocation = FVector(50.0f, 50.0f, 0.0f); //X = 50 is for debugging purposes only. Y set at 50 is strictly for debug; reset to 0 later.
NewFence->SetActorLocation(SpawnLocation);
NewFence->ObstacleSpeed = ObstacleSpeed;
//DEBUG: Show a prompt indicating that a new fence has been created
//FVector loc = NewFence->GetActorLocation();
auto const debug_msg = FString::Printf(TEXT("New fence %i spawned. Height = %i. Spd = %f. X = %f. Y = %f. Z = %f"), FenceIndex, FenceHeight, ObstacleSpeed, SpawnLocation.X, SpawnLocation.Y, SpawnLocation.Z);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, debug_msg);
//GUBED
Fences[FenceIndex] = NewFence;
FenceIndex++;
if (FenceIndex >= 4) {
FenceIndex = 0;
}
SpawnTimer = 5.0f; //<-- This will be modified later to depend on TimeElapsed
}
else {
SpawnTimer -= DeltaTime;
}
int ii;
FVector CurrentLocation;
if (TimeToAccel <= 0.0f) {
ObstacleSpeed += ObstacleAccel;
//Set the speeds of the fences
for (ii = 0; ii < 4; ii++) {
if (Fences[ii] != nullptr) {
Fences[ii]->ObstacleSpeed = ObstacleSpeed;
CurrentLocation = Fences[ii]->GetActorLocation();
if (CurrentLocation.X < -1000.0) {
Fences[ii]->Destroy();
Fences[ii] = nullptr;
}
}
}
TimeToAccel = 5.0f; //<-- This will be modified later to depend on TimeElapsed
}
else {
TimeToAccel -= DeltaTime;
}
TimeElapsed += DeltaTime;
}
}
}