Build string and use it as AaudioStream.stream name

StreetslabStreetslab Posts: 1Member
edited April 13 in Audio

Hi all, new member here.
I was wondering if there is a way to pass a string that is dynamically created as an AudioStream object.
For sure there is and i'm too new/dumb yet to figure it out.
Something like:

sound var r500 :  AudioStream = load("res://Sounds/500_"+name+".ogg") 

and then play it like this:

        snd = "r"+str(int(rpm))
        $sound.set_stream(snd)
        $sound.play(0)

Thanks.
Streetslab


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 3,076Admin
    edited April 13

    Welcome to the forums @Streetslab!

    I think the set_stream function expects an AudioStream object as the passed-in argument. This means that you will not be able to pass a string to it as the input.

    Instead, you'll need to load the AudioStream, and then pass that to it. For example, if name is the name of the audio file you want to load:

    var audio_path = "res://Sounds/500_" + str(name) + ".ogg"
    var audio_stream = load(audio_path)
    
    # setting stream directly is the same as calling set_stream, because
    # set_stream is the setter function for the property.
    $AudioStreamPlayer.stream = audio_stream
    $AudioStreamPlayer.play(0)
    

    In the sample code you provided in your post, I am not sure what snd is for, so I didn't incorporate it into the above example.

    Hopefully this helps a bit!

Leave a Comment

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