Godot Version
v4.2.1 stable
Question
I have a c++ class called “Projectile” which inherits from Area2D. I would like my callback function on_body_entered
to fire whenever a body enters the area of “Projectile”. Currently I am using get_overlapping_bodies()
in my process function, which works fine (also with collision layer/mask), but I wanted to try and set it up with a callback because that seems like the intended way to handle collisions.
I see in the editor that Area2D has a signal called “body_entered(body: Node2D)” but I have no clue how to link that to my callback function. This is what the relevant parts of my class look like:
class Projectile : public Area2D {
GDCLASS(Projectile, Area2D)
protected:
static void _bind_methods() {
// Attempt 3
ClassDB::bind_method(D_METHOD("body_entered"), &Projectile::on_body_entered);
// Attempt 4
ClassDB::bind_method(D_METHOD("body_entered", "body"), &Projectile::on_body_entered);
public:
Projectile() {
// Attempt 1
connect("body_entered", Callable(this, "on_body_entered"));
// Attempt 2
this->connect("body_entered", Callable(this, "on_body_entered"));
}
void on_body_entered(Node2D *body) {
UtilityFunctions::print("Entered Body");
}
};
I cannot get my callback to fire on hitting objects, while get_overlapping_bodies()
does work on them. Printing does work, I use it in this class and all over. What am I doing wrong? Am I being a dumbdumb?