C++ DoOnce

Hey all, I have a decent understanding of the Blueprint method and want to try doing some C++ stuff. I’ve made a simple script that plays a sound as the character steps onto a collision box want it to only play once, similar to the “DoOnce” node.

Everything works and I currently have this in the .cpp file:


void ACollision_Box_2::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (CollapseAudioComponent && Collapse_Cue)
{
CollapseAudioComponent->Play(0.f);
}
}

What and where would I introduce the DoOnce equivalent into this?

Thanks



// .h
bool bHasPlayedAudio;

// .cpp
if (bHasPlayedAudio == false)
{
     if (CollapseAudioComponent && Collapse_Cue)
     {
          CollapseAudioComponent->Play(0.f);

          // Set the bool to true, so next time the if statement will fail.
          // Meaning, we can only play the sound once.
          bHasPlayedAudio = true;
     }
}


Thanks STRiFE, works perfectly!