This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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.

get_parent().get_node(SamplePlayer).play("mysample")

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

in Engine by (330 points)

1 Answer

0 votes

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

get_tree().get_root().get_node("SamplePlayerName").play("mysample") 
#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.

by (7,954 points)

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

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

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 Frequently asked questions and 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 [email protected] with your username.