I want to fill an ItemList with all the files in a USER:// directory and I have no trouble getting the filenames but it the Icons just don't show up. I've tried to set the texture to a Sprite and no luck there either, from the output it seems like there are no issues loading the images.
I assumed there to be a problem where the texture would be replaced with empty textures but forcing it to run only once on a single image file made no difference.
Here is the code.
extends PopupMenu
var hasGottenFileList = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func get_filelist(scan_dir : String) -> Array:
if hasGottenFileList == false:
hasGottenFileList = true
var my_files : Array = []
var dir := Directory.new()
if dir.open(scan_dir) != OK:
printerr("Warning: could not open directory: ", scan_dir)
return []
if dir.list_dir_begin(true, true) != OK:
printerr("Warning: could not list contents of: ", scan_dir)
return []
var file_name := dir.get_next()
while file_name != "":
if dir.current_is_dir():
my_files += get_filelist(dir.get_current_dir() + "/" + file_name)
else:
my_files.append(dir.get_current_dir() + "/" + file_name)
var iconTex = self.crate_sprite(dir.get_current_dir() + "/" + file_name)
print(file_name)
get_node("ItemList").add_item(file_name, iconTex)
file_name = dir.get_next()
return my_files
return []
func crate_sprite(path):
var img = Image.new()
var err = img.load(path)
if(err != 0):
print("error loading the image: " + path)
return null
var img_tex = ImageTexture.new()
img_tex.create_from_image(img)
print("loading " + path + " images")
var sprite = Sprite.new()
sprite.texture = img_tex
return sprite