I'm building a type of Pinball game using Godot 4.0. I've got two types of components relevant to this question:
- balls, represented as a
RigidBody2D
scene withSprite
and circularCollisionShape2D
children - circular bumpers, represented as a
StaticBody2D
scene withSprite
and circularCollisionShape2D
children
When a ball hits any object, that other object should be "in charge" (code-wise) of what happens; at least, to get some clean code. However, the body_exited
signal is only available on the balls' node type, but not available on StaticBody2D
.
I'd want to write (pseudo-code):
# In StaticBody2D BumperCircular.gd:
func _on_body_exited(body):
if is_ball(body):
# add bonus velocity because of the bump
I feel like I'm missing something, because it seems wrong to add body_exited
on the Ball and then dispatch/delegate back to the other body for logic on what happens next.
So I feel like I have to write:
# In RigidBody2D Ball.gd:
func _on_body_exited(body):
if is_bumper(body):
# logic for bumper coded inside this script
else if is_some_component(body):
# logic for component type 2 coded inside this script
else if is_some_component(body):
# logic for component type 3 coded inside this script
# etc.
# etc.
Or at best:
# In RigidBody2D Ball.gd:
func _on_body_exited(body):
if has_ball_collision_logic(body):
body.on_ball_collision_logic(this) # dispatch to the other object for logic
But both seem to me the wrong way around.
What am I missing here? Why is there no body_exited
on StaticBody2D
?
PS. I've searched this Stack Exchange site and online in general, plus I've peered through the (inheritance tree of the) StaticBody2D documentation but found no relevant info yet.