0 votes

Suppose we have

var Dict = {"A":{"TITLE1":"I am title 1."}}

How could I add another entry to Dict[A]? I cannot do:

var Dict = {"A":{"TITLE1":"I am title 1.","TITLE2":"I am title 2."}}
asked May 15 in Engine by The_Black_Chess_King (265 points)

1 Answer

+1 vote
Best answer

Looks like you're trying to add a sub-key. This should give you an idea of how dicts work:

func _ready():
    var Dict = {"A":{"TITLE1":"I am title 1."}}
    print(Dict)
    print(Dict["A"]["TITLE1"])
    Dict["A"]["TITLE2"] = "I am title 2"
    print(Dict)
answered May 15 by jgodfrey (5,544 points)
selected May 15 by The_Black_Chess_King

Just another quick question which I am struggling, can I add more than 1 subkey at the same time?

I am trying to add a new subkey, but it keeps replacing it:

var Lib= {Page:{Category:CatName}}
Lib[Page][Category] = Content

I was trying to do category = {"A","B","C"} but always end up with just category = {"C"} the last value added. But I see that by your example the concept works, must be something bad in my code structure.

================================UPDATE:

Upon experimenting with dictionaries today after trying to fix a bug I had for 3 days -.-, I finally found the problem.

If you set a dictionary like:

var TITLE = loadedDate[0][2]#Just a example of a array stored with the Title

Library[TITLE_CATEGORY] = TITLE
Library[TITLE_CATEGORY][TITLE] = "Subtitle"

It will return a error like:

Invalid set index 'TITLE' (on base: 'String') with value of type 'String'.

Because TITLE was set only as a string, the loadedDate[0][2] is just a string. So I was trying to add a dictionary subkey to a string, :P

What I had to do with my code was:

Library[TITLE_CATEGORY] = {TITLE:"Subtitle"}

Now my TITLE is a dictionary and can be referenced through

Library[TITLE_CATEGORY][TITLE]

Don't know why that was so hard to understand at first :D

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 How to use this Q&A? before posting your first questions.