Hi all. I am fairly new to coding c++ and very new to Unreal. I have made an AActor called Bird (it will just fly in a straight line once it is spawned. Be animated. And live for n secs, then destroy itself).
I also create a Tree AActor. The bird and tree, I have also created BP off.
I am a coder first and foremost and enjoy coding written rather than visual scripting.
I created an ‘Actor Component’ class called BirdSpawner. This component is intended to be a component added to Tree. My plan was to have BirdSpawner hold a reference to the Bird_BP file and spawn a bird after specified interval.
Now, when I go into my Tree_BP to assign the AActor variable ‘Bird’, I have problem. The only items I can choose in drop down are existing scene Actors. And even if I drag a bird somewhere into the scene so that it becomes an option in the dropdown, I still cannot choose it. -I am pretty certain therefore that the ‘Bird_BP’ is not actually of type AActor.
So how do I make a component keep a reference to an Actor_BP and spawn it?
Here is my C++ code:
class INSANO_WORLD_API ABird : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABird();
private:
void UpdateBirdLocation(float DeltaTime);
void HandleLifespan(float DeltaTime);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditDefaultsOnly, Category = Colliders)
class USphereComponent* SphereComponent;
UPROPERTY(EditDefaultsOnly, Category = Mesh)
class USkeletalMeshComponent* SkeletalMeshComponent;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Mesh)
FVector MeshLocationOffset;
UPROPERTY(EditAnywhere, Category = Behaviour)
float Speed;
UPROPERTY(EditAnywhere, Category = Behaviour)
FVector Direction;
UPROPERTY(EditAnywhere, Category = Behaviour)
float Lifespan;
};
ABird::ABird()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>("SphereCollider");
RootComponent = SphereComponent;
SkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
SkeletalMeshComponent->SetupAttachment(RootComponent);
SkeletalMeshComponent->SetRelativeLocation(MeshLocationOffset);
}
// Called when the game starts or when spawned
void ABird::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABird::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdateBirdLocation(DeltaTime);
HandleLifespan(DeltaTime);
}
void ABird::UpdateBirdLocation(float DeltaTime)
{
FVector Loc = GetActorLocation();
Loc = Loc + Direction * Speed * DeltaTime;
FHitResult Hit;
SetActorLocation(Loc, true, &Hit);
}
void ABird::HandleLifespan(float DeltaTime)
{
Lifespan -= DeltaTime;
if (Lifespan <= 0)
{
Destroy();
}
}
class INSANO_WORLD_API ATree : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATree();
UPROPERTY(EditAnywhere, Category = Mesh)
class UStaticMeshComponent* StaticMesh;
UPROPERTY(EditAnywhere, Category = Spawners)
class USpawner_Bird* Spawner_Bird;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
ATree::ATree()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
RootComponent = StaticMesh;
Spawner_Bird = CreateDefaultSubobject<USpawner_Bird>(TEXT("Spawner Bird"));
Spawner_Bird->SetActive(true);
}
// Called when the game starts or when spawned
void ATree::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATree::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
class INSANO_WORLD_API USpawner_Bird : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
USpawner_Bird();
UPROPERTY(EditAnywhere, Category = Behaviour)
float Interval;
float Interval_Timer = 0.0f;
UPROPERTY(EditAnywhere, Category = Behaviour)
FVector BirdDirection;
UPROPERTY(EditAnywhere, Category = Behaviour)
AActor* Bird; // **<- THIS IS WHERE I AM HAVING PROBLEMS**
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
USpawner_Bird::USpawner_Bird()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void USpawner_Bird::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void USpawner_Bird::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
Interval_Timer += DeltaTime;
if (Interval_Timer >= Interval)
{
Interval_Timer = 0.f;
}
}