Dictionary: how to save new value after using get() ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By haha-lolcat1

quick example:

var dict = {
	"one" : "1"
}

func _ready():
	print (dict.get("two", "2") ) #will print "2"
	print (dict.has("two")) #still returns false
	print (dict) #will print {one:1}

in this example, I am able to set a new value using get() but it obviously won’t save it in the dictionary.
is there a way I can create and save a new value in the dictionary using get() or maybe another way should be used?

:bust_in_silhouette: Reply From: Wakatta

Its cute that you want to invent new ways to interact with a Dictionary but

# This is strictly how you set values
dict.key = "value"
dict["key"] = "value"
dict = {"key" : "value"}

The purpose of get is for comparisons sake or when you need a defualt value

var dict = {
    "one" : "1"
}

func _ready():
    if dict.get("two", "0") != "0":
        print (dict.two)

    $Label.text = dict.get("two", "2")

this works!

thank you for the explanation

haha-lolcat1 | 2021-12-04 17:15