How to use SetSound()

I understand in need two parameters according to the documentation

void UAudioComponent::SetSound( USoundBase* NewSound )

However what is USoundBase* meant to be? At the moment i have:

StartMusic->SetSound(USoundWave* Sound);

Where sound is defined like this:

static ConstructorHelpers::FObjectFinder<UAudioComponent> Sound(TEXT("SoundWave'/Game/Audio/StartMusic.StartMusic'"));

USoundBase is a single base class that represents a sound that can be played, which is inherited from by USoundWave and USoundCue. So if you already have a pointer to a sound wave then you can pass that to the SetSound function no problem.

EDIT: Your last bit of code should be:

static ConstructorHelpers::FObjectFinder<USoundWave> Sound(TEXT("/Game/Audio/StartMusic.StartMusic"));

Thankyou for the really quick answer :D, what would a pointer to a sound wave look like? And does that mean i should change this in my

  • .h

    TSubobjectPtr StartMusic;
    to

    TSubobjectPtr StartMusic;

? Thankyou :slight_smile:

No your StartMusic is fine as an AudioComponent, you can think of a USoundBase/Wave/Cue as the original data for a sound and an AudioComponent is a single instance of that sound being played. So using the static constructor helper you’ve set up you can get a pointer to the sound wave from Sound->Object;.

Most of time you would probably store this pointer in your class though, that way you can keep hold of it and use it outside of the constructor. You can either set it on your AudioComponent or use UGameplayStatics::PlaySoundAttached to start it off and create a new audio component at the same time. You can also use UGameplayStatics::PlaySoundAtLocation to start off a sound that you never need to update again (not advised for anything long or looping but good for short effects etc.).

Thankyou :slight_smile:

I’m not sure what is going on here. In one project this works and in another it doesn’t.

.h file

	/** engine sound */
	UPROPERTY(Category = Effects, EditDefaultsOnly)
		USoundCue* EngineSound;

	/** audio component for engine sounds */
	UPROPERTY(Category = Effects, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
		UAudioComponent* EngineAC;

.cpp file

	EngineAC = ObjectInitializer.CreateDefaultSubobject<UAudioComponent>(this, TEXT("EngineAudio"));
	EngineAC->bAutoActivate = true;
	EngineAC->AttachParent = RootComponent;

	static ConstructorHelpers::FObjectFinder<USoundCue> EngineLoopSound(TEXT("/Game/Sounds/EngineModel_Loop_Cue"));
	EngineSound = EngineLoopSound.Object;
...
void ASixDOFPawn::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	//Set the engine sound
	if (EngineAC)
	{
		EngineAC->SetSound(EngineSound);  //ERROR HERE
		EngineAC->Stop();
	}
}

This works fine in a different project but in this project I get the following error (we are running 4.7.2).

1>D:\Unreal Projects\SixDOF\Source\SixDOF\SixDOFPawn.cpp(108): error C2664: 'void UAudioComponent::SetSound(USoundBase *)' : cannot convert argument 1 from 'USoundCue *' to 'USoundBase *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Any ideas?

You need to include SoundCue.h in SixDOFPawn.cpp I would imagine. The error suggests that it is only aware of a forward declaration for USoundCue, it doesn’t know that it inherits from USoundBase.

Weird, I never got an e-mail that you replied. Anyhow, yes, this was the problem, I was using the Flying C++ project as a template and I needed to include Engine.h instead of EngineMinimal.h in the main .h file for the project. At least now I know where to look if I see weirdness like this again. Thank you Matt!