0 votes

i made this dictionary

var inv_data = {0: {"Item": "10009", "Stack": 1}}

it print this value

{0:{Item:10009, Stack:1}}

0 is the key for 1st value
i want to add a new value
so it will check first if the key exist then it will add a new key and assign to it the new value
can anyone help me with this?

Godot version 3.2
in Engine by (51 points)

1 Answer

+1 vote
Best answer

It really looks like an array of dicts would be a better solution here:

var inv_data = [{"Item": "10009", "Stack": 1}]
print (inv_data[0])
inv_data.append("Item": "10010", "Stack": 1})
print (inv_data[1])

But if you really want to use a dict of dicts, you could do it like this:

var inv_data = {0: {"Item": "10009", "Stack": 1}}
print (inv_data[0])
inv_data[inv_data.size()] = {"Item": "10010", "Stack": 1}
print (inv_data[1])
by (1,742 points)
selected by

it worked Thomas, Thank you very much.

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.
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 webmaster@godotengine.org with your username.