#[SOLVED] Sound Auto-Played at Position [0,0] after instantiate/add_child(sound_component)

5 messages · Page 1 of 1 (latest)

clever patio
#

Hey,

I wonder how to solve that best:

  • I've a scene with an AudioStreamPlayer2D which starts the sound playback in _ready():
  • I instantiate this scene via instantiate()/add_child()
  • I assign the global_position

⚠ Problem: The sound is NOT hearable if you're not close to [0,0]! Because the sound started playing as soon as it entered the scene and thus, it did not have it's position assigned yet since this command comes AFTER add_Child(). Assigning it before results in errors like can_process: Condition "!is_inside_tree()" is true. Returning: false

How do you solve this? Wait for 1 frame before playing the sound so that the position gets updated? Playing the sound in _process():instead of _ready():? ...

#

[SOLVED] Sound Auto-Played at Position [0,0] after instantiate/add_child(sound_component)

#

Ah ok, I found it!

Instead of starting my sounds in _ready(): I just to this now in _process(): and works!

func _process(_delta):

    if not started:
        started = true
        play_sounds()
austere lake
#

I ran into a similar problem a few days ago. What I did was make a new function and i call that function in the same place where I am instantiating the object after I have done the setup for the object

#

For example, let's say I have my main scene with a script that instantiates a child scene. My child scene has a script with a function (we'll call the function play_sound)

In my main scenes script, I instantiate the child object, I add it to the scene, I change the position of the object to where it needs to be, then I call the childs play_sound function. This will allow you to have more control over when a child does something because you explicitly tell it when to do it (in your case, after you have moved it to it's correct position.)