Godot Version
v4.2.1.stable.official [b09f793f5]
Question
Hello all!
I am creating a 2D point and click game, and working on finishing up my first scene.
In this scene, I have a player character and a bunch of objects that the player can interact with. Each object, typically, consist of an Area2D and a TextureRect.
I am using the Area2D and signals to check whether the player is in range when the character enters the Area2D. I am also using signals to check whether the player is hovering the object. When the player accesses or hovers, I toggle a variable in the script to allow for further interaction depending on the object.
All this works just fine, but in the end, it will be A LOT of identical signals, and I was wondering how to dry out signals.
What I’ve researched is that I can extend a script with another script that can contain these signals, and then connect them in each object script, but that hardly dries anything out.
So I was wondering if there are any idiomatic way of doing this?
I’ve added an example script. The signals and the process is reused and will be reused all over the game.
extends Control
@onready var locked: bool = true
@onready var allow_hover: bool = false
@onready var is_hovering: bool = false
@onready var textureRect: TextureRect = $TextureRect
func _on_area_2d_body_entered(body):
if body is CharacterBody2D:
allow_hover = true
func _on_area_2d_body_exited(body):
if body is CharacterBody2D:
allow_hover = false
func _on_area_2d_mouse_entered():
is_hovering = true
func _on_area_2d_mouse_exited():
is_hovering = false
func _process(_delta):
if is_hovering && allow_hover:
textureRect.mouse_filter = Control.MOUSE_FILTER_PASS
textureRect.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
else:
textureRect.mouse_filter = Control.MOUSE_FILTER_IGNORE
textureRect.mouse_default_cursor_shape = Control.CURSOR_ARROW