+2 votes

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

in Engine by (31 points)

1 Answer

+1 vote

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.
by (856 points)

There is a separate repository for feature requests: https://github.com/godotengine/godot-proposals (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)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.