I’m attempting to spawn a physical Waypoint actor from a WaypointManager object I have set up. The logic goes as follows:
- Controller gets mouse click info from player input
- Controller passes that info to the WPManager class via
WPManager->SetNewWPLocation(OutHit.ImpactPoint);
- The WPManager object updates its list of WP locations stored in
TArray WPList;
- WPManager calls
UpdateWaypoints()
, which is supposed to delete all existing Waypoint actors and respawn new Waypoint actors based on the updatedWPList
The new Waypoints are spawned with no issues, but I can’t set their location. I’ve tried setting their locations from the WPManager class (which is ideal), and also from the Waypoint actor class itself for debugging purposes, but the Waypoints are stuck at the world origin.
WaypointManager.h
UCLASS()
class UWaypointManager : public UObject
{
GENERATED_UCLASS_BODY()
/** Stores entire waypoint array */
UPROPERTY()
TArray<FVector> WPList;
/** Stores pointers to actual waypoint actors */
UPROPERTY()
TArray<AWaypoint*> WPActorList;
/** Initial call for creating a new waypoint */
UFUNCTION(BlueprintCallable, Category = "Waypoints")
void SetNewWPLocation(FVector inLoc);
/** Updates ALL waypoints (not 100% efficient) */
void UpdateWaypoints();
/** Stores pointer to world for context, set from Controller */
UWorld* World;
};
WaypointManager.cpp
UWaypointManager::UWaypointManager(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
}
void UWaypointManager::SetNewWPLocation(FVector inLoc)
{
WPList.Add(inLoc);
UpdateWaypoints();
}
void UWaypointManager::UpdateWaypoints()
{
// Delete existing waypoints
for (int32 i = 0; i < WPActorList.Num(); i++)
{
if (WPActorList[i])
WPActorList[i]->Destroy();
}
WPActorList.Empty();
// Respawn waypoints
if (World)
{
for (int32 i = 0; i < WPList.Num(); i++)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
WPActorList.Add(World->SpawnActor<AWaypoint>(WPList[i], FRotator(0, 0, 0), SpawnInfo));
if (WPActorList[i])
if (WPActorList[i]->SetActorLocation(WPList[i]))
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, FString("Successfully set location"));
}
}
}
Waypoint.h
UCLASS()
class AWaypoint : public AActor
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() OVERRIDE;
virtual void Tick(float DeltaTime) OVERRIDE;
};
Waypoint.cpp
AWaypoint::AWaypoint(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
PrimaryActorTick.bCanEverTick = true;
}
void AWaypoint::BeginPlay()
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString("WPActor spawned!"));
// Temporary SetLocation call for debugging (doesn't work)
SetActorLocation(FVector(0, 0, 50));
}
void AWaypoint::Tick(float DeltaTime)
{
DrawDebugSphere((), GetActorLocation(), 16.f, 8, FColor::Red);
GEngine->AddOnScreenDebugMessage(-1, DeltaTime, FColor::Green, GetActorLocation().ToString());
}
The DebugSpheres are being drawn, so I know the Waypoints are being spawned, but they’re all at the world origin, and GEngine->AddOnScreenDebugMessage(-1, DeltaTime, FColor::Green, GetActorLocation().ToString());
in AWaypoint::Tick
prints (0,0,0).
I’m stumped, any and all help is welcome