Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to merge two Dictionaries together #3739

Closed
DarcTellier opened this issue Jan 2, 2022 · 2 comments
Closed

Add a method to merge two Dictionaries together #3739

DarcTellier opened this issue Jan 2, 2022 · 2 comments

Comments

@DarcTellier
Copy link

DarcTellier commented Jan 2, 2022

Describe the project you are working on

Discord bot using godot engine.

Describe the problem or limitation you are having in your project

I couldn't find a method to merge dictionary's.
Found the solution on this page, works so good that it needs to be in the main repository.
https://godotengine.org/qa/8024/update-dictionary-method

Describe the feature / enhancement and how it helps to overcome the problem or limitation

when I would load a users save file, if I added new elements to the save file dictionary . the new elements will not be added to the loaded file.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

#dictionary merger copy paste below

static func merge_dir(target, patch):
    for key in patch:
        if target.has(key):
            var tv = target[key]
            if typeof(tv) == TYPE_DICTIONARY:
                merge_dir(tv, patch[key])
            else:
                target[key] = patch[key]
        else:
            target[key] = patch[key]

If this enhancement will not be used often, can it be worked around with a few lines of script?

yes

Is there a reason why this should be core and not an add-on in the asset library?

super useful

@Calinou Calinou changed the title Dictionary merger method *function code soultion included* Add a method to merge two Dictionaries together Jan 2, 2022
@Mickeon
Copy link

Mickeon commented Jan 2, 2022

It should probably be called Dictionary.merge().

If it's a reasonable suggestion, but not much of a deal-breaker, I personally think it would be better for it not to be static, so that something along the lines of target.merge(patch) would be possible, and less verbose.

@Calinou
Copy link
Member

Calinou commented Jan 2, 2022

Duplicate of #1756.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants