spawn and attach a mesh (not proven to work yet!)

This commit is contained in:
Brandon Thomas
2021-03-03 04:21:32 -05:00
parent d949d71eee
commit d9335f3398
5 changed files with 42 additions and 6 deletions
@@ -12,8 +12,9 @@ ASpawnableActor::ASpawnableActor()
// You can turn this off to improve performance if you don't need it. // You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
/*
// Attach a mesh to the object. // Attach a mesh to the object.
static ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); static ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'"));
BaseMesh = BaseMeshAsset.Object; BaseMesh = BaseMeshAsset.Object;
@@ -23,6 +24,11 @@ ASpawnableActor::ASpawnableActor()
BaseMeshComponent->SetStaticMesh(BaseMesh); BaseMeshComponent->SetStaticMesh(BaseMesh);
BaseMeshComponent->SetSimulatePhysics(true); BaseMeshComponent->SetSimulatePhysics(true);
*/
// NB: Only valid to call in the CTOR.
BaseMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseMesh"));
BaseMeshComponent->SetupAttachment(RootComponent);
} }
ASpawnableActor::~ASpawnableActor() ASpawnableActor::~ASpawnableActor()
@@ -44,6 +50,28 @@ void ASpawnableActor::Tick(float DeltaTime)
Super::Tick(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<UStaticMesh> BaseMeshAsset(*meshName);
BaseMesh = BaseMeshAsset.Object;
BaseMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(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) void ASpawnableActor::StartDestroyTimer(float seconds)
{ {
@@ -37,7 +37,7 @@ void ASpawningActor::Tick(float DeltaTime)
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
} }
void ASpawningActor::Spawn() ASpawnableActor* ASpawningActor::Spawn()
{ {
// Spawn actor // Spawn actor
// TODO(bt): This will memory leak. // 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. // 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. // Optionally, we could maintain a queue and cull the oldest actors.
spawnedActor->StartDestroyTimer(10.0); spawnedActor->StartDestroyTimer(10.0);
return spawnedActor;
} }
void ASpawningActor::SpawnCallback(FString Channel, FString Message) void ASpawningActor::SpawnCallback(FString Channel, FString Message)
@@ -4,8 +4,6 @@
#include "UObject/NoExportTypes.h" #include "UObject/NoExportTypes.h"
#include "UObject/Interface.h" #include "UObject/Interface.h"
#include "EventHandlerInterface.generated.h" #include "EventHandlerInterface.generated.h"
/** /**
@@ -29,6 +29,13 @@ public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true"))
class UStaticMesh* BaseMesh; 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. */ /** Set a timer to destroy this actor. */
void StartDestroyTimer(float seconds); void StartDestroyTimer(float seconds);
@@ -5,6 +5,7 @@
#include "UObject/NoExportTypes.h" #include "UObject/NoExportTypes.h"
// EventCenterPlugin // EventCenterPlugin
#include "SpawnableActor.h"
#include "SpawningActor.generated.h" #include "SpawningActor.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(SpawningActorLog, Log, All); DECLARE_LOG_CATEGORY_EXTERN(SpawningActorLog, Log, All);
@@ -22,11 +23,11 @@ public:
/** Create actor. */ /** Create actor. */
UFUNCTION(BlueprintCallable, Category = "SpawningActor") UFUNCTION(BlueprintCallable, Category = "SpawningActor")
void Spawn(); ASpawnableActor* Spawn();
/** Create actor with delegation callback. */ /** Create actor with delegation callback. */
UFUNCTION(BlueprintCallable, Category = "SpawningActor") UFUNCTION(BlueprintCallable, Category = "SpawningActor")
void SpawnCallback(FString Channel, FString Message); void SpawnCallback(FString Channel, FString Message);
/** Mesh to use */ /** Mesh to use */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true"))