Get X amount of .png files from a directory that includes sub directories and place them as a deck of cards.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mehmet432

First of all, im a beginner so please explain to me like im a 10 year old.

I have difficulty settings that define CardCount like this,

func _on_Easy_toggled(button_pressed):
	var CardCount = 30

func _on_Medium_toggled(button_pressed):
	var CardCount = 45

func _on_Hard_toggled(button_pressed):
	var CardCount = 60

after the difficulty is selected, i want it to draw “CardCount” amount of cards from res://cards/BaseCards, res://cards/DLC1… and so on that might be added by other players and place them on the next scene.

1st, i tried using arrays but couldn’t understand how to point it to file directories.
2nd, i tried a code i found here, https://forum.godotengine.org/38081/select-random-node2d-from-library-using-data-from-json-file

func get_all_files(path):
	#get all the files you have
	var files = [] 
	var dir = Directory.new()
	dir.open(path)
	dir.list_dir_begin()

	while true:
		var file = dir.get_next()
		if file == "":
			break
		elif not file.begins_with("."):
			files.append(file)
	dir.list_dir_end()
	return files

func create_list():
	 #create a list containing actual instances
	 var files = get_all_files("res://cards/")
	 var sprites = []
	 for file in files:
		 sprites.append(load(file).instance())
	 return sprites

but i don’t know how to get the output from this and place them and i don’t think it scans sub-directories.

:bust_in_silhouette: Reply From: Wakatta

Recursions can be the most annoying thing indeed.

"""
Get all files from path
 - path : top most directory to search
 - files : used to store all paths found
 - Filter : add only paths with extensions in filter to files
"""
func get_all_files(path : String, files : Array, filter : PoolStringArray = []):
	var dir = Directory.new()
	
	if dir.open(path) == OK:
		# ignore hidden and nav paths
		dir.list_dir_begin(true, true)
		
		# get next element
		var file_name = dir.get_next()
		while file_name != "":
			
			#if the current element is a directory
			if dir.current_is_dir():
				# call this function on the child folder
				get_all_files("%s/%s" % [path, file_name], files, filter)
				
			# else it must be a file
			else:
				if filter.empty():
					#add concat filepath to files array
					files.push_back("%s/%s" % [path, file_name])
				else:
					if file_name.get_extension() in filter:
						files.push_back("%s/%s" % [path, file_name])
			file_name = dir.get_next()
		dir.list_dir_end()
	else:
		print("An error occurred when trying to access %s" % [path])

func create_list():
     #create a list containing actual instances
     var files = Array()
     get_all_files("res://cards/", files, ["png"])
     var sprites = []
     for file in files:
         #png when loaded becomes a resource and does not need to be instanced
         sprites.append(load(file))
     return sprites

Will try tomorrow first thing and edit this message, thank you for your attention and time.

mehmet432 | 2023-03-01 00:08

Okay, so after i use either of the following

func _on_TestButton_pressed():
#	create_list()
#	get_all_files("res://cards/", files, [".png"])

when i

print(files)

it always turns out empty

[]

mehmet432 | 2023-03-01 12:07

Apologies seems there was an error in my code remove the . from

get_all_files("res://cards/", files, [".png"])

get_extension() returns the extension without the dot hence the issue

print(create_list()) # should now return a list of loaded StreamTextures

Wakatta | 2023-03-01 13:04

Yeah, now it works, i was going crazy as to why it wasn’t working :slight_smile:

now though when i try

func _on_PlaceRandom_pressed():
	var random_card = CardsList[randi() % CardsList.size()]
	$Card.texture = load(random_card)
	$Card.position.x = 300
	$Card.position.y = 350

I was hoping it would change the texture of $Card according to the file in the directory but it doesn’t ?

Edit: Nvm i got it working i’m dumb

mehmet432 | 2023-03-01 13:26