Godot读了嵌套词典

发布于 2025-02-14 01:21:45 字数 1379 浏览 1 评论 0原文

我正在尝试在Nest词典中访问项目。硬编码是很好的dict [string_1] [string_1a],但是当用户选择UI中的项目并向下钻探时,我不确定如何“关注”它们。我有一个小lendedit,可以显示诸如String_1/String_1之类的路径(类似于计算机显示文件夹的方式)。但是,如果我想创建一个新项目,例如[string_1] [string_1b],我不确定该怎么做。我能够用路径[string_1] [string_1b]打印字符串,但是我不知道如何将其实际添加到字典中,因为我无法做str2var(我知道),我可以t只需传递字符串_1b,就必须在String_1内部。

func update_path(object, full_path):
    var new_path = ""
    
    full_path.insert(0, object.get_text(0))
    new_path = object.get_parent()
    
    if new_path != null:
        update_path(new_path, full_path)
    
    else:
        pathEdit.text =  ""
        
        # for some stupid reason I can't remove 0, it throws an error
        for item in full_path:
            if item != "":
                pathEdit.text += item + "/"

    print("saving subcategory")
    print(pathEdit.text)
    
    var save_path= pathEdit.text.split("/")
    print(save_path)
    
    var dumb_string = "Globals.notebook_data"
    
    for path in save_path:
        if path != "":
            print(path)
            #print(Globals.notebook_data[path])
            #print(Globals.notebook_data[save_path])
            #print("Globals.notebook_data"+ )
            dumb_string += "[" + path + "]"
    
    print(dumb_string)
    print(str2var(dumb_string))

字符串是正确的路径,我只是不知道如何“访问”它。我正在考虑循环浏览数组,但是我不确定如何处理它,如果我只是访问该级别,它就不会使字典保持整体。

I am trying access items in a nest dictionary. Hard coding it is fine dict[string_1][string_1a], but when user is selecting items in the UI and they drill down, I am not sure how to "follow" them. I have a little LineEdit that shows the path such as string_1/string_1 (similar to how computers show folders). However, if I want to create a new item such as [string_1][string_1b], I am not sure how to do that. I am able to print the string with the path [string_1][string_1b], but I don't know how to actually add that back into the dictionary as I can't do str2var (that I am aware of), and I can't simply pass string_1b as it has to be inside string_1.

func update_path(object, full_path):
    var new_path = ""
    
    full_path.insert(0, object.get_text(0))
    new_path = object.get_parent()
    
    if new_path != null:
        update_path(new_path, full_path)
    
    else:
        pathEdit.text =  ""
        
        # for some stupid reason I can't remove 0, it throws an error
        for item in full_path:
            if item != "":
                pathEdit.text += item + "/"

    print("saving subcategory")
    print(pathEdit.text)
    
    var save_path= pathEdit.text.split("/")
    print(save_path)
    
    var dumb_string = "Globals.notebook_data"
    
    for path in save_path:
        if path != "":
            print(path)
            #print(Globals.notebook_data[path])
            #print(Globals.notebook_data[save_path])
            #print("Globals.notebook_data"+ )
            dumb_string += "[" + path + "]"
    
    print(dumb_string)
    print(str2var(dumb_string))

The string is the correct path, I just simply don't know how to "access" it. I was thinking of looping through the array, but I am not sure how to tack it on, and if I simply access just that level it doesn't keep the dictionary whole.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-02-21 01:21:45

找到答案,需要递归保存。

https://godotengengine.orgg/qa/ 81247/by-a-dictionary-by-titerrating-on-it-its-keys

func set_path(path, value):
    var keypath: PoolStringArray = ["key1","key1_1","key1_1_1","foo"] 
    _set_path(dictionary,keypath,value)

func _set_path(dict,keypath, value):
    var current = keypath[0]
    if dict.has(current):
        if typeof(dict[current]) == TYPE_DICTIONARY:
            keypath.remove(0)
            _set_path(dict[current],keypath, value) # recursion happens here
            return
        else:
            print_debug("found")
            dict[current] = value
            return
    print_debug("not found")

Found an answer, needed to save it recursively.

https://godotengine.org/qa/81247/update-a-dictionary-by-iterating-on-its-keys

func set_path(path, value):
    var keypath: PoolStringArray = ["key1","key1_1","key1_1_1","foo"] 
    _set_path(dictionary,keypath,value)

func _set_path(dict,keypath, value):
    var current = keypath[0]
    if dict.has(current):
        if typeof(dict[current]) == TYPE_DICTIONARY:
            keypath.remove(0)
            _set_path(dict[current],keypath, value) # recursion happens here
            return
        else:
            print_debug("found")
            dict[current] = value
            return
    print_debug("not found")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文