Godot has a really great collision system. Its flexible and variable in detail. For my space trader game, I wanted to detect whether the player had entered a predefined area to show a menu for docking. When leaving the area, the popup will then close.
The space station owns the detection area and the player has a kinematic body. The implementation goes as follows:
- Add an Area2D node to the space station.
- Attach a CollisionShape2D to the Area2D
- Choose a shape and ensure the “Trigger” is on. Area2Ds can’t detect collision without a CollisionShape2D
- In the Player Attach a KinematicBody2D and like the Area2D node, a CollisionShape2D to that.
Now we can use these nodes to detect when the player enters the activatable area of the space station. In order to utilize that we must setup and capture the signals emitted by the Area2D on the space station.
The syntax of a signal is: signal_name(var parameter_passed_with_signal). The signals emitted by a node are viewable from the “Node” tab, next to the “Inspector” tab.
In order to receive a signal, it needs to be connected to a function that takes the same parameters that the signal passes (signals can also have no parameters). The above image shows that the “body_enter” signal from the spacestation’s Area2D node, is going to be connected to the spacestation node’s (itself) showDockPopup(player) function (line 11).
That is all there is to it. The connected functions will be triggered by the signals. In this case, they will show and close the docking-request popup.