0 votes
    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 onAreabodyentered(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

asked Mar 30 in Engine by Okan Ozdemir (193 points)

1 Answer

+1 vote
Best answer

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.

answered Mar 30 by njamster (8,132 points)
selected Mar 30 by Okan Ozdemir

Thanks Mr.. You are a hard rock fellow citizen. getoverlappingareas() 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?

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.