Adding Custom MouseCursor Shapes

Godot Version

v4.7.dev2.official [778cf54da]

Question

How does one add custom mouse cursor shapes?

The Godot docs say this is how changing the mouse cursor image using code could be done:

extends Node


# Load the custom images for the mouse cursor.
var arrow = load("res://arrow.png")
var beam = load("res://beam.png")


func _ready():
	# Changes only the arrow shape of the cursor.
	# This is similar to changing it in the project settings.
	Input.set_custom_mouse_cursor(arrow)

	# Changes a specific shape of the cursor (here, the I-beam shape).
	Input.set_custom_mouse_cursor(beam, Input.CURSOR_IBEAM)

I love this, but what if I want to have custom shapes for custom states? Like let’s say, hovering over an enemy spaceship whilst having a weapon selected, and you need to know if the shot will pierce their armour or not, with the cursor image. That sort of thing.

The same Doc page then goes on to say:

There are multiple mouse cursors you can define, documented in the Input.CursorShape enum. Which ones you want to use depends on your use case.

But checking there, I’m not sure how I’d add my own. Any help would be greatly appreciated!

By setting it on the ready method, is set for the entire live of the node. You have to change it based on the status you described. You could have a new method with a parameter like setCursor(type_of_cursor) and called based on that logic.

I understand that I would have methods connected using the hovered or focus_entered or mouse_entered signals on the nodes, and I understand how to match them to different uses of the set_custom_mouse_cursor() method, but how does one add more ENUM values to the already defined ones?

Or do I not need to?

For instance, if I had an icon for “impenetrable_armour,” then would I just do
Input.set_custom_mouse_cursor(armour_impenetrable), having defined it in the load() calls earlier?

Yes, you can use only the first parameter and create if you want to your own enum which will be sued as parameter for your custom method, or just pass the new state and have a switch or a dictionary with the state as key and the value as your preloaded cursor shape.

1 Like