This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Build string and use it as AaudioStream.stream name

StreetslabStreetslab Posts: 1Member
edited April 2020 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: 2,561Admin
    edited April 2020

    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

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.