From a048494d97bd6852d2f7a9f5c9cc5b3c7baf0dfe Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 16 Feb 2021 04:35:24 -0500 Subject: [PATCH] Actors that spawn other actors (no meshes yet - that segfaults) --- .../Private/SpawnableActor.cpp | 47 ++++++++++++++ .../Private/SpawningActor.cpp | 64 +++++++++++++++++++ .../EventCenterPlugin/Public/SpawnableActor.h | 38 +++++++++++ .../EventCenterPlugin/Public/SpawningActor.h | 40 ++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 Source/EventCenterPlugin/Private/SpawnableActor.cpp create mode 100644 Source/EventCenterPlugin/Private/SpawningActor.cpp create mode 100644 Source/EventCenterPlugin/Public/SpawnableActor.h create mode 100644 Source/EventCenterPlugin/Public/SpawningActor.h diff --git a/Source/EventCenterPlugin/Private/SpawnableActor.cpp b/Source/EventCenterPlugin/Private/SpawnableActor.cpp new file mode 100644 index 0000000..0bf39c0 --- /dev/null +++ b/Source/EventCenterPlugin/Private/SpawnableActor.cpp @@ -0,0 +1,47 @@ +#include "SpawnableActor.h" + +#include "UObject/Object.h" + +DEFINE_LOG_CATEGORY(SpawnableActorLog); + +ASpawnableActor::ASpawnableActor() +{ + UE_LOG(EventSourceLog, Log, TEXT("CTOR()")); + + // 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; +} + +ASpawnableActor::~ASpawnableActor() +{ +} + +void ASpawnableActor::BeginPlay() +{ + Super::BeginPlay(); +} + +void ASpawnableActor::BeginDestroy() +{ + Super::BeginDestroy(); +} + +void ASpawnableActor::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); +} + + +void ASpawnableActor::SetMesh() +{ + //BaseMesh = CreateDefaultSubobject(TEXT("BaseMesh")); + //BaseMesh->SetupAttachment(RootComponent); + + /*static ConstructorHelpers::FObjectFinder BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); + + if (BaseMeshAsset.Object) + { + BaseMesh->SetStaticMesh(BaseMeshAsset.Object); + }*/ +} diff --git a/Source/EventCenterPlugin/Private/SpawningActor.cpp b/Source/EventCenterPlugin/Private/SpawningActor.cpp new file mode 100644 index 0000000..f0f5a96 --- /dev/null +++ b/Source/EventCenterPlugin/Private/SpawningActor.cpp @@ -0,0 +1,64 @@ +#include "SpawningActor.h" + +#include "SpawnableActor.h" + +#include "Engine/World.h" +#include "UObject/Object.h" + +DEFINE_LOG_CATEGORY(SpawningActorLog); + +ASpawningActor::ASpawningActor() +{ + UE_LOG(EventSourceLog, Log, TEXT("CTOR()")); + + // 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; +} + +ASpawningActor::~ASpawningActor() +{ +} + +void ASpawningActor::BeginPlay() +{ + Super::BeginPlay(); +} + +void ASpawningActor::BeginDestroy() +{ + Super::BeginDestroy(); +} + +void ASpawningActor::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); +} + +void ASpawningActor::Spawn() +{ + //SpawnActor(this, this); + + FTransform location; + FRotator rotation; + //GetWorld()->SpawnActor(ASpawnableActor::StaticClass(), &spawnLocation); + + /*ASpawnableActor* spawnedActor = (ASpawnableActor*)GetWorld()->SpawnActor( + ASpawnableActor::StaticClass(), + NAME_None, + &location, + &rotation, + nullptr, // ?? + false, // bNoCollisionFail + false, // ?? + nullptr, // owner + nullptr // instigator + );*/ + //ASpawnableActor* spawnedActor = (ASpawnableActor*)GetWorld()->SpawnActor(&spawnLocation); + ASpawnableActor* spawnedActor = (ASpawnableActor*)GetWorld()->SpawnActor(ASpawnableActor::StaticClass()); + + spawnedActor->SetMesh(); + + +} + diff --git a/Source/EventCenterPlugin/Public/SpawnableActor.h b/Source/EventCenterPlugin/Public/SpawnableActor.h new file mode 100644 index 0000000..7f7b3d8 --- /dev/null +++ b/Source/EventCenterPlugin/Public/SpawnableActor.h @@ -0,0 +1,38 @@ +#pragma once + +// Unreal Core +#include "CoreMinimal.h" +#include "UObject/NoExportTypes.h" + +// EventCenterPlugin +#include "SpawnableActor.generated.h" + +DECLARE_LOG_CATEGORY_EXTERN(SpawnableActorLog, Log, All); + +// NB(bt): "Blueprintable" seems to remove the ability to add BP event nodes. +UCLASS() +class EVENTCENTERPLUGIN_API ASpawnableActor : public AActor +{ + GENERATED_BODY() + +public: + + ASpawnableActor(); + ~ASpawnableActor(); + + void SetMesh(); + + /** Mesh to use */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) + class UStaticMeshComponent* BaseMesh; + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + virtual void BeginDestroy() override; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; +}; diff --git a/Source/EventCenterPlugin/Public/SpawningActor.h b/Source/EventCenterPlugin/Public/SpawningActor.h new file mode 100644 index 0000000..ac54348 --- /dev/null +++ b/Source/EventCenterPlugin/Public/SpawningActor.h @@ -0,0 +1,40 @@ +#pragma once + +// Unreal Core +#include "CoreMinimal.h" +#include "UObject/NoExportTypes.h" + +// EventCenterPlugin +#include "SpawningActor.generated.h" + +DECLARE_LOG_CATEGORY_EXTERN(SpawningActorLog, Log, All); + +// NB(bt): "Blueprintable" seems to remove the ability to add BP event nodes. +UCLASS() +class EVENTCENTERPLUGIN_API ASpawningActor : public AActor +{ + GENERATED_BODY() + +public: + + ASpawningActor(); + ~ASpawningActor(); + + /** Create actor. */ + UFUNCTION(BlueprintCallable, Category = "SpawningActor") + void Spawn(); + + /** Mesh to use */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true")) + class UStaticMeshComponent* BaseMesh; + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + virtual void BeginDestroy() override; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; +};