Godot Version
4
Question
how do I use get_overlapping_body() in area2d I heard you can use it to tell if something is in the area2d or not but I don't know how to use it
4
how do I use get_overlapping_body() in area2d I heard you can use it to tell if something is in the area2d or not but I don't know how to use it
Maybe it would be more helpful if you described what you want to get done with get_overlapping_bodies()
? because typing get_overlapping_bodies()
is all you have to do to call the function.
I want to know when something is inside the area2d without using example func area_entered()
I heard that get_overlapping_bodies() is how you do that, but I’ve never really used something that said get, so I don’t know how to get true, false or a value from it. I don’t have any idea of how to get anything from calling get_overlapping_bodies(), I’m not sure what calling get_overlapping_bodies() does.
Let’s look through the documentation.
It’s return type is Array[Node2D]
so we will have to index or iterate the results. The description tells us the array will only contain physics bodies and tilemaps.
You can use Arrays in a for loop to apply the same operator for each element in the Array. This will print the name of each overlapping body.
for overlapped_body in get_overlapping_bodies():
print("hit: ", overlapped_body.name)
Back to what you are trying to do, can you give the big picture? What problem in your project has occured that you think requires get_overlapping_bodies()
to solve? You also mention func area_entered()
I think you mean the signal area_entered
, but that also does not get bodies it triggers when another Area2D enters the area, not physics bodies.
I want it for melee attacks, so I know when an enemy is in the area2d and when it not I have read the documentation and I’m still not sure how to use this
Cool, get_overlapping_bodies
can be used for this, if you have one frame you want to check hits you could make a function like so
func attack() -> void:
for overlapped_body in $MeleeHitbox.get_overlapping_bodies():
if overlapped_body is Attackable:
overlapped_body.take_damage(1)
so
func myattack()
if $meleeHitbox.get_overlapping_bodies():
print(“attack”)
so if I call get_overlapping_bodies and there is a overlapping body than it should print(“attack”) right?
also
whats → void:
mean
get_overlapping_bodies
returns an array of Node2Ds. If you’re only interested in whether there are any overlapping bodies or not, use has_overlapping_bodies
instead. That one returns a bool.
It means the method does not return anything.
Methods can return data. The -> void
indicates what data is returned when you call this function. void
means nothing is returned.
In your case, the get_overlapping_bodies()
returns all bodies that overlap with the area where you called the function. So you could do something like:
var bodies_in_melee_range = $meleeHitbox.get_overlapping_bodies()
now the returned data is stored in your variable bodies_in_melee_range
in form of an array. If you don’t know what an array is, you should read the docs: Array — Godot Engine (stable) documentation in English
Now you can do stuff like for example calling a hit method of all bodies in range (don’t know if this is useful to you, since this means potentially more than one body is hit at the same time)
for body in bodies_in_melee_range:
if body.has_method("hit"):
body.hit()
I would recommend against using get_overlapping_bodies(). You’re going to have to put it inside _physics_process() and you’re going to get a hit every frame, which means your weapon will get hundreds of hits every swing unless you add exceptions after the first collision, and then clear them after the attack is over. It’s possible, but messy.
Here’s a much easier way:
func damage(amount: int) -> void:
health -= amount
if health <= 0:
die()
func on_bod_entered(body: Node3D) -> void:
body.damage(damage_amount)
In this way you only detect the first collision, and there’s not a lot of code. If you hit multiple enemies at once, that’s handled. Also, your collision detection is handled by the GPU instead of the CPU, which is literally anywhere from thousands to millions of times faster than you coding it to be run by the CPU.
I didn’t know about has_overlapping_bodys how does that work it sounds like what I was really looking for
Returns true if intersecting any PhysicsBody2Ds or TileMaps, otherwise returns false
— Godot Docs
It’s probably not what you want since it doesn’t tell you what overlapped, only that something is overlapped.
its half of what I wanted. Can I do
$area2d.has_overlapping_bodies
print(“cheese”)
as in a character node has an area2d child node and I only want to the has_overlapping_bodys to return true for the area2d not the character node, will this do that and if not than how
does has_overlapping_bodys only return true or can I use as
has_overlapping_bodys
print(“something is overlapping”)
or what about cheese = has_overlapping_bodies
than would cheese == true or false depending on the has_overlapping_bodies
if you mean as an if statement like so, then yes this is a functional statement, but again it’s probably not what you want since it doesn’t tell you what overlapped, only that something is overlapped.
if has_overlapping_bodies():
print("something is overlapping")
With parenthesis yes, that’s how variable assignments work
var cheese: bool = has_overlapping_bodies()
# cheese will equal true or false
if cheese:
print("something is overlapping")