0 votes

I am working with a dictionary again, and I want to implement writing several keys.

I already have several keys in my dictionary and I want to add some more keys. I generate them, but not the essence. In my code, they replace the old ones.


match probability:
        dictionary_inv.itemDictionary = {

*NumberSlot Generated in a specific range

I have several such keys and this one is approximate.
In the main dictionary I have 12+ ready-made keys
And this one replaces everyone

And a small additional question, in case the main one is solved :D
Can I delete keys in a specific range, for example 10 to 15? I know for sure that you can clear the entire dictionary, but this does not suit me :/

Thanks in advance :3

in Engine by (71 points)

Sorry, I don't quite understand the question.

Dictionaries map key->value. You have a nested-dictionary structure with an outer-dictionary stored at dictionary_inv.itemDictionary. This dictionary has 1 key-value pair, a key called numberSlot which holds an inner-dictionary.

The numberSlot inner-dictionary has 8 key-value pairs: itemEq->list, itemIcon->resource, itemName->string, a->boolean, b->integer, t->integer, x->string, id->integer.

To help with the first question. What is being replaced?

To help with the second question. Yes, you can delete a range of keys, but in order to delete keys range(10,15), the keys need to be integer. Your dictionaries have string keys.

My main goal is Numberslot. Common dictionary example

var itemDictionary = { 0:{'itemEq':['none'], 'itemIcon':preload("res://data/assets/rpgsystemCardinal/Itemss/ammo/shot_shell.png"), 'itemName':'shot_shell', 'itemStack':true, 'itemStackMax':1000, 'itemStackValue':1, 'itemType':'ammo', 'itemId':randi()}, 1:{'itemEq':['none'], 'itemIcon':preload("res://data/assets/rpgsystemCardinal/Itemss/medic/medbac.png"), 'itemName':'medbak', 'itemStack':true, 'itemStackMax':64, 'itemStackValue':1, 'itemType':'consumable', 'itemId':randi()} }:

It already stores 2 items under keys 0 and 1
I want to add another item through the script, under a different key, but it replaces the existing ones.

About 2 questions. That is, while I have a string in my dictionaries, I will not be able to delete them correctly?

1 Answer

0 votes
Best answer

Hello Jerry,

I think I have enough information to answer.

You have a dictionary, itemDictionary with two inner-dictionaries. Key 0 has a dictionary describing shot_shell, key 1 has a dictionary describing medbak.

If you do itemDictionary = ... you will replace the existing dictionary with a new one. And loose all of the existing keys.

To add a new item with the key 2, you would do

itemDictionary[2] = {'itemName':'another_item', ...}

Here ... is all of the other entries in the dictionary, itemEq, itemIcon etc...

Maybe better would be an array?

var itemArray = []
itemArray.append( {'itemName':'shot_shell', ...} )
itemArray.append( {'itemName':'medbak', ...} )
itemArray.append( {'itemName':'another_item', ...} )

Having either a dictionary with integer keys, or an array, will make deleting ranges easier than if the dictionary keys were strings.

To remove 10-15 for an integer-key dictionary

for i in range(10, 15):

To remove 10-15 for an array

for i in range(10, 15):
by (392 points)
selected by

Many thanks! Everything worked out!

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.