When I try to use get on anything more than one word (“testdict” itself for example) it wont work and prints out null. This is a really simplified version of the problem I’m having but I didn’t expect it to persist on this. Do I need to modify the get function or something? I have also tried str_to_var(test)
func _ready() -> void:
var testdict: Dictionary = {"test1": 23, "test2": 11}
var test: String = "testdict.test1"
print(test)
print(get(test))
print(testdict.test1)
get can only retrieve object properties for an object you’re calling it on. So calling get without an object implies self as an object and therefore will only see the class properties of self, which is a script class your code is written in.
If you want to get a dictionary entry you’ll need to call get on a dictionary, in your case:
It includes script class properties, either custom or built in, but it doesn’t include local variables.
var foo # class property
func _ready() -> void:
var bar # local variable
self.get("foo") # works, foo is a class property
self.get("bar") # won't work, bar is a local variable
Typing self. is, of coruse, optional. If you omit it - it will be implied.
This is my actual code currently, and I cant seem to get it to work with this new information. An example command would be “A:/users” and found should be equal to the dictionary with foldername users, however I cannot get it to be reusable with the current ‘found += “.foldercontents[” + str(count) + “]”‘ line.
var found: String = ""
func explorefiles(command: String) -> void:
print("Command: " + command)
var segments: Array = command.split("/", false)
found = ""
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 a in get(found).foldercontents:
if get(found) is Dictionary and get(found).foldername and a.foldername and a.foldername == segments[i]:
found += ".foldercontents[" + str(count) + "]"
count += 1
elif !get(found):
found += "files"
print(found)
#how i have 'files' formatted
{"foldername": "A:", "foldercontents": [{"foldername": "users","foldercontents": [{"fname": "READ_ME", "ftype": "txt", "fcontents": "hai :3"}]}]}
You should ask a new question with this problem as this is no longer strictly related to the title. It’s a much broader problem.
If you want to simulate a directory tree traversal, you first need to precisely define the “syntax” of your dictionary, and also should it allow for folders that contain both files and folders.
Ultimately, you could just create actual files/folders in user directory and use DirAccces to navigate it easily.