From d9335f3398ba4ada160aa09ea4f84a292adeefbd Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 3 Mar 2021 04:21:32 -0500 Subject: [PATCH] spawn and attach a mesh (not proven to work yet!) --- .../Private/SpawnableActor.cpp | 30 ++++++++++++++++++- .../Private/SpawningActor.cpp | 4 ++- .../Public/EventHandlerInterface.h | 2 -- .../EventCenterPlugin/Public/SpawnableActor.h | 7 +++++ .../EventCenterPlugin/Public/SpawningActor.h | 5 ++-- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Source/EventCenterPlugin/Private/SpawnableActor.cpp b/Source/EventCenterPlugin/Private/SpawnableActor.cpp index 1be48f5..aa72af1 100644 --- a/Source/EventCenterPlugin/Private/SpawnableActor.cpp +++ b/Source/EventCenterPlugin/Private/SpawnableActor.cpp @@ -12,8 +12,9 @@ ASpawnableActor::ASpawnableActor() // You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; + /* // Attach a mesh to the object. - static ConstructorHelpers::FObjectFinder BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); + static ConstructorHelpers::FObjectFinder BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); BaseMesh = BaseMeshAsset.Object; @@ -23,6 +24,11 @@ ASpawnableActor::ASpawnableActor() BaseMeshComponent->SetStaticMesh(BaseMesh); BaseMeshComponent->SetSimulatePhysics(true); + */ + + // NB: Only valid to call in the CTOR. + BaseMeshComponent = CreateDefaultSubobject(TEXT("BaseMesh")); + BaseMeshComponent->SetupAttachment(RootComponent); } ASpawnableActor::~ASpawnableActor() @@ -44,6 +50,28 @@ void ASpawnableActor::Tick(float DeltaTime) Super::Tick(DeltaTime); } +// TODO: This doesn't work outside of constructor +void ASpawnableActor::SetMeshByString(const FString& meshName) +{ + // Attach a mesh to the object. + // eg. TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'") + static ConstructorHelpers::FObjectFinder BaseMeshAsset(*meshName); + + BaseMesh = BaseMeshAsset.Object; + + BaseMeshComponent = CreateDefaultSubobject(TEXT("BaseMesh")); + + BaseMeshComponent->SetStaticMesh(BaseMesh); + + BaseMeshComponent->SetSimulatePhysics(true); +} + +void ASpawnableActor::SetMesh(UStaticMesh* staticMesh) +{ + BaseMesh = staticMesh; + BaseMeshComponent->SetStaticMesh(BaseMesh); + BaseMeshComponent->SetSimulatePhysics(true); +} void ASpawnableActor::StartDestroyTimer(float seconds) { diff --git a/Source/EventCenterPlugin/Private/SpawningActor.cpp b/Source/EventCenterPlugin/Private/SpawningActor.cpp index 8b28515..e310ea7 100644 --- a/Source/EventCenterPlugin/Private/SpawningActor.cpp +++ b/Source/EventCenterPlugin/Private/SpawningActor.cpp @@ -37,7 +37,7 @@ void ASpawningActor::Tick(float DeltaTime) Super::Tick(DeltaTime); } -void ASpawningActor::Spawn() +ASpawnableActor* ASpawningActor::Spawn() { // Spawn actor // TODO(bt): This will memory leak. @@ -56,6 +56,8 @@ void ASpawningActor::Spawn() // We don't want to keep the actor around indefinitely since they may pile up unused. // Optionally, we could maintain a queue and cull the oldest actors. spawnedActor->StartDestroyTimer(10.0); + + return spawnedActor; } void ASpawningActor::SpawnCallback(FString Channel, FString Message) diff --git a/Source/EventCenterPlugin/Public/EventHandlerInterface.h b/Source/EventCenterPlugin/Public/EventHandlerInterface.h index efaa1bb..31da62d 100644 --- a/Source/EventCenterPlugin/Public/EventHandlerInterface.h +++ b/Source/EventCenterPlugin/Public/EventHandlerInterface.h @@ -4,8 +4,6 @@ #include "UObject/NoExportTypes.h" #include "UObject/Interface.h" - - #include "EventHandlerInterface.generated.h" /** diff --git a/Source/EventCenterPlugin/Public/SpawnableActor.h b/Source/EventCenterPlugin/Public/SpawnableActor.h index c4e7074..8f2f909 100644 --- a/Source/EventCenterPlugin/Public/SpawnableActor.h +++ b/Source/EventCenterPlugin/Public/SpawnableActor.h @@ -29,6 +29,13 @@ public: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) class UStaticMesh* BaseMesh; + // FIXME: This won't work outside of the ctor :( + UFUNCTION(BlueprintCallable, Category = "SpawnableActor") + void SetMeshByString(const FString& meshName); + + UFUNCTION(BlueprintCallable, Category = "SpawnableActor") + void SetMesh(UStaticMesh* staticMesh); + /** Set a timer to destroy this actor. */ void StartDestroyTimer(float seconds); diff --git a/Source/EventCenterPlugin/Public/SpawningActor.h b/Source/EventCenterPlugin/Public/SpawningActor.h index 0a7476b..8f7c993 100644 --- a/Source/EventCenterPlugin/Public/SpawningActor.h +++ b/Source/EventCenterPlugin/Public/SpawningActor.h @@ -5,6 +5,7 @@ #include "UObject/NoExportTypes.h" // EventCenterPlugin +#include "SpawnableActor.h" #include "SpawningActor.generated.h" DECLARE_LOG_CATEGORY_EXTERN(SpawningActorLog, Log, All); @@ -22,11 +23,11 @@ public: /** Create actor. */ UFUNCTION(BlueprintCallable, Category = "SpawningActor") - void Spawn(); + ASpawnableActor* Spawn(); /** Create actor with delegation callback. */ UFUNCTION(BlueprintCallable, Category = "SpawningActor") - void SpawnCallback(FString Channel, FString Message); + void SpawnCallback(FString Channel, FString Message); /** Mesh to use */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true"))