Sound in GUI - Am I really doing this right?

I want to play a “click” sound when a button is pressed in a GUI. I can’t see anything in the documentation that addresses this.

AFAICT the only way to access a sound is by adding it to a game object, right?

Like so:

go
-sound

AFAICT There is NO WAY to add a sound object to a GUI.

The problem is that the gui I want to play the sound in is a gui loaded by the root collection and then overlaid on all proxies loaded. It’s a dialogue box implementation. I do it this way so that I can easily pause the proxy while the dialogue is active, etc.

So, structure is:

main.collection
-#level_proxy
-main_controller
-dialogue_gui

i.e. the sound I want to play in the gui needs to be added to this main.collection as an object like so?

MAIN.collection
-#level_proxy
-main_controller
-dialogue_gui
-sound_go

And then I have the dialogue_gui message the sound_go to play the sound.

This works fine, but I’m worried about constantly having all the .go sounds loaded over all subsequent collections like that… Is there a better implementation of this I’m missing, or is this really the way it’s intended?

I think the best way is to have one game object with all the sound components and a script that acts as sound manager, and to send messages from other scripts to the sound manager to play and stop sounds. Or maybe to make a Lua module that handles the messages to that sound manager.

It is no problem to have a sound manager with a few dozen sound components be loaded at all times. If you have a large number of sound files and you notice any performance problems from it, you could still modify the sound manager to load and unload sounds, but you’d need a lot of sounds for that, I wouldn’t worry about that from the start.

3 Likes