All Questions


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.
  • Nathan Lovato replied

    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" %
  • K
    Jernej Kladnik replied

    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:

    ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D")));
    ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  • L
    Margus Lillemägi replied

    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
  • Nathan Lovato replied

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

  • Nathan Lovato replied
  • L
    Margus Lillemägi replied

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

  • J
    Dana Johnson replied

    Just a suggestion here: I think Margus' suggestion to cast the Node to PhysicsBody2D should also be part of the API change note under the video.