Expose C++ Properties and Functions to Blueprints in UE5 (For Game Audio)
Si prefieres leer una versión en Español, da click en este botón =>
Unreal Engine Version: 5.0.2
Unreal Engine's visual scripting (Blueprints) is a complete and powerful tool for creating audio systems (and everything else!) for our projects. Despite this, there are times when we want to interact and collaborate with our team's programmers, that write native C++ code. We don't have to be expert C++ programmers to achieve this. In this tutorial, I'll give some simple examples of creating C++ properties and functions for audio in Unreal Engine 5 and expose these to blueprints.
We will create an empty third-person project called "MyUE5Project" and a Sound Wave asset within it.
You must have an IDE installed beforehand (Visual Studio for Windows or Xcode for Mac). I use Rider as my preferred IDE, so the look of my interface might be different than yours.
First Steps:
1. Create your UE5 project
Open the Epic Games Launcher and create a new C++ - Third Person Project:
Your project will be created and then compiled….
On the UE5 editor, go to Tools > Open Visual Studio or Tools > Open Xcode.
In our project, we will be working on the "Character" class called "MyUE5ProjectCharacter" in our project.
Inside your IDE go to Games/MyUEProject/Source/MyUEProject and open both "MyUE5ProjectCharacter.cpp" and "MyUE5ProjectCharacter.h".
About .h and .cpp files: Every time you create a class in Unreal Engine, you end up with two files. The .h file or "header" will hold your "Declarations" (Properties/Variables, Constants, and Functions), and the .cpp file will have the "Implementations" or "Definitions" of these declarations.
2. USoundWave Property
Inside "MyUE5ProjectCharacter.h," create a new property of class "USoundWave" and name it "testSound." On top of this declaration, add the UPROPERTY macro with some arguments. This macro will allow us to expose this property to the editor and to blueprints:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Audio") USoundWave* testSound;
3. Custom Function: PlayTestSound()
Inside "MyUE5ProjectCharacter.h”, declare a custom function called PlayTestSound(). On top of this declaration, you will add the UFUNCTION macro with some arguments that will allow us to call this function from blueprints:
UFUNCTION(BlueprintCallable, Category="Audio") void PlayTestSound();
Inside "MyUE5ProjectCharacter.cpp” define PlayTestSound(), calling it from the class where it was declared using AMyUE5ProjectCharacter::PlayTestSound(). Inside the “{}” brackets call UGameplayStatics::PlaySound2D() with the arguments: “this” and your USoundWave property “testSound”.
void AMyUE5ProjectCharacter::PlayTestSound() { UGameplayStatics::PlaySound2D(this, testSound); }
this: This USoundWave will be placed to the Object where this script is attached.
testSound: Is the USoundWave sound asset that this function will play.
4. Compile
Save your modified code. Go back to your UE project and compile.
Blueprints:
1. Add an audio file
In the editor, click on the Content Drawer and then on Import to add a .Wav sound file. Alternatively, you can drag and drop a file directly into the editor.
IMPORTANT: Currently UE5 just supports 16 bit audio files. If you prefer, you can use this audio file:
2. Third Person Character Blueprint
On your Content Drawer, go to Content > Third Person > Blueprints and double click on “BP_ThirdPersonCharacter”
"BP_ThirdPersonCharacter" is a Blueprint class inherited from the C++ class we worked on before: "MyUE5ProjectCharacter". Commonly, C++ classes are the "parents" of Blueprint classes.
After you open this Blueprint, you might see this message:
"NOTE: This is a data only blueprint, so only the default values are shown. It does not have any script or variables".
Click on Open Full Blueprint Editor
Select the Event Graph, click on the cog-shaped button, and then click "Show Inherited Variables." Now, you should see your "Test Sound" property exposed.
Select "Test Sound" and go to the Details panel on the far right of your editor. Assign your sound asset to "Test Sound."
3. Trigger your sound with PlayTestSound()
Inside the Graph Editor, right-click and search for “Keyboard Events E.” Left-click to add this node.
Inside the Graph Editor, right-click and search for your C++ custom function, “Play Test Sound.” Left click to add this node.
Inside the Graph Editor, right click and search for “Print String”. Left click to add this node.
Connect all the nodes and type a custom text on In String. You might want to click the down arrow and extend the duration time of this message. Your full graph should look like this:
Keyboard Events E will trigger our custom C++ function PlayTestSound() and then print a message to the screen.
Gameplay:
Go back to the main editor and click on the Play button. While in the game, press the “E” key. You should be able to hear your sound now!
IMPORTANT: If you press the “E” key more than once more instances of your audio will start playing on top of the previous one. We will learn how to deal with this in future tutorials.