Play a Sample if any Button is pressed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ingo_nikot
:warning: Old Version Published before Godot 3 was released.

Hello,

is it possible to play a specific sample everytime any Button gets pressed without changing every single button individually?

Something like a global setting or theme setting.

Or do i have to implement my own Button class derived from Button which then will call the play function?

I don’t think it’s possible but I’ve never needed it so IDK

HatEm | 2017-01-05 16:44

:bust_in_silhouette: Reply From: Zylann

I dont’t think themes have this, but you have several ways:

  1. You can have a script iterate over all nodes of the scene, find all those that inherit Button and add a connection to the same function on your script. So when you press any of the buttons it will trigger the function.

  2. Have your own button script or scene that handles sound the way you want (I would do that personally because it’s more flexible).

can you explain 2) further?

in my case i have like 50+ buttons and i want them all to play the same click sound. thats why i want a central solution.

or is " your own button script" meant as an inherited version of the class Button?

ingo_nikot | 2017-01-05 20:15

Yes, you would make a script inheriting Button to do whatever you want with it.
If you have 50 Buttons you can have a script running through them to connect a signal or add a script like in 1), or make a new scene for that kind of button (with a script in it) and instance the scene 50 times. This way you’ll be able to change all these buttons by simply changing their scene.
Also, as a general tip, if you find yourself copying and pasting something many times, there must be a way to factor it so you’ll be able to change instances more easily :slight_smile:

Zylann | 2017-01-06 00:28

> if you find yourself copying and pasting something many times, there must be a way to factor it so

agree, thats what i meant with “central solution”

ingo_nikot | 2017-01-06 10:14