Make Games in UE5 with C++: Background Music

Why was this lesson in Blueprint instead of C++? This was kind of frustrating because while it taught us how to create a looping background song, it did so in Unreal’s BP Event graph, which I am not interested in at the moment. Why wouldn’t we do this in C++? I would assume it would be done in code with GameplayStatics and Soundbase like we used for the last lesson, so why not do it there?

Also, what about engine sounds? I tried adding my own using the last lesson as a guide and I have them playing when I move but it sounds like it is playing multiple times on top of each other instead of the one time when I hold the button down to move. I also set an idle engine sound but it doesn’t play unless I click left or right.

Interestingly, these engine sounds also cut the music out. it goes away when they play.

Hi Robert,
Some things are best done using blueprint. Music, input and UI based features generally are done this way because, honestly, way less code. To do this in C++ you have the call, you have to expose a UPROPERTY to blueprint to get the audio and you have to validate and then play. Blueprint you just assign to the node. It isn’t difficult, just easier with BP. I would even go as far as saying blueprint is the correct way to do this regarding music. It gives the designers rather than developers control of when and how music is played because if you want to control volume and so on, as often you do, you have to expose all the properties necessary to do this. I hope this makes sense.

As for sound, there’s a component you can add to a class for that and it will play in a loop. If you spawn the sound in BeginPlay however, it should only play once.

Try this link if you want to expore yourself. if you want to start and stop audio, make sure you capture a reference.

UGameplayStatics::SpawnSound2D | Unreal Engine 5.0 Documentation | Epic Developer Community

interesting. I find myself feeling like Unreal is just so…bloated. I loved the spartan-ness of Raylib, just you and the code. I don’t like interacting with Blueprints but if that is the way it is done…when in Rome I guess.

I checked out that link and I find the documentation…lacking. It seems to just be the constructor for the object/class. No notes on what those variables are or what they do, no notes on how to use it or call it properly, no examples, nothing. I am finding that C++ documentation with unreal is extremely lacking, with people pushing Blueprints and tons of videos on Blueprints. I ended up buying Ulibarri’s C++ book online just for any kind of reference, as even if it is for UE4, at least having documentation I can reference is helpful. I did thumb through it and didn’t see anything about audio though…but in fairness I haven’t read it yet.

The method above isn’t a constructor. It’s a static method. The gameplay statics class has a lot of decent static calls do do things quickly in it but honestly, you could write a course on audio in unreal with topics such as meta sounds and audio queues. In fact, looking through the UGameplayStatics, you could argue the methods are factory methods for generating objects, varying the parameters to get what you need.

Documentation is lacking, yes, which is why there are so many courses.

As for C++, using for audio, I can’t even find any projects I have where I used C++ for music going back over 6 years. I know it can be done.
If you want the music to restart during levels, you either use the game mode or the level blueprint (ideal if you want different music for each level)

If you want the audio to play continuously between levels, you use the GameInstance (not really covered in this course - more advanced really, think of it like a class for holding global data like high score and settings to be used elsewhere)

So if you really want to learn how to do it, you’d get a USoundBase* for the sound file reference (Think UPROPERTY and assign your audio file) and use a UGameplayStatics::SpawnSound2D to play, and capture the return value so you can control it or change properties. Alternatively, in the class you wish to play sound, you create a UAudioComponent and you can use that to play the sound.

SpawnSound is good because you can let it play and it will finish and destroy. A UAudioComponent requires the component to persist while the audio plays. This can be added to any class using CreateDefaultSubobject and then attach to the root using SetupAttachment. This is covered in the course.

Playing audio in C++ is covered in the Crusty Pirates section of the course and music, while a little different, shares the same principles.

I wanted to take the time to write a more complete description but have been AFK for a most of the day and the phone is not good for this.

Still, playing Audio is little more involved but the absolute simplest form from that link I shared is:

in the .h

  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  class USoundBase* CollectionSound;

in the cpp method call

  if (CollectionSound)
  {
    UGameplayStatics::SpawnSound2D(GetWorld(), CollectionSound);
  }

Privacy & Terms