How do I make an Audio Component play when my character moves?

I’m decent with C++, but as I try to figure out programming in UE4 I find myself not knowing what the hell I’m doing.

I’ve tried to read a lot of the documentation now, and through a tutorial I figured out how to make FPS movement (without understanding why it works). I’m now reading about UAudioComponent and am trying to figure out how I can declare one of those in my player character class definition, then in the source file counstructor attach it to an Asset I’ve imported to the project in the Editor, and finally implement it into the MoveForward and MoveRight functions in a way that whenever those functions are called, the asset audio loops, and whenever the character is falling or not moving, the sound stops playing.

I can do the part about setting the conditions for checking whether my character is moving and not falling or not, but otherwise I have no idea what I’m doing. So, could someone show me an example of how to declare, initialize and use a UAudioComponent to conditionally play sound?

There are a variety of ways you can do this.

You can make a native audio component child subobject of your class by adding a TSubObjectPtr MyAudioComponent and init it in the classes constructor by doing PCIP.CreateDefaultSubobject(this, TEXT(“AudioComp”));
In the blueprint instance of that actor you can set the SoundCue to use and other properties. Then in your MoveForward and MoveRight functions you can do MyAudioComponent->Play(). You can call IsPlaying() on it to see if tis already executed and of course all ->Stop() to stop.

An alternative way is you just expose the SoundCue reference to be filled in in the blueprint reference doing something like: UPROPERTY(EditAnywhere, Category = Sound) USoundCue* MoveSound;
Then you can trigger it a bunch of different ways, but one utility function that will make a new audiocomponent that plays the cue is UGameplayStatics::PlaySoundAttached. It will return an AudioComponent which you can cache and modify.

Well, see, while I understand how you say it can be done, I really don’t know the UE4 C++ API well enough to pull it off without some more detailed guidelines. I understand the theory of how to do it, I just don’t have the knowledge to apply it yet.

For example, I have no idea what the TSubObjectPtr template does, and the arguments to the CreateDefaultSubobject member of PCIP seems completely random to me. Attaching a sound to a SoundCue is beyond me as well, because I don’t even know what a SoundCue is.

In short, I’m pretty clueless about this all. I’d be very happy if you’d be willing to give me some example/pseudo-code and a brief explanation of what it’s supposed to do, so I can fully understand what you were telling me in your answer right now.

You should download the shooter game sample project and dive through it. There are examples of playing sounds using Audio components in there. The code I provided in my comment is all that is needed to play a sound, but if you can’t understand that than you should focus on a walk-through the sample projects or go through the C++ tutorial video series.

I can walk through a sample project at least, because I already know the C++ tutorial series is really bad. It’s only a bunch “do this, and then this, and then you’re done!” without ever explaining anything. I need to learn how the API works, not just see someone use it without explaining what they’re doing.

I’ll check out the shooter game, though. Hopefully that’ll answer some questions I have.