Working with a dictionary

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

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.

Example:

match probability:
	1:
		dictionary_inv.itemDictionary = {
		numberSlot:{'itemEq':['weapon'], 
		'itemIcon':preload("texture_path"),
		 'itemName':'30_carabine',  
		'a':true, 
		'b':1000, 
		't':quantity, 
		'x':'ammo', 
		'id':randi()}
	}

*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 :smiley:
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 :confused:

Thanks in advance :3

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.

Tim Martin | 2020-09-26 19:26

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?

JerryHayat | 2020-09-27 06:29

:bust_in_silhouette: Reply From: Tim Martin

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):
   itemDictionary.erase(i)

To remove 10-15 for an array

for i in range(10, 15):
   itemArray.remove(i)

Many thanks! Everything worked out!

JerryHayat | 2020-09-27 17:41