How to change the stream of an AudioStreamPlayer via gdscript?

mjpateymjpatey Posts: 12Member

Hello!

I'm working on a 2D platformer, and have my character's footstep sounds synced to his animation. Currently, I'm using two AudioStreamPlayers, one to play the left footstep, one to play the right.

On long stretches of just walking, it sounds repetitive and mechanical, so I'm trying to add variety. Instead of each AudioStreamPlayer always playing the same sample, I want to script one AudioStreamPlayer to randomly select from a list of "left foot" samples, and the other AudioStreamPlayer to select from a list of "right foot" samples.

So I've pulled apart a recording of 30-ish footsteps, separated them into folders, and started scripting.

The issue I'm having is that I don't understand the syntax of how I'm supposed to code this. Here's my first try, just trying to select a specific file (not doing the random selection yet). This is in one AudioStreamPlayer's _process(delta):

self.stream = res://Sounds/Indexed_Footsteps/Left/Footstep_L01.ogg

It gives an error:

Expected end of statement after expression, got ":" instead

Anybody have any idea if this property (stream) is scriptable, and if so, how? I've looked at the docs, which do indeed cover definitions of everything, but I think I'm missing some basic understanding of how to interpret the docs, so examples of the functions and properties in actual use are very helpful to me. So far, I can't find any examples of what I'm lookiung to do.

Thanks in advance for any insight you may have!

-Mark


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 3,959
    Accepted Answer

    Try this:

    self.stream = load("res://Sounds/Indexed_Footsteps/Left/Footstep_L01.ogg")
    

    You will need to use load to load a resource at a given file/resource path, and then you can pass that into the stream of the AudioStreamPlayer (assuming the sound loaded correctly). :smile:

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,959Admin
    Accepted Answer

    Try this:

    self.stream = load("res://Sounds/Indexed_Footsteps/Left/Footstep_L01.ogg")
    

    You will need to use load to load a resource at a given file/resource path, and then you can pass that into the stream of the AudioStreamPlayer (assuming the sound loaded correctly). :smile:

  • mjpateymjpatey Posts: 12Member

    @TwistedTwigleg you rock! That was it. I'm still getting my sea legs in Godot (and programming in general), so when these little pieces of the puzzle come together, it makes a big difference. Greatly appreciated!

    I'm noticing that now the audio in my game glitches a little (short, crackly spikes from one speaker or the other) when I'm walking, occasionally. Could all the realtime loading be causing a bottleneck... and if so, is there a way to get it all loaded in once and just refer to the loaded version (or would that even help)?

  • TwistedTwiglegTwistedTwigleg Posts: 3,959Admin

    Awesome! I'm glad I could help :smile:

    You can preload the audio at the start, which would probably help a bit:

    const SOUND_footstep_L01 = preload("res://Sounds/Indexed_Footsteps/Left/Footstep_L01.ogg")
    
    # Then when you need it...
    # (I'll just be using _ready for this example)
    func _ready():
        self.stream = SOUND_footstep_L01
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file