0 votes

So I have a AudioStreamPlayer, that has script that iterates through a folder and pulls all the wav files and stores them in an array and then a method to play one of them at random and another to skip to the next song. When I do this in the editor it works with no issues or errors, but as soon as I export it to a runnable windows exe it fails to retrieve any of the files and shows a error of
Invalid get index '0' (on base: 'Array').
At: res://World/MusicPlayer.gdc:32

this is the code below im using

extends AudioStreamPlayer

var music_list = []
func ready():
getallmusic()
picksong()

func input(event):
    if event.isactionpressed("skipsong"):
    pick_song()

func getallmusic():
    var dir = Directory.new()
    dir.open("res://Assets/Music/")
    dir.listdirbegin()

    while true:
        var file = dir.get_next()
        if file == "":
            #no more files
            break
        elif not file.begins_with(".") && !file.ends_with(".import"):
            music_list.append(load("res://Assets/Music/" + file))

    dir.list_dir_end()
     func picksong():
    randomize()
    var listlen = musiclist.size()
    var i = randi() % (listlen - 1)
    stream = musiclist[i]
    print(musiclist[i])
    play(0)

func onMusicPlayerfinished():
    picksong()
Godot version 3.3.stable.mono
in Engine by (21 points)

1 Answer

+1 vote
Best answer

The problem is that you cannot find imported files like your music in an exported project. They are not exported. What is exported is the imported files in the .import folder. However, the .import files are still there. You will be able to find the .import files with the directory. So instead of excluding them from the search, only look for them. Then, remove the ".import" from the end and load the file.

by (8,546 points)
selected by

thanks that solved it!

elif not file.begins_with(".") && file.ends_with(".import"):
        file = file.split(".import")
        music_list.append(load("res://Assets/Music/" + file[0]))

there is my edit to my code case anyone else runs into this issue

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.