Can someone please tell me how to fix this line of code?

Godot Version

4

Question

`I need help with making a system for item picking up and dropping. What I’m having trouble with is changing values from my equipped item. The item has this var and give this var to the player when it gets picked up:

Blockquote

var item_info = {
“name”: “sword”,
“scene”: preload(“res://scenes/plastic_sword.tscn”)}

Blockquote

And I’m dropping it like this from the player:

Blockquote
func drop_item():

var dropped_item = item_data["scene"]
dropped_item = dropped_item.instantiate()
dropped_item.item_info = item_data.duplicate()

var drop_offset = Vector2(7, 0).rotated(rotation)
dropped_item.global_position = global_position + drop_offset

get_tree().current_scene.add_child(dropped_item)

Blockquote

But it keeps saying that dropped_item doesn’t have item_info as a value and thats not right because it does. This is my only problem, if I know how to do this then I should be able to edit values from the item by changing the dropped_item info later.

Not sure if that blockquote is how your meant to show code or not

Can you show full code of of item_data and item ?

Blockquote
extends Area2D #the item

var item_info = {
“name”: “sword”,
“damage”: 10,
“rarity”: “common”,
“scene”: preload(“res://scenes/plastic_sword.tscn”)
}

func _ready():
add_to_group(“items”)

func left_grab(player):
print(“Hit by raycast”)
player.left_item_held(item_info)
queue_free()

Blockquote

This is some more player code; I left unneeded things out

Blockquote

func left_item_held(item_info):
item_data = item_info

Blockquote

	if Input.is_action_just_pressed("leftclick"):
		var hit_object = handcast.get_collider() 
		if hit_object and hit_object.is_in_group("items"):
			hit_object.call("left_grab", self)

I don’t see how is declared item_data in the code you show me so it’s hard to debug

What you should do is give explicit type to your variables so it’s easier to read and debug :wink:

For instance your item:

class_name Item  # <================ ADD THIS
extends Area2D #the item

var item_info = {
“name”: “sword”,
“damage”: 10,
“rarity”: “common”,
“scene”: preload(“res://scenes/plastic_sword.tscn”)
}

func _ready():
add_to_group(“items”)

func left_grab(player):
print(“Hit by raycast”)
player.left_item_held(item_info)
queue_free()

And here

var dropped_item = item_data["scene"] # Unknown type ???
dropped_item = dropped_item.instantiate() # If you instantiate you suppose it's a PackedScene
# Here the there is probably a silent error from godot
dropped_item.item_info = item_data.duplicate() dropped item is probably null

var drop_offset = Vector2(7, 0).rotated(rotation)
dropped_item.global_position = global_position + drop_offset

get_tree().current_scene.add_child(dropped_item)

You can do:

var dropped_item: Item = item_data["scene"].instantiate()

Then if there is a problem wioth instantiation Godot will tell you directly

Blockquote
#item code sends the item_info to the player
func left_grab(player):
print(“Hit by raycast”)
player.left_item_held(item_info)
queue_free()

Blockquote

Blockquote
#player then turns that item_info into item_data
func left_item_held(item_info):
var item_data = item_info

Blockquote

Btw if you want to show code properly you can use “crtl+e” here (Preformatted text) :wink:

1 Like

what is Item? why is it green?

#item code sends the item_info to the player
func left_grab(player):
print(“Hit by raycast”)
player.left_item_held(item_info)
queue_free()
#player then turns that item_info into item_data
func left_item_held(item_info):
var item_data = item_info

Here, as you don’t specificy explicitly the type of your variables it’s really hard to debug
item_info could be a Node3D, an int, a String, there is no way to know so it’s error prone

Keep the control of your variables by specifying explicitly your variable everywhere

It’s the class name I suggested you to give to your Item in the code

Give me the full code of Item and Player it will be easier to debug because with just fragment it’s hard :stuck_out_tongue:

1 Like

No, I had shared it before. Here is it again

var item_info = {
“name”: “sword”,
“damage”: 10,
“rarity”: “common”,
“scene”: preload(“res://scenes/plastic_sword.tscn”)
}

ok, but the full code of the player will not be easier to debug so I will just snip somethings out that don’t affect this

Items code

extends Area2D


var item_info = {
"name": "sword",
"damage": 10,
"rarity": "common",
"scene": preload("res://scenes/plastic_sword.tscn")
}


func _ready():
	add_to_group("items")



func left_grab(player):
	print("Hit by ray from")
	player.left_item_held(item_info)
	queue_free()


players code…

extends CharacterBody2D

var item_data

func left_item_held(item_info):
item_data = item_info

func pick_up_item():
	var hit_object = raycast.get_collider() 
	if hit_object and hit_object.is_in_group("items"):
		hit_object.call("left_grab", self)

func drop_item():

	var dropped_item = item_data["scene"]
	dropped_item = dropped_item.instantiate()
	dropped_item.item_info = item_data.duplicate()

	var drop_offset = Vector2(7, 0).rotated(rotation)
	dropped_item.global_position = global_position + drop_offset

	get_tree().current_scene.add_child(dropped_item)

that should be it

This code should not compile, godot should tell you errors:
item_data is not defined in your function drop_item() and your function left_item_held() is empty

1 Like

why can’t they be empty and I tried to make the easier to read and broke it a bit, I have item_data as a var at the start, I’ll edit the code I’ve already sent

Here, this function does nothing as it is empty

no, it has item_data = item_info in it