Toggling Sound

So i am making a Build-able campfire that you can turn on, and off. Like in Rust, so i can get the Sound to play when the Fire Starts but, When i turn the fire off it will not stop playing, I’m pretty Lost when it comes to toggling a Sound on and off. Thanks!

Could you provide screenshots of your blueprints code that toggles the sound?

Here ya go this is what i got

Try this setup. This works in a networking game so if you toggle sound all clients hear it.

  1. Within your Character/Pawn class, catch the OnActorBeginOverlap and OnActorEndOverlap event. You want to either pass these events to the PlayerController by calling events/functions on the PlayerController or do what I did below and dispatch an event on the Character blueprint and let the PlayerController listen for those events.

  1. Within your PlayerController, bind to your Character’s/Pawn’s event dispatcher or create the Begin/End overlap functions that your Character/Pawn calls. Implement the code that figures out if you are overlapping an actor and optionally, on key press, check to make sure you are overlapping an actor.

In my setup you must be overlapping the campfire followed by pressing E to toggle the campfire sound. If the campfire is overlapped and E is pressed we replicate a PlayerController event to itself, but on the server instance of the PlayerController. During that event execution we are now executing the code on the server so later we can push events to all clients, not just our client.

NOTE: Do not forget to configure your PlayerController to Replicate in your PlayerController’s Class Settings.

  1. Now in your Campfire blueprint you need to create a replicated variable called IsFireOn and set the replication type to RepNotify (we will fill in the RepNotify function next). Don’t forget to create the ToggleSound event within the Campfire actor’s event graph like I have below.

Don’t forget to configure your Campfire blueprint to Replicate under the Class Settings too, just like your PlayerController.

  1. Lastly, you need to create a new Audio component within your Campfire blueprint. Set the Audio source to what you desire and uncheck the “Auto Activate” checkbox if you do not want the fire sound to play when this campfire is first spawned into the world. I.e. the campfire will be “put out”. Don’t forget to fill in the RepNotify function that is called each time your IsFireOn boolean is replicated to each client. Each time you change this IsFireOn variable on the server it will call the RepNotify function on EACH client.

We have to use a RepNotify function to toggle the sounds because to my knowledge, you can’t play a sound on the server and have it automatically replicate across the network.

I didn’t see your image until after I posted my answer. Read through my answer, it might give you valuable information on how to achieve this.

SNDevs,

If you’d like to keep your existing campfire Blueprint structure (https://answers.unrealengine.com/storage/temp/118720-campfirebp.png) here’s what you can do to fix this.

First, the problem: You are calling Play Sound at Location for both turning the fire on and off. Play Sound at Location does exactly that, it plays a sound, it does not toggle the existing sound. What you’re looking for is a way to turn the sound on/off.

The solution: Within your Campfire blueprint create a new Audio component, set it the the Audio source you’d like and specify whether you want the audio to play by default on spawn and toggle the Audio component’s Active state.

Basically, follow my fourth step from my first answer. But instead of calling Set Active on the Audio component in a RepNotify function you can call it after you Toggle Visibility on your PFire variable.

Look at your Set Active node, it has a “New Active” checkbox (boolean) parameter. You have to tell it true or false if it should be Activated (sound on) or Deactivated (sound off), respectively.

Look at my image below and notice how I have my “IsFireOn” boolean variable assigned to the “New Active” parameter.

Alternatively, you could use the “Toggle Active” node instead of “Set Active”.

I’ve done what i think you’re talking about but its still not working for me, I’ve most likely done something wrong, and I’m still pretty new to Blueprinting so talk to me like I’m dumb haha

Check out this answer as it shows how to play your sound with a spawn sound, and then setting it as an audio component. Then you would fade out the audio component when you want it to stop (rather than toggling the active state).

Ah that did it! Thank you so much! i Used the Toggle Active instead and it works

If this answer is the solution you were looking for could you could mark this as the Accepted Answer?