I want my mouse cursor to change depending on what it's hovering over. Are signals optimal?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kowid51384

I want the mouse cursor to change depending on what it is hovering over. It seems an object can tell if the cursor has entered it with the mouse_entered() signal.

But is that still optimal for a 2D RTS game that will have many units? I feel like it’d make more sense for the cursor to constantly check what’s underneath it, rather than have hundreds of units constantly check if the mouse cursor has entered them, but I am very new to Godot so maybe my understanding is wrong. Plus, I’ve saved the units as their own scenes and it doesn’t seem like I can connect their signals to the main scene.

Basic skeleton of what I was thinking:

extends Node2D

#Custom cursors
var cursor_default = load("res://Assets/RTSP_cursor_default.png")
var cursor_ally = load("res://Assets/RTSP_cursor_ally_hover.png")
var cursor_neutral = load("res://Assets/RTSP_cursor_neutral_hover.png")
var cursor_enemy = load("res://Assets/RTSP_cursor_enemy_hover.png")

func _process(delta):
	#getting the position of the mouse
	var hoverCheck = get_viewport().get_mouse_position()
	#Some code that would check if
	#there's anything remarkable at "hoverCheck" would go here.
	#If there is an enemy at "hoverCheck", then it would change the cursor.
		Input.set_custom_mouse_cursor(cursor_enemy)
	#However, a different cursor is set if it is an ally.
		Input.set_custom_mouse_cursor(cursor_ally)
	else:
		Input.set_custom_mouse_cursor(cursor_default)
:bust_in_silhouette: Reply From: mneg

Maybe not directly an answer to your question (optimization of mouse hovering back-end) but I really would just go for one of the two solutions and continue developing the rest of the game. When it seems to be too slow just replace it with the other one and see if it’s better.

However, I have a vague feeling that a constant world querying is more costly than using signals. Either way there has to be some search tree in the background which both querying and signals will be using. It probably depends on the signals being emitted anyways.

Anyway, what mouse_entered() are you talking about? I found the one of Control nodes but your entities won’t be Controls nodes…?

:bust_in_silhouette: Reply From: grognard3

Like Mneg suggested, just add a mouse_entered() and mouse_exited() signal to each of the unit scenes. Whenever the mouse enters a unitbox, the signal will tell the cursor to change based on that unit’s information (attack cursor for enemies, select cursor for allies…) when the mouse leaves the unit, you can reset the cursor or do another cursor check with the mouse_exited() signal.