Need help with code

Godot Version

4.2.1

Question

I’m trying to do an inventory equipment system, but it’s not working. When I equip an item it equips it but also leaves one in the inventory. Here’s the full code

At a glance, because it’s not that easy to just read code and find bugs, but I think (might be wrong) that you expect this

item_data = {
					"Name": "",
					"Quantity": null,
					"Type": "",
					"Rarity": "",
					"Lock": "False",
				}

from func _on_inventory_slot_input(event, item_data, texture_path_node):

To clear the item from inventory? It does not, as it is just a reference and not the same object. You need something like _set_inventory_item_data(item, data)

I see i just checked and you’re right. Thank you.

I’m sorry, but I am not sure why this actually worked.
A reference to the object is the object and therefore anything you do to the reference gets done to the original.

Built-in types are stack-allocated. They are passed as values. This means a copy is created on each assignment or when passing them as arguments to functions. The only exceptions are Arrays and Dictionaries, which are passed by reference so they are shared

In the code item_data is a dictionary and therefore it is passed into the function as a reference.
Therefore anything you do to that parameter is reflected in the variable you used to when you called it.

If the code now works it is for another reason.
Long story short: working with a reference to an object == working with the object.

Note the following code:

func moo(a:Dictionary)->void: 
	printt(a,2)   
	a["goodbye"] = "soul"
	printt(a, 3)
func _ready() -> void:
	var d:Dictionary = {"hello":"world", "goodbye":"heart"}
	printt(d,1)
	moo(d)
	printt(d,4)

This is the output:

{ "hello": "world", "goodbye": "heart" }	1
{ "hello": "world", "goodbye": "heart" }	2
{ "hello": "world", "goodbye": "soul" }	3
{ "hello": "world", "goodbye": "soul" }	4

I’m sorry for the confusion. I meant @J.G.S pointed me in the right direction with the set part

I just did this PlayerData.player_status[“Inventory”][str(i)] = item_data

item_data is just the dictionary being pulled.

I had forgotten to set the inventory slot that is being clicked.

1 Like

Hi, sorry for the late answer.

Yes, but the reference is broken when you re-assign the variable.

item_data = {/../}  # no longer a reference as it is reassigned

now item_data is no longer referencing the passed parameter.

This however woud have worked as it reference the data held by the reference:

item_data.Name = ""
item_data.Quantiy = null
# etc
1 Like

Yes, I see what you mean now.
Unfortunately its too late to edit my other post.
For the benefit of anyone who comes here the connection between the passed parameter and the parameter variable can be broken by reassigning the parameter:

func _ready() -> void:  
	var d:Dictionary = {"hello":"world", "goodbye":"heart"}
	printt(d,1)
	moo(d)
	printt(d,4)
	pass
func moo(a:Dictionary)->void: 
	printt(a,2)   
	a = {}
	printt(a, 3)

This outputs:

{ hello: world, goodbye: heart }1
{ hello: world, goodbye: heart}2
{  }3
{ hello: world, goodbye: heart }4