How do I set a value in a dictionary/structure (For a inventory system)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder
:warning: Old Version Published before Godot 3 was released.

I want to make a inventory system, however the elif ladder i have set up currently is not automated enough, and needlessly lengthy

Here’s the current inventory system:

func addtoinv(item, value):#Inventory management, needs to be optimized better
	if item == "itemtest":
		if itemenabled.Itemtest == true:
			itemquantity.Itemtest += value
			refresh_hud()
			pass
		else:
			itemenabled.Itemtest = true
		pass 
	elif item == "coinage":
		if itemenabled.Coinage == true:
			itemquantity.Coinage += value
			refresh_hud()
		else:
			itemenabled.Itemtest = true

As you can see everytime I add a collectible item to my game, I have to have a ladder with itemenabled.the_item_I_want . Instead of checking if that item is there.
Now with my new system I am using itemenabled.has(the_item_I_want)

But I’ve hit a roadblock, I can detect the item, but I can’t add to the item. How can I add/change a specific structure variable, based on an outside variable.

The one i’m currently trying to make:

func addtoinv(item, value):
print(str(itemenabled.keys())+item)
	if itemenabled.has(item)==true:#collect the item.
		itemquantity.values().has(item) += value #add to item
		refresh_hud()
	elif itemenabled.has(item)==false:# enabled the new item.
		itemenabled = true#WIp change the item's stance

P.S. If your wondering why I have two structures, It’s because I want a pop up to occur when the player picks up an item for the first time.

You may save itemenabled etc. as arrays, e.g itemenabled = [item1, item2], so you may use Array.has(). If you want get property you may use get() but that isn’t best way to do it.

Kamil Lewan | 2017-06-04 22:59

:bust_in_silhouette: Reply From: lavaduder

Okay so there is dictionary.item, but i can just have dictionary[item] which allows for more creative room, including adding/subtracting variables in the dictionary. So my itemquantity now doesn’t need any variables in it, it will just be added in game.

As long as I declare a value before I add to it like so.

func addtoinv(item, value):#Inventory management
	if itemquantity.has(item) && itemquantity[item] > 0:#If the item is apart of the structure and it's enabled, then collect the item.
		set_dialog("Picked up "+str(item))
		itemquantity[item] += value
		refresh_hud()
	elif !itemquantity.has(item):#If the item is not apart of the structure, then play a text message
		set_dialog("I found a "+str(item)+" What Joy!")
		itemquantity[item] = 0
		itemquantity[item] += value
		refresh_hud()
	#Else the Item is not apart of the structure, then it is ignored
	print(str(item) + str(value))

Thank you Godot Docs.

lavaduder | 2017-06-06 18:22