Adding the file location of a sound to a Sound Base?

If you would look in to the logs (Saved/Logs in project directory) you would get immediate answer ;] ConstructorHelpers functions as name suggests work only in class constructor and this is where you should do this, otherwise it will crash the engine and it should inform you that this should not be used in elsewhere in constructor inside the logs. It needs to be in constructor in order to hard code default asset in to CDO without need for pointed asset to be loaded.

The runtime code errors (at least once that engine is programmed to detect) either prints out errors in log or crash engine (via assetion system Assertion (software development) - Wikipedia) and prints out failed condition, sometimes with full error message sometimes also hinting solution. So every time you have crash, your first action should be checking log, it will save you a lot of time.

How do I attach the address/file location of a sound file directly to a Sound Base?

Right now, I have a declaration of a Sound Base object with a UPROPERTY EditAnywhere tag in my .h file and then the call the sound effect in my .cpp file.

.h snippet:

// The sound effect for failing a teleport action.
UPROPERTY(EditAnywhere, Category = "Ability Settings")
class USoundBase* FailureSound;

.cpp snippet:

// Plays a buzzer at UI level when called.
void UShooterAbility::PlayFailureSound()
{
	if (FailureSound != NULL)
	{
		UGameplayStatics::PlaySound2D(this, FailureSound, 1.f, 1.f, 0.f, nullptr, GetOwner());
	}
}

It works! – after I launch the game and add the sound effect to the component file. When I stop the game, the file is no longer associated and I have to re-assign it every time.

How do I hard code this location? I’m not seeing anything in the documentation.

I found a post that recommended I add an additional Sound Cue object and link it:

// Called when the game starts
void UShooterAbility::BeginPlay()
{
	Super::BeginPlay();
	static ConstructorHelpers::FObjectFinder<USoundCue> MySoundFile(TEXT("/ShooterGame/Content/Sounds/InterfaceAudio/Stereo/MenuExitStereo_Cue"));
	FailureSound = MySoundFile.Object;
}

This crashes the editor on execution.

Thoughts?

Awesome, thanks! But that still didn’t fix my file directory issue.

Wound up playing around with it for another couple of hours. Here’s the result:

  1. Moved the assignment code to the constructor, per 's comment.

  2. Used the filepath from the mouse-over tooltip in Unreal instead of the Windows Explorer filepath.

    // Sets default values for this component’s properties
    UShooterTeleportAbility::UShooterTeleportAbility()
    {
    // Set this component to be initialized when the game starts, and to be ticked every frame.
    PrimaryComponentTick.bCanEverTick = true;

     static ConstructorHelpers::FObjectFinder<USoundCue> AbilitySoundFile(TEXT("/Game/Sounds/InterfaceAudio/Stereo/MenuExitStereo_Cue"));
     AbilitySound= AbilitySoundFile.Object;
    

    }

Working great!