This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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)
Godot version v3.3.4.stable.mono.official [faf3f883d]
in Engine by (43 points)

2 Answers

0 votes

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..?

by (74 points)
0 votes

Like Mneg suggested, just add a mouseentered() and mouseexited() 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.

by (48 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.