From 66cdfd5f6f8d43373279cc82c1a6be62fff1b321 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 7 Mar 2021 03:13:50 -0500 Subject: [PATCH] Async sound playing works! (in vocodes plugin only) I haven't spawned this new actor type yet... --- .../Private/SoundSpawnActor.cpp | 39 +++++++++++++++ .../Private/SpawnableActor.cpp | 49 ------------------- .../Public/SoundSpawnActor.h | 33 +++++++++++++ .../Public/SpawnableActor.h | 16 +----- 4 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 Source/ActorSpawningPlugin/Private/SoundSpawnActor.cpp create mode 100644 Source/ActorSpawningPlugin/Public/SoundSpawnActor.h diff --git a/Source/ActorSpawningPlugin/Private/SoundSpawnActor.cpp b/Source/ActorSpawningPlugin/Private/SoundSpawnActor.cpp new file mode 100644 index 0000000..46e66cb --- /dev/null +++ b/Source/ActorSpawningPlugin/Private/SoundSpawnActor.cpp @@ -0,0 +1,39 @@ +#include "SoundSpawnActor.h" + +#include "UObject/Object.h" +#include "Kismet/GameplayStatics.h" + +DEFINE_LOG_CATEGORY(SoundSpawnActorLog); + +ASoundSpawnActor::ASoundSpawnActor() +{ + UE_LOG(SoundSpawnActorLog, Log, TEXT("CTOR()")); +} + +ASoundSpawnActor::~ASoundSpawnActor() +{ +} + +void ASoundSpawnActor::BeginPlay() +{ + Super::BeginPlay(); +} + +void ASoundSpawnActor::BeginDestroy() +{ + Super::BeginDestroy(); +} + +void ASoundSpawnActor::SetSound(USoundBase* sound) +{ + UE_LOG(SoundSpawnActorLog, Log, TEXT("playing sound...")); + + UGameplayStatics::PlaySound2D( + this, // world context + sound, + 1.0, // VolumeMultiplier + 1.0, // PitchMultiplier + 0.0 // StartTime + ); +} + diff --git a/Source/ActorSpawningPlugin/Private/SpawnableActor.cpp b/Source/ActorSpawningPlugin/Private/SpawnableActor.cpp index ca38bd6..2a45242 100644 --- a/Source/ActorSpawningPlugin/Private/SpawnableActor.cpp +++ b/Source/ActorSpawningPlugin/Private/SpawnableActor.cpp @@ -11,30 +11,6 @@ ASpawnableActor::ASpawnableActor() // 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; - - /* - // Attach a mesh to the object. - static ConstructorHelpers::FObjectFinder BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); - - BaseMesh = BaseMeshAsset.Object; - - BaseMeshComponent = CreateDefaultSubobject(TEXT("BaseMesh")); - BaseMeshComponent->SetupAttachment(RootComponent); - - BaseMeshComponent->SetStaticMesh(BaseMesh); - - BaseMeshComponent->SetSimulatePhysics(true); - */ - - // NB: Only valid to call in the CTOR. - //BaseMeshComponent = CreateDefaultSubobject(TEXT("BaseMesh")); - //BaseMeshComponent->SetupAttachment(RootComponent); - - // Attach a mesh to the object to try to turn on physics. - // Maybe not necessary... - //static ConstructorHelpers::FObjectFinder BaseMeshAsset(TEXT("StaticMesh'/Game/StarterContent/Props/MaterialSphere.MaterialSphere'")); - //BaseMeshComponent->SetStaticMesh(BaseMeshAsset.Object); - //BaseMeshComponent->SetSimulatePhysics(true); } ASpawnableActor::~ASpawnableActor() @@ -56,31 +32,6 @@ 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); -}*/ - -// Works!! -/*void ASpawnableActor::SetMesh(UStaticMesh* staticMesh) -{ - BaseMesh = staticMesh; - BaseMeshComponent->SetStaticMesh(BaseMesh); - BaseMeshComponent->SetSimulatePhysics(true); - BaseMeshComponent->UpdateCollisionFromStaticMesh(); -}*/ - void ASpawnableActor::StartDestroyTimer(float seconds) { GetWorldTimerManager().SetTimer(DestroyTimer, this, &ASpawnableActor::InvokeDestroy, seconds); diff --git a/Source/ActorSpawningPlugin/Public/SoundSpawnActor.h b/Source/ActorSpawningPlugin/Public/SoundSpawnActor.h new file mode 100644 index 0000000..154c242 --- /dev/null +++ b/Source/ActorSpawningPlugin/Public/SoundSpawnActor.h @@ -0,0 +1,33 @@ +#pragma once + +// Unreal Core +#include "CoreMinimal.h" +#include "UObject/NoExportTypes.h" +#include "Engine/EngineTypes.h" + +// EventCenterPlugin +#include "SoundSpawnActor.generated.h" + +DECLARE_LOG_CATEGORY_EXTERN(SoundSpawnActorLog, Log, All); + +UCLASS() +class ACTORSPAWNINGPLUGIN_API ASoundSpawnActor : public AActor +{ + GENERATED_BODY() + +public: + + ASoundSpawnActor(); + ~ASoundSpawnActor(); + + /** Set the sound. */ + UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor") + void SetSound(USoundBase* sound); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + virtual void BeginDestroy() override; +}; + diff --git a/Source/ActorSpawningPlugin/Public/SpawnableActor.h b/Source/ActorSpawningPlugin/Public/SpawnableActor.h index 6cfcf41..66ed040 100644 --- a/Source/ActorSpawningPlugin/Public/SpawnableActor.h +++ b/Source/ActorSpawningPlugin/Public/SpawnableActor.h @@ -21,21 +21,6 @@ public: ASpawnableActor(); ~ASpawnableActor(); - /** Mesh to use */ - //UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawnableActor", meta = (AllowPrivateAccess = "true")) - //class UStaticMeshComponent* BaseMeshComponent; - - /** Mesh to use */ - //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); @@ -55,3 +40,4 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; }; +