Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Godot Audio Player Node Looping

A topic by Drunko created Mar 28, 2020 Views: 842 Replies: 7
Viewing posts 1 to 4
Submitted

So I am trying to create a platformer and decided to add a sound that plays when the player falls on a platform. This is the code I used:

    if move_and_collide(direction.normalized()):
        $AudioStreamPlayer.play()

The only problem is that the sound loops unnecessarily every frame there is a collision. I would like to know if anyone knows how to make it play once. Apologies for my amateur coding and amateur coding knowlegde.

Cheers,

-Drunko

Submitted

Is the audio stream set to loop?

Submitted

no

Submitted

Probably you are executing this code into a loop (_physics_process or _process) and move_and_collide is returning the colision information when it's hits the platform, so $AudioStreamPlayer.play() is always be called. To solve it you can declare a gloal variable thats saves the current touched plataform then check if the current platform is diferent from the saved, so the play function will be called once per platform. Something like this:

var current_platform = null # global variable
[...]
func _physics_process(delta):
     [...]
     var colision = move_and_collide(direction.normalized()) # get collision data
     if colision: # check if is colliding
          if current_platform != colision.collider: # check if the colliding object is different from last one
               current_platform = colision.collider # save current platform
              $AudioStreamPlayer.play() # play the audio

Will be good to set current_platform as null when an action like jump be done for the audio be played when platform be touched again.
There are several other approaches to solve this problem, this is one of them.

  

Submitted

This bit of code works, but the sound only plays once per scene. Not exactly sure what is causing this, but it is most likely a problem with the node's properties.  Here they are.

P.s: It is a .wav file and none of the import options are on.

Submitted (1 edit)

The node options looks good. It might be some logic problem. Take a look in this code: https://github.com/Guizeronet/godot-platform-test/blob/master/obj/char/char.gd

You can download my example and see it running. It is different from my last comment, it can works better. (Download here: https://github.com/Guizeronet/godot-platform-test )

Submitted

Sorry, but the way you used that logic doesn't fit with what I imagined. Fortunately, the basics of your logic helped me out with sort of what I wanted. So, thanks a bunch!

Submitted

What kind of audio file are you playing? Last I remember .ogg files will loop by default as these are usually used for game music and .wav files play only once.