File Directory Simulation

Godot Version

Godot 4.5.1.stable with godotsteam

Question

Attempting to simulate a file directory tree thingy, but whenever I attempt to run “A:/users” as the command to display the text file, it prints “Nothing found.” to the terminal. Any advice would help a ton.

#filesystem
var files: Dictionary = {"foldername": "A:", "foldercontents": [{"foldername": "users","foldercontents": [{"fname": "READ_ME", "ftype": "txt", "fcontents": "hai :3"}]}]}
var found: String = ""
var remotefiles: Dictionary

func explorefiles(command: String) -> void:
	print("Command: " + command)
	var segments: Array = command.split("/", false)
	found = ""
	#findlocation
	for i in segments.size():
		if get(found) is Dictionary and get(found).foldercontents and get(found).foldercontents is Array:
			var count: int = 0
			for j in get(found).foldercontents:
				if get(found) is Dictionary and get(found).foldername and j.foldername and j.foldername == segments[i]:
					found += '.foldercontents[' + str(count) + ']'
				count += 1
		elif !get(found):
			found += "files"
	print(found)
	
	#displaytitle
	var something: bool = false
	if get(found) is Dictionary and get(found).foldername:
		printtoterm("Contents in Folder$" + get(found).foldername)
		something = true
	
	#displaycontents
	if get(found) is Dictionary and get(found).foldercontents and get(found).foldercontents is Array:
		for i in get(found).foldercontents:
			if i is Dictionary:
				if i.foldername:
					printtoterm("Folder$" + i.foldername)
				elif i.fname and i.ftype:
					printtoterm(i.fname + "." + i.ftype)
	if something == false:
		printtoterm("Nothing found.")

get(found) tries to read a variable by the name of found

for example

var foo: int = 123
var bar: int = 456

var bar_text: String = "bar"

print(get("foo")) # prints 123
print(get(bar_text)) # prints 456
print(get("baz")) # prints <null>

So your get(found) will only return null as found is often empty "", furthermore the get function does not attempt to index into variables, so you cannot use get to retrieve data inside a dictionary or array.

var dict: Dictionary = {"foo": "A", "bar": "B"}

print(get("dict.foo")) # prints <null>
print(get("dict[\"foo\"]")) # prints <null>
1 Like

Is there a way to set up a function or something so that I can get data from inside an array from a get function? I’ve just tried something like this;

if get(found) is Dictionary and get(found).foldername and j.foldername and j.foldername == segments[i]:
					var segments2: Array = found.split('.', false)
					var temp: String = '.get("foldercontents.get("[' + str(count) + ']")")'
					
					found += '.get("foldercontents").get("[' + str(count) + ']")'

I also tried this;

if get(found) is Dictionary and get(found).foldername and j.foldername and j.foldername == segments[i]:
					#found += '.foldercontents[' + str(count) + ']'
					found = "files"
					for k in segments2.size():
						if k + 1 != segments2.size():
							found += "." + segments2[k]
						else:
							found += segments2[k].substr(0,segments2[k].find(")") - 1)
					
					found += temp
					
					for k in segments2.size() - 1:
						found += '")'

You could try using the filenames as keys, directories would be sub-dictionaries and files would be Strings

var files: Dictionary = {
	"A:": {
		"users": {
			"READ_ME": "hai :3",
		},
	},
}

Then directory traversal is through dictionary keys, much like nodes in a tree. Instead of mutating found we change which directory is being looked through, deeper and deeper.

var current_directory: Dictionary = files
var segments := command.split("/", false)
for s in segments:
	if s in current_directory:
		var file: Variant = current_directory[s]
		if file is Dictionary:
			current_directory = file
		else:
			print("Found contents: ", file)
	else:
		print("Could not traverse path: ", s)
2 Likes

I am so confused right now but i will give this a shot (also right now)

Worked perfectly, thanks a ton!