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 for setting dictionary key #11645

Open
Jesusemora opened this issue Jan 26, 2025 · 2 comments
Open

Add a method for setting dictionary key #11645

Jesusemora opened this issue Jan 26, 2025 · 2 comments

Comments

@Jesusemora
Copy link

Describe the project you are working on

3d game

Describe the problem or limitation you are having in your project

currently the way to set dictionary values is through the [] operator, by providing a key:
mydictionary["key"] = value

obtaining a value can be done the same way:
value = mydictionary["key"]

but I prefer to use the get method instead, as it can always return a correct value, which makes code clearer:
value = mydictionary.get("key", 0)
without the get method I would have had to do:

if mydictionary.has("key"):
    value = mydictionary["key"]
else:
    value = 0

however, godot's dictionaries do not have a set method to set a key/value, so in order to avoid repetition we need to first test for the key:

if mydictionary.has("key"):
    mydictionary["key"] = value

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

add a set method to dictionaries that works similar to get. we can use an additional optional field to define behaviour:

#mode can be an enum with options such as "create or replace", "create if it doesn't exist but don't replace" or "replace if it exists but don't create new"
mydictionary.set("key", value, mode)

the previous code would now look something like this:
mydictionary.set("key", value, AccessDictionaryKeyMode.SWAP)

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

the set method would look something like this:

void set(key: Variant, value: Variant, mode: AccessDictionaryKeyMode = 0)

enum AccessDictionaryKeyMode:
    AccessDictionaryKeyMode TRUNCATE = 0 #replace contents of key or set a new key, equivalent to using the [] operator
    AccessDictionaryKeyMode NEW = 1 #create a key if it doesn't exist but don't override if it does
    AccessDictionaryKeyMode SWAP = 2 #replace contents of an existing key only, don't create a new one

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

this reduces the lines of code required when dealing with dictionaries. technically yes, but this aims to make code cleaner

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

this is core

@TanyaPegasus
Copy link

Unless I’m misunderstanding what this means, and what can already be done, a way to update entries if they exist, without having to do a separate check first, would be quite useful.

I ran into several bugs while misusing get_or_add. It clearly states what it does, so that was my bad for misunderstanding while learning a lot of new things, but something like an add_or_update function would be extremely welcome.

That being said, while reading the docs just now, I’ve realised that perhaps merge might be useful to me, but I do think that for code readability, and user friendliness, something like add_or_update would be quite useful. For me personally, just having a function with a name like that would have helped me realise straight away that get_or_add wasn’t always what I needed.
(And yes, with hindsight, get_or_add is a perfectly clear name. I’m definitely not arguing that)

@sockeye-d
Copy link

sockeye-d commented Jan 27, 2025

I think instead of using an enum, a new method could be added

# Does nothing if the key doesn't exist
dictionary.update(key, value)

# already exists
# Does nothing if the key does exist
var x = dictionary.get_or_add(key, value)
# or more simply
dictionary.get_or_add(key, value)

# already exists
# normal behavior
dictionary[key] = value

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

4 participants