How to list all json files in a directory?

Godot Version

4.5.1

Question

I know how to read and write to specific .json files, but i would like the user to get a list to select what json file to load, for that i need a way to scan a directory for files and list the filenames. As i understand it DirAccess.get_files() only works on files allready present when the game is compiled so it can't accept user generated files.

This is not true, you can very much use this to get files in other directories, not just the res:// folder. I don’t know where these Json files would be, but you can use get_files to get a list of files, then simply use the string’s get_extension method, as seen here: String — Godot Engine (stable) documentation in English

Here’s a very simple example:

func _ready() -> void:
	var items = DirAccess.get_files_at("/home/tib/")
	for item in items:
		if item.get_extension() == "json":
			print(item)
3 Likes

Thank you, this helped greatly.

1 Like