Add a sound to all the buttons in a project

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hamza_memon

Is there an option on the texture_button or something like that where you can specify a sound to be played on button-press. I have created a global sound_manager but for the button presses, I have to manually code it everywhere the script has a button_pressed signal. Or some kind of logic that when a button is pressed anywhere in the project, sound_manager plays a particular sound.

:bust_in_silhouette: Reply From: JimArtificer

This is a great question and probably something that should be added to Theme in the future. (I suggest adding it as a feature request in Github.)

In terms of how to do this today, I can think of some options, but no perfect solution:

  • Create a small script that you attach to all buttons that plays the sound (perhaps by calling your sound manager)
  • Use a method of observing the scene tree that connects to the appropriate TextureButton signal whenever one is added to the scene tree.
  • Try to detect if the mouse is above a button each time a click is performed. (Probably the least accurate and most difficult approach.)
  • Extend TextureButton with your own “MyTextureButton” class that adds the logic to play the sound. Then attach the same empty script that extends from your own class to all of your buttons. The advantage of this approach is that there’s only one place to make a change because you don’t end up with duplicated code when a button requires unique scripting.

There is a separate repository for feature requests: GitHub - godotengine/godot-proposals: Godot Improvement Proposals (GIPs) (Creating an issue in the main repository will display that as well, but people might miss it, so I’d argue it’s better to link them to the right repo right away)

njamster | 2020-07-16 17:22