Skip to content

coin: Reparent AudioStreamPlayer rather than awaiting#79

Closed
wjt wants to merge 1 commit intomainfrom
wjt/coin-reparent-audiostreamplayer
Closed

coin: Reparent AudioStreamPlayer rather than awaiting#79
wjt wants to merge 1 commit intomainfrom
wjt/coin-reparent-audiostreamplayer

Conversation

@wjt
Copy link
Copy Markdown
Member

@wjt wjt commented Feb 4, 2026

We want the "collected" sound effect to outlast the visible lifetime of the coin: the coin should disappear immediately, but the sound effect needs to keep playing until it's done.

Previously this was implemented by having the coin disable collisions, hide itself, wait for the sound effect (if any) to finish playing, and only then free itself.

Instead, we can reparent the AudioStreamPlayer to the coin's parent node, so that it will continue playing even after the coin is removed from the scene.

We want the "collected" sound effect to outlast the visible lifetime of the
coin: the coin should disappear immediately, but the sound effect needs to keep
playing until it's done.

Previously this was implemented by having the coin disable collisions, hide
itself, wait for the sound effect (if any) to finish playing, and only then free
itself.

Instead, we can reparent the AudioStreamPlayer to the coin's parent node, so that
it will continue playing even after the coin is removed from the scene.
@wjt
Copy link
Copy Markdown
Member Author

wjt commented Feb 4, 2026

The question is: is this clearer than the previous code? I think yes - but if one wanted to add an animation, then one would have to restore the old monitoring/monitored logic anyway.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 4, 2026

Play this branch at https://endlessm.github.io/moddable-platformer/branches/endlessm/wjt/coin-reparent-audiostreamplayer.

(This launches the game from the start, not directly at the change(s) in this pull request.)

Comment thread scripts/coin.gd
if _collect_sound_player:
# Move the sound player node to the coin's parent. Otherwise, when the coin is freed,
# the sound player would also be freed and the sound would cut off.
_collect_sound_player.reparent(get_parent())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the sound change its panning in the left/right speakers because of this? I haven't tried.

In case that happens, Node.reparent() has a second parameter keep_global_transform so you can pass True to it. Unless it doesn't matter because the sound is already playing!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second parameter defaults to True.

@manuq
Copy link
Copy Markdown
Contributor

manuq commented Feb 4, 2026

The question is: is this clearer than the previous code? I think yes - but if one wanted to add an animation, then one would have to restore the old monitoring/monitored logic anyway.

Indeed. Looking at the diff, disabling the collision seems a better option. When debugging collision shapes in the editor, they even change to a grayed-out color when disabled.

@wjt
Copy link
Copy Markdown
Member Author

wjt commented Feb 4, 2026

I'll close this without merging. I wanted to see how it looked because I saw this technique in a video about sound effects and wondered if it would be an improvement. Neither one seems particularly elegant to me, and I think the current code leaves more options open for the curious learner.

@wjt wjt closed this Feb 4, 2026
@wjt wjt deleted the wjt/coin-reparent-audiostreamplayer branch February 4, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants