Toggle Music On/Off From UI Widget

Yeah, sorry if I didn’t mention this… On my end all this is in the PC. That’s where I’m creating and adding the MainMenu widget.

Then it does not matter - but the widget does not need to Get Controller → Cast. It’s all happening in the controller already.

So then in the Widget itself, do I have to add Event Dispatchers for the ToggleButton and for the slider?

No. Buttons have dispatchers already, so do sliders - see the animation above. You can add a New Event Dispatcher if you need to send some custom data.

Gotcha. Never done it before so I wasn’t sure if you can Bind to those buttons without actually creating a dispatcher beforehand. All clear now… well… hopefully :slight_smile:

1 Like

couple of questions if you don’t mind…
I am trying to do this inside a function just to keep the PC EG cleaner. I noticed that after creating the widget, you didn’t put the “add to viewport” node. I would like this function to kick in as soon as the widget is added to the viewport. How can I get the target - > ToggleButton inside my function to continue the flow in there???

Not sure, if I relayed my intention clearly… When the game starts, the MainMenu is added by the PC to the viewport instantly.
I would like the music to start playing at that moment, but give the user the option to turn it off or adjust the volume. When closing the widget and actually beginning to play I want the music to stop, as soon as the widget is closed.
So the music should only ever play if the MainMenu is on

@Everynone
Also, you have Add Audio Component hooked up to Event BeginPlay. In my case the Create New Widget → Add to Viewport is already hooked up on Event BeginPlay.
How else can I fire this?

And yeah, I guess I’ll have to ditch the function, since I cannot seem to be able to add a custom event inside a function :frowning:

Pfewwwwww… this was a doozy :slight_smile:

IT WORKS :+1:

Thanks guys for being patient with me.

Now… how can I Pause the music, when the Widget is removed from parent???
Inside the Widget EG this is how I kill the Widget:

How can I let PC know to Pause the music when this happens???

Oh… snap… I think I got it

A little messy having all this in the PC, but it actually works beautifully. Thanks @Everynone and @BIGTIMEMASTER

If anything… at least I understand how binding to elements from the UI works now :rofl:

1 Like

Aye. You can always drag the red wire and create an event. This can be done inside a function. The delegates can bind to functions themselves, too!

@Everynone

Ok, sorry for trying to beat a dead horse here… but… is there a way to implement all this inside the widget itself???

Hear me out… this music should only ever play inside the MainMenu. When it’s loaded it starts… let’s the user play with the buttons, sliders etc etc, but as soon as the widget is closed… the music should be gone.
Having it in the PC is not only messy, but seems overkill to me.
Also, if I have to call the MainMenu ever again, let’s say on pressing ‘Esc’ key… I have to do all this all over again. (One of the reasons, I wanted it to be in a function to begin with.)

In the MainMenu widget I noticed there’s a “Play Sound 2D” (Plays a sound directly with no attenuation, perfect for UI sounds.)
Would it be possible to incorporate all this functionality using that, so there’s no need to communicate/delegate/dispatch anything else??? Everything happens in that widget?
It seems more intuitive to me since this music belongs to that widget.
And instead of BeginPlay, maybe it could be hooked on Event Construct??? just a thought…
Let me know what you think…
Or is this just bad practice? Any guidance is greatly appreciated.

If the PC does not need to know about any of this, I’d keep it out of it, for sure. Regarding the mess, you can do this:

This will auto-generate a function / event with a matching signature. In addition, you can also do the above inside a separate function, cleaning up everything really nicely. I usually have a separate function dedicated to binding / unbinding dispatchers.

it could look like so:

2 functions only, no mess and the functionality is reusable.

This method of binding would also work on 2 completely unrelated actors by providing the target object for the Create Even node.


Would it be possible to incorporate all this functionality using that, so there’s no need to communicate/delegate/dispatch anything else??? Everything happens in that widget?

Sure, why not - makes sense. The fewer blueprints are involved and more encapsulation you can produce - the better. You may be able to spawn a component like this inside the widget:

Never tried but it could / should work.

Yeah, I think I don’t want it in the PC. Not only messy, but I feel like its place should not be there. No need to pile crap on PC for a thing like this.
I also, always read that you should stay away from adding code like this in the level blueprint, so the only other logical way I was thinking of was… to find a way and have it contained in the widget itself.

I’ll see what I can come up with…

After more tweaking and playing with it this is what I was able to come up with…
Please let me know if you see any issues with this, but as of right now it is working exactly as I wanted it to. Game loads, MainMenu pops up, music starts, user has control to toggle on/off and adjust volume. Much simpler, no dispatching, casting or delegating. It all happens in the widget.

I haven’t tested this functionality yet, but what happens to the spawned sound when the widget is killed??? Let’s say if I bind the widget to “Esc” key and later on the user loads the MainMenu again… do I get a trail of garbage spawned sounds? Do I need to de-spawn that somehow when I close the widget??? Not sure of the implications later on, but as of right now it’s the simplest solution I could come up with.
Any suggestions and/or tips are greatly appreciated.

Hello, Thanks for the topic!
I am new in UE, it’s my code and it works :grin: