How to play sample after any button is pressed?

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

I have multiple scenes and all of them have buttons. What Im trying to do is have 1 global SamplePlayer and when any button is pressed mysample is played.


Tried code above but for this I need SamplePlayer in every scene…
When im trying to create global I dont know how to setup sample library.

:bust_in_silhouette: Reply From: eons

To keep it visual, a scene (tscn/scn) with a sampleplayer can be added as autoload, then:

#if the sampleplayer is root on autoloaded scene (check remote inspector tree)

A has_node check first may be good.

Or better, add it to a group too, then on the buttons:

get_tree().call_group(0, "global_sample_group", "play","mysample")

This will work even if the global is not loaded (like for tests) so you wont have to check has_node all the time.

There are many other ways to work with groups.

So I have to create new scene just with sampleplayer - make it autoload and then i can call it from any scene right?

Sprowk | 2016-11-24 20:02

Exactly, is useful for cases where you have a common scene for the whole game (like music, backgrounds…).

eons | 2016-11-24 20:10