Nonexistent function 'pick_up_item' in base 'KinematicBody2D(player.gd'

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quadra123
func _input(event):
if event.is_action_pressed("pickup"):
	if $Area2D.items_in_range.size()>0:
		var pickup_item = $Area2D.items_in_range.values()[0]
		pickup_item.pick_up_item(self)
		$Area2D.items_in_range.erase(pickup_item)

This is player.gd

var player = null     
var being_picked_up = false 
func _pick_up_item(body):
player = body
being_picked_up = true

This is item.gd

var items_in_range = {} 
func _ready():
pass
func _on_Area2D_body_entered(body):
items_in_range[body]= body
func _on_Area2D_body_exited(body):
if items_in_range.has(body):
	items_in_range.erase(body)

This is Area2D script.
This is the tutorial I was following:https://www.youtube.com/watch?v=ssYqIQQPt7A&t=0s

When I press key for “pickup”, i got "Nonexistent function ‘pick _ up _ item’ in base ‘KinematicBody2D(player.gd’

Also does anybody know what does this mean?

func _on_Area2D_body_entered(body):
  items_in_range[body]= body
:bust_in_silhouette: Reply From: jgodfrey

Looks like you’re trying to call a function named pick_up_item, but your function is actually named _pick_up_item. Notice the leading underscore. The two names need to be the same.

Actually i got error saying i should add _.

quadra123 | 2022-02-20 09:32

:bust_in_silhouette: Reply From: Inces

That means Player tries to pick himself up. You didn’t exclude it from your “items in area” array. You should check what class is picked object before picking it up or before detecting it in area. I recommend designing whole separate class for ITEM, so You will be able to do:

 if pickup_item is ITEM

I think it has something to do with the AREA2D script. Do you know what does this mean?

func _on_Area2D_body_entered(body):
items_in_range[body]= body

quadra123 | 2022-02-20 09:33

No, this script is good - whatever enters Your Area2D is collected in “items_in_range” dictionary. You want to collect pickable items like this, but player itself also is inside this Area2D, and he is treated like pickable item. So You need to make this Area2D collect only objects that are items, You need to exclude player there. For example :

 func _on_Area2D_body_entered(body):
             if body != player:
                     items_in_range[body]= body

You need to reference player correctly, or check his class using IS keyword

Inces | 2022-02-20 12:11