如何在 GDScript(Godot 引擎)中合并两个字典或数组?

发布于 2025-01-12 23:45:22 字数 368 浏览 2 评论 0原文

假设我有两个字典(或者可能是数组),每个字典都有子字典(或子数组):

var dict_A = {'a': 1, 'sub_dict':  {'hello': 'world', 'quick': 'fox'}}
var dict_B = {'b': 2, 'sub_dict': {'hello': 'godot'}}

GDScript 中是否有内置方法来合并它们,包括子字典或子数组?

预期结果是:

merge_dict(dict_A, dict_B)

# {'a': 1, 'b': 2, 'sub_dict':  {'hello': 'godot', 'quick': 'fox'}}

Say I have two Dictionaries (or could be Arrays), each having sub-dictionaries (or sub-arrays):

var dict_A = {'a': 1, 'sub_dict':  {'hello': 'world', 'quick': 'fox'}}
var dict_B = {'b': 2, 'sub_dict': {'hello': 'godot'}}

Is there a builtin method in GDScript to merge them, including the sub-dictionaries or sub-arrays?

Expected result would be:

merge_dict(dict_A, dict_B)

# {'a': 1, 'b': 2, 'sub_dict':  {'hello': 'godot', 'quick': 'fox'}}

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

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

发布评论

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

评论(1

铃予 2025-01-19 23:45:22

GDScript 中没有为此提供内置方法,但您可以使用以下函数(查找使用示例、单元测试等更多信息 Github gist):

观察:使用时deep_merge=true 使用 merge_array,所有子词典和子数组都必须是 JSON 可序列化的。

func merge_array(array_1: Array, array_2: Array, deep_merge: bool = false) -> Array:
    var new_array = array_1.duplicate(true)
    var compare_array = new_array
    var item_exists

    if deep_merge:
        compare_array = []
        for item in new_array:
            if item is Dictionary or item is Array:
                compare_array.append(JSON.print(item))
            else:
                compare_array.append(item)

    for item in array_2:
        item_exists = item
        if item is Dictionary or item is Array:
            item = item.duplicate(true)
            if deep_merge:
                item_exists = JSON.print(item)

        if not item_exists in compare_array:
            new_array.append(item)
    return new_array


func merge_dict(dict_1: Dictionary, dict_2: Dictionary, deep_merge: bool = false) -> Dictionary:
    var new_dict = dict_1.duplicate(true)
    for key in dict_2:
        if key in new_dict:
            if deep_merge and dict_1[key] is Dictionary and dict_2[key] is Dictionary:
                new_dict[key] = merge_dict(dict_1[key], dict_2[key])
            elif deep_merge and dict_1[key] is Array and dict_2[key] is Array:
                new_dict[key] = merge_array(dict_1[key], dict_2[key])
            else:
                new_dict[key] = dict_2[key]
        else:
            new_dict[key] = dict_2[key]
    return new_dict

此代码已获得 BSD 3 条款许可证 |版权所有 2022 Hackverse.org

There's no builtin method in GDScript for that, but you can use the following functions (find usage examples, unit tests, and more on this Github gist):

Observation: when using deep_merge=true with merge_array, all sub-dictionaries and sub-arrays must be JSON serializable.

func merge_array(array_1: Array, array_2: Array, deep_merge: bool = false) -> Array:
    var new_array = array_1.duplicate(true)
    var compare_array = new_array
    var item_exists

    if deep_merge:
        compare_array = []
        for item in new_array:
            if item is Dictionary or item is Array:
                compare_array.append(JSON.print(item))
            else:
                compare_array.append(item)

    for item in array_2:
        item_exists = item
        if item is Dictionary or item is Array:
            item = item.duplicate(true)
            if deep_merge:
                item_exists = JSON.print(item)

        if not item_exists in compare_array:
            new_array.append(item)
    return new_array


func merge_dict(dict_1: Dictionary, dict_2: Dictionary, deep_merge: bool = false) -> Dictionary:
    var new_dict = dict_1.duplicate(true)
    for key in dict_2:
        if key in new_dict:
            if deep_merge and dict_1[key] is Dictionary and dict_2[key] is Dictionary:
                new_dict[key] = merge_dict(dict_1[key], dict_2[key])
            elif deep_merge and dict_1[key] is Array and dict_2[key] is Array:
                new_dict[key] = merge_array(dict_1[key], dict_2[key])
            else:
                new_dict[key] = dict_2[key]
        else:
            new_dict[key] = dict_2[key]
    return new_dict

This code is licensed under BSD 3-Clause License | Copyright 2022 Hackverse.org

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文