All Questions

Community

K
Jernej Kladnik

body_entered function argument type changed to Node in Godot 3.2

I've noticed that the body_entered function argument has been changed from PhysicsBody2D to Node in Godot 3.2. Since in this tutorial we're making sure only Player may enter the StompDetector via collision mask it is safe to assume that the body entering will have global_position property inherited from Node2D. But in case a node that does not inherit from Node2D would enter the area it would lead to a bug in the code. I am unsure what the reasoning behind the decision to modify this attribute type to Node was since when dealing with 2D games all 2D related nodes extend from Node2D.
  • L
    Margus Lillemägi

    Thank you for the very quick response and an excellent e-course!!!

  • I'll add this to a list of code changes in Godot 3.2. Thanks.

  • L
    Margus Lillemägi

    Same Node issue in Godot 3.2 here. I found a solution in YouTube. Not sure if it is correct, but it works.

    func _on_StompDetector_body_entered(body: Node) -> void:
    
    	var physics_body: = body as PhysicsBody2D
    
    	if physics_body.global_position.y < get_node("StompDetector").global_position.y:
    
    		get_node("CollisionShape2D").disabled = true
    
    		queue_free() 
    
  • K
    Jernej Kladnik

    Thank you for explanation. Seems the difference between 3.1 and 3.2 branches is in type hinting for body entered signal in area_2d.cpp:

    3.1:
    ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D")));
    
    3.2:
    ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
    
  • Correct

    A Node cannot enter an area, nor can a Node2D. bodies are necessarily PhysicsBody2D, so you shouldn't have an issue.

    If you do, you can always add an extra type check in the callback method:

    if not body is Monster:
        printerr("Some error message for debugging: %s" % body.name)
        return