How do I get the information out of a dictionary's subdict?

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

Hi, I’m attempting to make a keybinding menu loosely following this tutorial. I’ve got my actions in a .json file and store that in a dictionary within an autoload script. Within each action there’s a sub-dictionary(I think, I hardly understand what I’m doing). To keep this short, lets focus on the jump action and one joybutton(I have two for each input type). Here’s that key in the dict:
"gp_jump": { "primary_gp" : "JOY_BUTTON_A" },

Then I have a scene filled with sprites to show what button is active for each action and to it is a script. In that script I loop through the keys in the dict and if index == action_name(the name of the project settings keybinds). If I print index, I only get “gp_jump”(without “”). Not any of the keybinds, just the action name and nothing else.

Anytime I try to access any information within with something like i[primary_gp], I’m of course just getting errors because I can’t access the information further in. If I print the dictionary I get the entire thing printed, subdicts and all.


The idea is to then pass in the keybinding into the set_gp_button() function and use that to set the icon. I’d really appreciate any help. Thanks in advance!

In case the image doesn’t load. Here the code:

func find_this_key():
    for i in KeybindManager.key_dict:
       if i == action_name:
            set_gp_button(prim_gp_btn,  i.primary_gp)

func set_gp_button(what_button : Button, index_and_button_key):
    if index_and_button_key == JOY_BUTTON_A:
	what_button.frame = 4

Burloe | 2023-03-26 10:49

:bust_in_silhouette: Reply From: Enfyna

Godot documentation about dictioary doesnt tell you about nested dictionaries explicitly but when you have a problem with the basic data types in GDScript you can look in the python tutorials too.

GDscript syntax is like python (but it is not based from it) so it can help.

That’s a great tip, thanks. Hadn’t thought about doing that before. I’m trying to get the bare minimum working now by trying to access the nested values. Following the docs you got me to realize what I was missing. I had to do this:


I expected that KeybindManager.key_dict was unecessary because I was inside of it already in the for loop but now I see that’s wrong. I’m far from done but I know now how to proceed. Thank you so much for the help!

Burloe | 2023-03-26 12:06