How to get what Area collides, I need the name of it, like the one with RayCast.get_collider()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Okan Ozdemir
	var x = $InteractionRayEnabled.get_collider()

the above code is great for Raycast! The below code should be sth. like this, but doesn’t work

	var xy = $Area.connect("body_entered", emit_signal("body_entered"), "body_name")

Of course I can get the body name in a function :

func _on_Area_body_entered(body):

if not taken and body is RigidBody and body.get_name() == "Player":
	
	Global_batch.P1points += 100

but I want it in a line please

:bust_in_silhouette: Reply From: njamster

The below code should be sth. like this, but doesn’t work

Of course it doesn’t work! You’re completely ignoring how connect works:

  1. The second argument is supposed to be the target-instance (where the callback is called) but you’re passing a function call that returns nothing instead.
  2. The third argument is supposed to be the name of the callback. From your example it’s not entirely clear if body_name is a variable, a function or even declared at all, but I’d guess it’s not the name of a function, right?
  3. connect() returns an Error, so xy won’t contain the collider!

Of course I can get the body name in a function

And this is how you’re supposed to do it. What’s your issue with that?

but I want it in a line please

You could use get_overlapping_areas() and get_overlapping_bodies(). That will give you an array of all overlapping areas/bodies. You’d still need to step through this on your own though. Also be aware of this part of the documentation:

For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved.

Thanks Mr… You are a hard rock fellow citizen. get_overlapping_areas() not that I know of. Now I will proceed thanking you again.

Why can we get so simple with Raycast

var x = $InteractionRayEnabled.get_collider()

but not with Area and its collision shapeI dont understand

Your second question as the below is that you are right. It is not the name of the function. I try to mimic func something(body): which is not existing yet

a function or even declared at all, but I’d guess it’s not the name of
a function, right?

Okan Ozdemir | 2020-03-30 11:03

Why can we get so simple with Raycast but not with Area

It’s as close as it gets. Unlike a RayCast, an Area can collide with multiple objects at the same time. So you have to return a list. If you’re only looking for a specific node, you can use overlaps_area(Node) and overlaps_body(Node).

It’s worth mentioning that the RayCasts collider is also only updated once during each physics step. Unlike for the Area, you can force it to update in between though, by using force_raycast_update(). So the difference isn’t that big!

njamster | 2020-03-30 11:25