ConstructorHelpers::FObjectFinder crashing in constructor

In my constructor I have this code:

static ConstructorHelpers::FObjectFinder<USoundWave> objFinder(TEXT("SoundWave'/Game/UI/Sounds/Menu/MenuButtonClicked.MenuButtonClicked'"));

When it is executed (“Play” inside the editor) it crashes with:

Fatal error: [File:D:\Build\++UE4+Release-4.17+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 3233] FObjectFinders can't be used outside of constructors to find SoundWave'/Game/UI/Sounds/Menu/MenuButtonClicked.MenuButtonClicked'

which is exactly the above line.

Any ideas? Perhaps I have to have certain macros in the class header or something like that?

I assume you got that string by right clicking the asset and selecting “Copy Reference”?

Try setting your path file string to this:

static ConstructorHelpers::FObjectFinder<USoundWave> objFinder(TEXT("'/Game/UI/Sounds/Menu/MenuButtonClicked'"));

Hoping that does it!

Just checked in my own project, and this is my code:

	static ConstructorHelpers::FObjectFinder<USoundBase> FireAudio(TEXT("/Game/TwinStick/Audio/KKIIDDZZ_00018"));
	FireSound = FireAudio.Object;

and in my .h file :

	UPROPERTY(Category = Audio, EditAnywhere, BlueprintReadWrite)
		class USoundBase* FireSound;

I’m not sure what the difference is between USoundBase and USoundWave, but that might be the problem? :stuck_out_tongue:

I’m sorry, it was a fair while ago I set this up, so I just had to go and check where the sound was used. I currently have this code to play a 2D sound (though the commented out line should create a 3D sound effect ) using FireSound:

	if (FireSound != nullptr)
	{
		UGameplayStatics::PlaySound2D(this, FireSound);	
		//UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
	}

I’m quite sure that I need to either use an object finder (as I did) or assign the variable via blueprint in order for FireSound to make a sound.

You shouldn’t require UPROPERTY() , though I’m not certain on that - UE4 can be funny about its macros sometimes!

Am I being clear enough? Cos I’m not certain that I am =P

Edit: Here’s the include for UGameplayStatistics:

 #include "Kismet/GameplayStatics.h"

No, it was a good idea, I tried different variants

  • SoundWave’/Game/UI/Sounds/Menu/MenuButtonClicked.MenuButtonClicked’
  • SoundWave’/Game/UI/Sounds/Menu/MenuButtonClicked’
  • Game/UI/Sounds/Menu/MenuButtonClicked

I tried to get an UObject instead of a USoundWave, no change.

When I use

UObject* objFinder = StaticLoadObject(USoundWave::StaticClass(), nullptr, TEXT("SoundWave'/Game/UI/Sounds/Menu/MenuButtonClicked.MenuButtonClicked'"));

it seems to work.
At least

USoundWave* pSoundClick = dynamic_cast <USoundWave*> (objFinder);

does not give me a nullptr.

Now I have to find out how to play a SoundWave or - better - how to convert a SoundWave to a FSlateSound to know if I really loaded that asset.

It would just be nice to be able to use the normally used method. Or at least to know why it crashes. Makes me think something is really wrong here with my code. :frowning:

NOW I am really confused.
I thought, if you do it your way, you can just use FireSound, no need for any ObjectFinder.

I want to avoid the URPOPERTY way here, if I have to, I will use it, but I’ll try for a bit.

Did you ever try to just use the FireSound?

Thank you. That should help with the tests.