How to reference a sound cue/wave?

Hi,

I’m working on a class where I would like to add some SoundCue in it. I would like to add some default sound. Unfortunately I didn’t found a way to use an object finder for this particular case.

static ConstructorHelpers::FObjectFinder sndBaseFire(TEXT( ... ));

The code above doesn’t compile, I get an error about an “undefined type USoundWave”. Same thing if I try with USoundCue.

So how do you load a sound ? I was also wondering how to make this sound the default one and still allowing the user to override it in case he makes a blueprint from this class. I believe I need to use a TSubobjectPtr right ?

Generally the way we set up our class asset default properties would look something like so:

UEditorEngine::UEditorEngine(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	// Structure to hold one-time initialization
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinder BadTexture;
		ConstructorHelpers::FObjectFinder EditorCubeMesh;
		FConstructorStatics()
			: BadTexture(TEXT("Texture2D'/Engine/EditorResources/Bad.Bad'"))
			, EditorCubeMesh(TEXT("StaticMesh'/Engine/EditorMeshes/EditorCube.EditorCube'"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;

	Bad = ConstructorStatics.BadTexture.Object;
	EditorCube = ConstructorStatics.EditorCubeMesh.Object;

Where Bad and EditorCube are UPROPERTY members of UEditorEngine

To make it overridable from a blueprint you’ll want to define a UPROPERTY that is EditDefaultsOnly and then assign your default value that you get from the FObjectFinder to it in the constructor.

As for why USoundWave is an undefined type I can only guess that the necessary includes are not in place. For USoundWave and USoundCue you should #include “SoundDefinitions.h”

I tried to add “SoundCue.h” but “SoundDefinitions.h” was actually what I needed ! UPROPERTY with EditDefaultsOnly was already what I was using, but I wasn’t sure it was the best method.

Thanks a lot ! :slight_smile:

Dear Fabrice,

This is where blueprints become very handy!

~Whatever class you need to call the sound cue in, in the .h file, put this there:

 /** Your Sound Comment */
    UPROPERTY(EditDefaultsOnly, Category=Sounds)
    USoundCue* YourSound;

Make sure this is in the public section or near very top of class definition

~Then compile

~then go into editor and blueprint your class that you just ededited

~then drag your soundcue into the blueprint’s default properties

~you can reference the blueprint using your findobject method or by using a UPROPERTY like above and using (UClass*)

I usually inter-reference my class instances if I need to set a lot of properties like this, so that one Blueprinted master actor is the one where I put all the sound cues or other asset information, and then reference that master actor any time I need the assets

Enjoy!

Rama

Thanks for the input, however this is not what I’m looking for. I want the sound to be specified in C++ first, and let the user later the possibility to override it with a inherited blueprint of the class.

I already know the way that you describe, but it doesn’t work with my current workflow for some specific reasons.

“I want the sound to be specified in C++ first, and let the user later the possibility to override it with a inherited blueprint of the class.”

I dont understand how that is different than what I am suggesting.

You are indeed specifying in c++ first

and then you refer to YourSound in your code wherever you want to play the sound (make sure to check if it is null or not)

" and let the user later the possibility to override it with a inherited blueprint of the class"

this is exactly what blueprints do! :slight_smile:

in your code it is YourSound and you play it wherever

the user can make YourSound be whatever they want through drag and drop use of awesome UE4 editor and blueprints

What are you trying to do, if it is different than the way I am interpreting you?

I dont understand how that is different than what I am suggesting.

What your are suggesting is creating the variable in C++, and then making its content in blueprint. What I would like is to specify the sound file in C++ first (with a FObjectFinder for example) and not only in the blueprint.

By default my class is not planned to be used as a blueprint, it’s just a possibility that I let to the user. Therefore I need to specify in C++ the path to the sounds.

Hi Fabrice – I am doing this now; I declare this

USoundCue* m_ClickSound;

in my header file and

static ConstructorHelpers::FObjectFinder soundCueLoader(TEXT("SoundCue'/Game/Click1_Cue.Click1_Cue'"));
m_ClickSound = soundCueLoader.Object;

in my constructor.

Unfortunately, there’s no USoundCue::Play(), but it looks like you can pass the USoundCue in to AActor::PlaySoundOnActor(), AActor::PlaySoundAtLocation(), APlayerController::ClientPlaySound(), and so on.

posted this as a separate answer due to length and amount of code

I’m not sure what you mean by

“By default my class is not planned to be used as a blueprint”

The terminology is a bit vague at the moment in c++

there are several different uses of the word blueprint

I will explain the 3 that I know of


1. blueprint, as in blueprint graphs: this is where you see all the nodes connecting and people doing game logic using blueprints instead of c++

2. Anim blueprint, a special kind of blueprint graph

3. Blueprint, as a special cases of a base c++ class: These are classes, you use them in c++ directly, just like you would your normal c++ classes. These have nothing to do with the nodes or graphs and you are actually using C++ not blueprint node logic


Example of using a Blueprinted class

You use blueprint classes the same way you use regular base classes, here’s an example of spawning a blueprinted class, with specific values set in the editor, but all actual game logic done in c++

//get the blueprint of the base class. The base class of CreatureClass is AEvolverWarrior, but CreatureClass is a blueprinted version with special values set
static ConstructorHelpers::FObjectFinder EvolverOb(TEXT("Blueprint'/Game/Blueprints/EvolverWarriorBP.EvolverWarriorBP'"));
    if (EvolverOb.Object != NULL)
    {
       CreatureClass = (UClass*)EvolverOb.Object->GeneratedClass;
    }


//spawn the creature from the blueprinted class
//spawn creature
    AEvolverWarrior* NewCreature = 
       GetWorld()->SpawnActor(CreatureClass, TestLocation, GetControlRotation(),SpawnInfo );

//more c++ game logic

so you see I am not using the base class AEvolverWarrior, I am using the blueprinted version of that base class.

but all game logic is done entirely in the c++

So realize that blueprints is a bit of a vague term at the moment,

and one definition of blueprints is special version of your base c++ class designed to make setting properties and making different variations using one base c++ class very easy.

It really is a wonderful system and has nothing to do with blueprint nodes :slight_smile:

Rama

Thanks you for taking the time to answer my question. :slight_smile:

 I am using the blueprinted version of that base class.

And it’s exactly not what I want.

Sorry but I don’t think I can’t be more clear than my previous answers : I want to load a soundcue in C++ and reference it to my variable. That’s all.

in the interest of debugging why you are getting USoundCue as not compiling

what happens if you try my code in the .h ?

Does it compile?

We need to figure out if it is in the objectfinder or in your entire project that USoundCue is not being found