Dictionary inside Dictionary returning null

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

Hi everyone,

beginner Godot developer her.

Can some one explain to me why this line is returning null:

var TempDictSub = TempDict.get(1);

TempDict content is: {1:{ID:1, Type:Normal}, 2:{ID:2, Type:Immediate}}

Screenshot from VS Code: Imgur: The magic of the Internet


:bust_in_silhouette: Reply From: estebanmolca

If I run it like this in gdscript:

var TempDict = {1 : {"ID" : 1, "Type" : "Normal"}, 2 : {"ID" : 2, "Type" : "Immediate"}}

func _ready():
	var TempDictSub = TempDict.get(1);

print: {“ID” : 1, “Type” : “Normal”}

You see very little code in the image, but it seems that your error is in the variables that make up your dictionary, or the dictionary itself is null, where do you define the dictionary? and the variables? is it gdscript or c #? If it’s c # I can’t help you much. It seems that the vs gives you an error (or is that red circle in the image a debug point?). Can you run the code in godot editor?

This is quite interesting. the TempDict is created like this:

TempDict [SomeOtherDic.get(“ID”)] = SomeOtherDict_2

Basically, I am taking another Dictionaries Value and putting it as the key

TempDict .keys() works fine but TempDict.has(1) returns false.

The way i solved it (more like a workaround) is that I convert the Value to a String before putting it as the Key of the TempDict Dictionary:

TempDict [str(SomeOtherDic.get(“ID”))] = SomeOtherDict_2

This seems to work for some reason. I would really like to know why this is, if someone can explain!

Lenquist | 2020-06-14 13:36