0 votes

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: https://imgur.com/a/niAvV0T


asked Jun 13 in Engine by Lenquist (12 points)

1 Answer

0 votes

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?

answered Jun 14 by estebanmolca (1,199 points)

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!

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.