You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
The text was updated successfully, but these errors were encountered:
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)
I think instead of using an enum, a new method could be added
# Does nothing if the key doesn't existdictionary.update(key, value)
# already exists# Does nothing if the key does existvarx=dictionary.get_or_add(key, value)
# or more simplydictionary.get_or_add(key, value)
# already exists# normal behaviordictionary[key] =value
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:
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:Describe the feature / enhancement and how it helps to overcome the problem or limitation
add a
set
method to dictionaries that works similar toget
. we can use an additional optional field to define behaviour: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)
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
The text was updated successfully, but these errors were encountered: