0 votes

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?

Godot version v3.4.stable.official [206ba70f4]
in Engine by (95 points)

1 Answer

+1 vote
Best answer

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")
by (6,876 points)
selected by

this works!

thank you for the explanation

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.