PlaySoundAtLocation plays sound after some delay

I’m developing the Pong game to learn Unreal Engine using C++.

I use this code in the class’ constructor to load the sound:

static ConstructorHelpers::FObjectFinder<USoundWave> HitSoundAsset(TEXT("/Game/Effects/pong-paddle.pong-paddle"));
	if (HitSoundAsset.Succeeded())
		HitSound = HitSoundAsset.Object;

And in OnHit event this code:

void APaddle::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr))
	{
		if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Paddle has been hit by: %s"), *OtherActor->GetName()));

		if (ABall* Ball = Cast<ABall>(OtherActor))
		{
			PlayHitSound();
		}
	}
}

And finally:

void APaddle::PlayHitSound()
{
	if (HitSound != nullptr)
	{
		UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation());
	}
}

But it sounds after the ball has bounced.

Is there any way to make it sounds immediately?

Is the sound file right? I mean, the sound starts at 0 and hasn’t any silent.

1 Like

Good question. The sound starts at 0 and it hasn’t any silent.

pong-paddle-wave

I have tried the same code in another game (developed using only C++, like this one) and the same thing happens. But here, I play the same sound more than once, and the second, third times play without any delay.

Do I need to preload (or anything similar) the sound before play it?

Yes, you need to either mark the cue’s wave files as preloaded, or preload the cue explicitly from code.

Thanks. And, how can I do that?

I’m not at the editor right now, so I can’t take a screen shot, but I seem to recall seeing a “preload” checkbox somewhere in the wave properties in the cue interface when I last looked at this.

And I looked in the docs, which SURELY would mention this, and … nothing? That’s pretty crazy!

I think the word to search for is “prime” perhaps?
[bPrimeOnLoad] USoundCue | Unreal Engine Documentation