#Trying to make AudioStreamPlayer2D survive queue_free()

25 messages · Page 1 of 1 (latest)

serene mural
#

Trying to make generalized solution for AudioStreamPlayer2D to survive when parent is queue_free()

Here is my attempt

extends AudioStreamPlayer2D
class_name AudioStreamPlayer2DQFree
func play(from_position=0.0):
    var copy = duplicate()
    copy.finished.connect(queue_free)
    get_owner().add_child(copy)
    copy.play()

Problem is "The method "play" overrides a method from native class "AudioStreamPlayer2D". This won't be called by the engine and may not work as expected."

Any way to make this work, workarounds or best practices?

lilac latch
#

Remove the audio stream using remove_child(audiostream2d)

#

Mrthod

#

Then write

#

get_root().get_tree().add_child(audiostream2d)

serene mural
lilac latch
#

Yeah. You will need to reparent it

#

Other wise itwill also

#

Get deleted

#

With parent

serene mural
#

That's what the example code I provided is trying to do. But you can't override play() method because that's a rule for some reason I guess. I wanted it to work out of the box like AudioStreamPlayer2D you just put it in your scene and it works without having to call special functions. Which is why I thought overriding play() would be the call.

lilac latch
#

Use thst line

#

Instead of get_parent().sdd_child()

#

It will attach it directly to current level

serene mural
#

From what I understand this does the same thing:
get_owner().add_child(copy)

I'll double check that:
get_owner().add_child(copy)
Get_tree().get_root().add_child(copy)
do the same but but that part has been working for a few days.
It's the attempt to override play() so it works out of the box like AudioStreamPlayer2D that is the current problem.

lilac latch
#

So play()

#

Is preventing it

#

??

serene mural
#

Give me a moment to confirm
get_owner().add_child(copy)
Get_tree().get_root().add_child(copy)

lilac latch
#

Notify me if it works

serene mural
#

Okay so there is a difference between those two. An online resource said get_owner() would work but get_tree().get_root() works better. Thank you for that.

#
extends AudioStreamPlayer2D
class_name AudioStreamPlayer2DQFree
func play_uninterrupted():
    var copy = duplicate()
    copy.finished.connect(queue_free)
    get_tree().get_root().add_child(copy)
    copy.play()

This is the current code that works 95% how I want. The only issue now is I can't override the play method so it works with everything out of the box. But this is workable.

lilac latch
#

Thank for sharing . glad it works correctly

serene mural
#

Saw a lot of mention of this issue online. Sound not playing because queue_free() etc. Figured I'd give it a wack. Thanks for your help.