Using Godot 4, I want to make a point and click style game where:
- if the player clicks on an object (interactable), highlight the object,
- else, move the player to the clicked location.
I have a Camera2D
which move the player on click:
extends Camera2D
@export var controlled: Player
...
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
get_viewport().set_input_as_handled()
if event.is_pressed():
var pos = self.get_global_mouse_position()
controlled.clicked_to(pos)
And an Interactable
object in my game:
extends Area2D
class_name InteractableArea
...
func _on_input_event(viewport: Viewport, event: InputEvent, shape_idx: int) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
viewport.set_input_as_handled()
if event.pressed:
EventBus.interactable_clicked.emit(true, self)
else:
EventBus.interactable_clicked.emit(false, self)
Problem is: I want to make sure we try the event in the Interactable before trying to move the player.
According to this section of the doc, that will never work:
https://docs.godotengine.org/en/stable/tutorials/inputs/inputevent.html#how-does-it-work
Because Godot always tries the “_unhandled_input” (in my camera) before passing to the “Physics Picking Events” (which is the event passed to the Area2D).
How do you make a “move to mouse click”, while keeping the option to capture the click first to trigger other events?