`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”)}
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.
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)
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
#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
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
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