Hello,
i’ve made a test BP of a building that spawns aliens in 10 second intervals. The problem is the AI of the alien instance (BP) does not work. So I thought i’ll do it in code (i’d do it in the future). The problem here is that, when i call world spawn, i do not see the spawned object and in the editor there is a deleted actor. I think the object just got garbage collected. Can somebody please give me directions on what am i doing wrong? Because i searched for answers and everything that i found does not work.
To the test the object i only drag and drop it from content browser to scene and then hit run. I am using UE 4.8.3 because UE 4.9.0 was just crashing and crashing .
joe
code below
(.h)
#pragma once
#include "Engine/StaticMeshActor.h"
#include "EnemyHiveCpp.generated.h"
/**
*
*/
UCLASS(Config = Game, BlueprintType, Blueprintable)
class TESTCPP48_API AEnemyHiveCpp : public AStaticMeshActor
{
GENERATED_BODY()
FDateTime timeStart;
UClass* classToSpawn;
public:
UPROPERTY()
AActor * spawnedPawn;
AEnemyHiveCpp();
virtual void TickActor(float DeltaTime, enum ELevelTick TickType, FActorTickFunction & ThisTickFunction) override;
};
(.cpp)
#include “TestCpp48.h”
#include “EnemyHiveCpp.h”
AEnemyHiveCpp::AEnemyHiveCpp()
{
PrimaryActorTick.bCanEverTick = true;
ConstructorHelpers::FObjectFinder<UBlueprint> enemyPawnToSpawn(TEXT("Blueprint'/Game/MyEnemyPawn.MyEnemyPawn'"));
if (enemyPawnToSpawn.Object != nullptr)
{
classToSpawn = (UClass*)enemyPawnToSpawn.Object->GeneratedClass;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DID NOT FIND OBJECT"));
}
spawnedPawn = nullptr;
}
void AEnemyHiveCpp::TickActor(float DeltaTime, enum ELevelTick TickType, FActorTickFunction & ThisTickFunction)
{
Super::Tick(DeltaTime);
FDateTime now = FDateTime::Now();
FTimespan d = now - timeStart;
int32 sec = d.GetSeconds();
if (sec > 10)
{
FVector location = GetActorLocation();
FActorSpawnParameters fasp;
if (classToSpawn != nullptr)
{
FVector location;
FActorSpawnParameters fasp;
fasp.Owner = this;
fasp.Instigator = Instigator;
fasp.bDeferConstruction = false;
timeStart = now;
if (spawnedPawn == nullptr)
{
spawnedPawn = GetWorld()->SpawnActor(classToSpawn, &location, &FRotator::ZeroRotator, fasp);
UE_LOG(LogTemp, Warning, TEXT("SPAWNED %s"), *(spawnedPawn->GetName()));
//spawnedPawn->SetFlags(RF_RootSet);
}
}
}
}