如何在 GDScript(Godot 引擎)中合并两个字典或数组?
假设我有两个字典(或者可能是数组),每个字典都有子字典(或子数组):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GDScript 中没有为此提供内置方法,但您可以使用以下函数(查找使用示例、单元测试等更多信息 Github gist):
观察:使用时
deep_merge=true
使用merge_array
,所有子词典和子数组都必须是 JSON 可序列化的。此代码已获得 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
withmerge_array
, all sub-dictionaries and sub-arrays must be JSON serializable.This code is licensed under BSD 3-Clause License | Copyright 2022 Hackverse.org