How to pick up Item ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By markelliot_94

I have a Coconut with an Area node on it and I want to walk over to it with my Character press ‘space’ bar when the characters collision shape has entered the Coconuts area and have the coconut disappear as if it was added to an inventory. I have my signals set up so as the character enters and exits the area it Prints (entered) and (exited), I know this is simple im just missing something inbetween if i were too add If_action_is_pressed and Queue_free. Heres my script

extends Area

signal in_pickup_range
signal out_pickup_range

func _on_Coconut_body_entered(body):
print (“Entered”)

func _on_Coconut_body_exited(body):
print (“Exited”)

“Like i want it to go, If character enters area, press space bar and coconut will disappear”

:bust_in_silhouette: Reply From: andersmmg

I would probably just create a variable to store the last entered object. Then, when the spacebar is pressed, collect the last item if there is one. Then just set the last item to null when there is no item.

I think i just don’t know enough of the syntax to make it happen i’ll get there

markelliot_94 | 2020-04-01 15:53

:bust_in_silhouette: Reply From: njamster

I would do it the other way around and add an Area2D - I’m assuming you’re working in 2D here, but all things discussed here should apply to 3D as well - to the player! Then your player-script would need to contain something like this:

func _physics_process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		for body in $Area2D.get_overlapping_bodies():

To avoid freeing other bodies on accident, consider adding your coconut-scene to a group and check if a body belongs to that group before freeing it:

func _physics_process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		for body in $Area2D.get_overlapping_bodies():
			if body.is_in_group("coconuts"):

So put the Area node on the Character, do I leave the Area node on the coconut aswell, as to have 2 area zones overlapping or should I just have the physics body on the coconut and my characters area reads when it’s in the zone of the coconuts physics body, I was looking towards _overlapping_bodies(): , I need to read more into them, Don’t fully understand their parameters. every reply is a big help thank you.

markelliot_94 | 2020-04-02 02:41

So put the Area node on the Character

Correct. It represents an area around the character where it will detect other bodies. These can be NPCs, enemy characters or - as in our case - items on the floor.

do I leave the Area node on the coconut aswell

For my code to work, you don’t have to. it check’s for overlapping bodies. However, you can also check for overlapping areas with get_overlapping_areas().

Calling these functions on an Area2D will just give you an array, filled with all bodies / others areas present in that Area2D at this moment in time. So the output will change depending on when you’re calling it. You can then loop over the entries and check out each one of them individually, like I do in my code with the groups.

njamster | 2020-04-02 09:52

Awsome It works great, that helps alot,I was way over complicating a simple thing I think one of my problems is coming in with no coding exp. at all expecting to get by with just online tutorials, I’ve started learning Python on the side to give me a better understanding of how everything works, I was going to use unity because they had more tutorials but couldn’t see my self not using Godot as I love it so much it’s just my lack of understanding. cheers for the feedback

markelliot_94 | 2020-04-03 02:49