mirror of
https://github.com/storytold/ActorSpawningPlugin.git
synced 2026-10-18 07:33:10 +00:00
Sound actor destroys itself after play
This commit is contained in:
@@ -1,19 +1,28 @@
|
|||||||
#include "SoundSpawnActor.h"
|
#include "SoundSpawnActor.h"
|
||||||
|
|
||||||
#include "UObject/Object.h"
|
#include "Components/AudioComponent.h"
|
||||||
#include "Kismet/GameplayStatics.h"
|
|
||||||
#include "Components/BillboardComponent.h"
|
#include "Components/BillboardComponent.h"
|
||||||
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
#include "UObject/Object.h"
|
||||||
|
|
||||||
DEFINE_LOG_CATEGORY(SoundSpawnActorLog);
|
DEFINE_LOG_CATEGORY(SoundSpawnActorLog);
|
||||||
|
|
||||||
ASoundSpawnActor::ASoundSpawnActor(const FObjectInitializer& ObjectInitializer) :
|
ASoundSpawnActor::ASoundSpawnActor(const FObjectInitializer& ObjectInitializer) :
|
||||||
Super(ObjectInitializer),
|
Super(ObjectInitializer),
|
||||||
BillboardComponent(nullptr)
|
BillboardComponent(nullptr),
|
||||||
|
HasSoundStarted(false),
|
||||||
|
DoDestroyAfterSoundPlays(false)
|
||||||
{
|
{
|
||||||
UE_LOG(SoundSpawnActorLog, Log, TEXT("CTOR()"));
|
UE_LOG(SoundSpawnActorLog, Log, TEXT("CTOR()"));
|
||||||
|
|
||||||
BillboardComponent = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
|
BillboardComponent = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
|
||||||
BillboardComponent->SetupAttachment(RootComponent);
|
BillboardComponent->SetupAttachment(RootComponent);
|
||||||
|
|
||||||
|
AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Audio"));
|
||||||
|
AudioComponent->SetupAttachment(BillboardComponent);
|
||||||
|
|
||||||
|
// We use tick to kill the actor when the sound is done playing
|
||||||
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASoundSpawnActor::~ASoundSpawnActor()
|
ASoundSpawnActor::~ASoundSpawnActor()
|
||||||
@@ -31,6 +40,29 @@ void ASoundSpawnActor::BeginDestroy()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ASoundSpawnActor::SetSound(USoundBase* sound)
|
void ASoundSpawnActor::SetSound(USoundBase* sound)
|
||||||
|
{
|
||||||
|
AudioComponent->SetSound(sound);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASoundSpawnActor::SetSprite(UTexture2D* sprite)
|
||||||
|
{
|
||||||
|
BillboardComponent->SetSprite(sprite);
|
||||||
|
BillboardComponent->RegisterComponent();
|
||||||
|
BillboardComponent->bHiddenInGame = false; // NB: This defaults to true! :(
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASoundSpawnActor::PlaySoundWithSprite(USoundBase* sound, UTexture2D* sprite, bool DestroyAfterPlay)
|
||||||
|
{
|
||||||
|
SetSprite(sprite);
|
||||||
|
SetSound(sound);
|
||||||
|
|
||||||
|
AudioComponent->Play(0.0);
|
||||||
|
|
||||||
|
HasSoundStarted = true;
|
||||||
|
DoDestroyAfterSoundPlays = DestroyAfterPlay;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASoundSpawnActor::PlaySoundImmediate(USoundBase* sound)
|
||||||
{
|
{
|
||||||
UE_LOG(SoundSpawnActorLog, Log, TEXT("playing sound..."));
|
UE_LOG(SoundSpawnActorLog, Log, TEXT("playing sound..."));
|
||||||
|
|
||||||
@@ -43,10 +75,14 @@ void ASoundSpawnActor::SetSound(USoundBase* sound)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASoundSpawnActor::SetSprite(UTexture2D* sprite)
|
void ASoundSpawnActor::Tick(float DeltaTime)
|
||||||
{
|
{
|
||||||
BillboardComponent->SetSprite(sprite);
|
Super::Tick(DeltaTime);
|
||||||
BillboardComponent->RegisterComponent();
|
|
||||||
BillboardComponent->bHiddenInGame = false; // NB: This defaults to true! :(
|
if (HasSoundStarted
|
||||||
|
&& DoDestroyAfterSoundPlays
|
||||||
|
&& !AudioComponent->IsPlaying()) {
|
||||||
|
Destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ ASpawnableSkeletalMeshActor* ASpawningActor::SpawnSkeletalMeshActor()
|
|||||||
return spawnedActor;
|
return spawnedActor;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASoundSpawnActor* ASpawningActor::SpawnSoundActor(USoundBase* sound)
|
ASoundSpawnActor* ASpawningActor::SpawnSoundActor()
|
||||||
{
|
{
|
||||||
FVector playerLocation = LookupPlayerLocation();
|
FVector playerLocation = LookupPlayerLocation();
|
||||||
|
|
||||||
@@ -127,15 +127,6 @@ ASoundSpawnActor* ASpawningActor::SpawnSoundActor(USoundBase* sound)
|
|||||||
actorSpawnParams
|
actorSpawnParams
|
||||||
);
|
);
|
||||||
|
|
||||||
actor->SetSound(sound);
|
|
||||||
|
|
||||||
return actor;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASoundSpawnActor* ASpawningActor::SpawnSoundActorWithText(USoundBase* sound, const FString& text)
|
|
||||||
{
|
|
||||||
ASoundSpawnActor* actor = SpawnSoundActor(sound);
|
|
||||||
|
|
||||||
return actor;
|
return actor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,11 +20,15 @@ public:
|
|||||||
ASoundSpawnActor(const FObjectInitializer& ObjectInitializer);
|
ASoundSpawnActor(const FObjectInitializer& ObjectInitializer);
|
||||||
~ASoundSpawnActor();
|
~ASoundSpawnActor();
|
||||||
|
|
||||||
/** Access to the optional billboard component */
|
/** Access to the audio component */
|
||||||
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoundSpawnActor")
|
||||||
|
class UAudioComponent* AudioComponent;
|
||||||
|
|
||||||
|
/** Access to the billboard component */
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoundSpawnActor")
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SoundSpawnActor")
|
||||||
class UBillboardComponent* BillboardComponent;
|
class UBillboardComponent* BillboardComponent;
|
||||||
|
|
||||||
/** Set the sound. */
|
/** Set sound to the audio component. */
|
||||||
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
||||||
void SetSound(USoundBase* sound);
|
void SetSound(USoundBase* sound);
|
||||||
|
|
||||||
@@ -32,7 +36,25 @@ public:
|
|||||||
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
||||||
void SetSprite(UTexture2D* sprite);
|
void SetSprite(UTexture2D* sprite);
|
||||||
|
|
||||||
|
/** Play sound with the sprite, then destroy. */
|
||||||
|
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
||||||
|
void PlaySoundWithSprite(USoundBase* sound, UTexture2D* sprite, bool DestroyAfterPlay);
|
||||||
|
|
||||||
|
/** Play a sound. Does not set to the component. */
|
||||||
|
UFUNCTION(BlueprintCallable, Category = "SoundSpawnActor")
|
||||||
|
void PlaySoundImmediate(USoundBase* sound);
|
||||||
|
|
||||||
|
// Called every frame
|
||||||
|
// Used to invoke destroy.
|
||||||
|
virtual void Tick(float DeltaTime) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// Whether sound has started playing at any point.
|
||||||
|
bool HasSoundStarted;
|
||||||
|
|
||||||
|
// Whether to destroy after sound finishes playing the first time.
|
||||||
|
bool DoDestroyAfterSoundPlays;
|
||||||
|
|
||||||
// Called when the game starts or when spawned
|
// Called when the game starts or when spawned
|
||||||
virtual void BeginPlay() override;
|
virtual void BeginPlay() override;
|
||||||
|
|
||||||
|
|||||||
@@ -34,11 +34,7 @@ public:
|
|||||||
|
|
||||||
/** Create sound actor. */
|
/** Create sound actor. */
|
||||||
UFUNCTION(BlueprintCallable, Category = "SpawningActor")
|
UFUNCTION(BlueprintCallable, Category = "SpawningActor")
|
||||||
ASoundSpawnActor* SpawnSoundActor(USoundBase* sound);
|
ASoundSpawnActor* SpawnSoundActor();
|
||||||
|
|
||||||
/** Create sound actor with text. */
|
|
||||||
UFUNCTION(BlueprintCallable, Category = "SpawningActor")
|
|
||||||
ASoundSpawnActor* SpawnSoundActorWithText(USoundBase* sound, const FString& text);
|
|
||||||
|
|
||||||
/** Mesh to use */
|
/** Mesh to use */
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true"))
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "SpawningActor", meta = (AllowPrivateAccess = "true"))
|
||||||
|
|||||||
Reference in New Issue
Block a user