BaseAudioTrigger.cpp

From Kieran McEvaddy, 2 Months ago, written in C++, viewed 93 times. This paste is a reply to BaseAudioTrigger.h from Kieran McEvaddy - go back
URL http://codebin.org/view/8ea7f428/diff Embed
Viewing differences between BaseAudioTrigger.h and BaseAudioTrigger.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "BaseAudioTrigger.generated.h"
#include "Components/AudioComponent.h"
#include "Components/SphereComponent.h"
#include "ConstructorHelpers.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.
h"

UCLASS()
class PLATFORMER3D_API ABaseAudioTrigger : public AActor
// Sets default values
ABaseAudioTrigger::ABaseAudioTrigger()
{
        GENERATED_BODY()
         // 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;

        /*Sphere Trigger*/
        SphereTrigger = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Trigger"));
        if( SphereTrigger )
        {
                SphereTrigger->InitSphereRadius(50.0f);
                SphereTrigger->SetCollisionProfileName(FName("STCol"));
                SphereTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABaseAudioTrigger::OnOverlapBegin);
                SphereTrigger->SetMobility(EComponentMobility::Static);
        
public:        
        // Sets default values for this actor's properties
        ABaseAudioTrigger();

protected:
         //  We don't need BeginPlay()
         //         Called when the game starts or when spawned
         //         virtual void BeginPlay() override;
 
        virtual void PostInitializeComponents() override;
 
public:        
         // We don't need Tick()
        // Called every frame
        // virtual void Tick(float DeltaTime) override;
 
private:
 
        UPROPERTY(EditAnywhere, Category 
                RootComponent "Collision")
        class USphereComponent* SphereTrigger;

        UPROPERTY(EditDefaultsOnly, Category = "Audio")
        class USoundCue* CollectedCue;

        UPROPERTY(EditDefaultsOnly, Category = "Audio")
        class UAudioComponent* CollectableAudioComponent;

        UPROPERTY(EditAnywhere, Category = "Particles")
        class UParticleSystem* ParticleEffect;

        bool bFinished;

        UFUNCTION()
        void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
                bool bFromSweep, const FHitResult& SweepResult);
SphereTrigger;
        }
        
        UFUNCTION()
        virtual 
/*Audio*/        
        //The cue to play
        CollectedCue = ConstructorHelpers::FObjectFinder<USoundCue>
        (TEXT("SoundCue'/Game/Sounds/Test/Rock_Cue.Rock_Cue'")).Object;
        
        //The container of the cue's logic
        CollectableAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CollectableAudioComp"));
        if( CollectableAudioComponent )
        {
                CollectableAudioComponent->bAutoActivate = false;
                CollectableAudioComponent->SetupAttachment(RootComponent);
                CollectableAudioComponent->OnAudioFinished.AddDynamic(this, &ABaseAudioTrigger::OnAudioFinished);
        }


}

void OnAudioFinished();

};
ABaseAudioTrigger::PostInitializeComponents()
{
        Super::PostInitializeComponents();

        CollectableAudioComponent->SetSound(CollectedCue);
}

void ABaseAudioTrigger::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
        if( ( OtherActor != nullptr ) && ( OtherActor != this ) && ( OtherComp != nullptr ) )
        {
                if( !bFinished )
                {
                        bFinished = true;
                        CollectableAudioComponent->Play();
                        
                        UGameplayStatics::SpawnEmitterAttached(
                                ParticleEffect, 
                                RootComponent        
                        );
                }
        }
}

void ABaseAudioTrigger::OnAudioFinished()
{
        Destroy();
}

Replies to BaseAudioTrigger.cpp rss

Title Name Language When
Re: BaseAudioTrigger.cpp Kieran McEvaddy cpp 2 Months ago.

Reply to "BaseAudioTrigger.cpp"

Here you can reply to the paste above