How to set my custom cursor back to normal with a right click?

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

Greets!

That in a button script. If you know and answer, that’s cool!

:bust_in_silhouette: Reply From: Sween123

Use Input.set_custom_mouse_cursor(YourImage)
To set it back to normal, just pass a null image: Input.set_custom_mouse_cursor(null)

Thxs, that i know, but i want it to execute in a button script, after a right mouse click:

var cursor = load("res://Textures/Icons/UseC.png")

func _on_Use_pressed():
	Input.set_custom_mouse_cursor(cursor)

That’s my code changin the cursor to my custom one after the button is pressed, but i’d like to know the syntax in the function to return it normal after a right mouse click.

Syl | 2020-02-12 08:54

There’s signal “gui_input”, if you want to handle multiple different input at the same time, connect the signal to the method:

_on_Button_gui_input(event):
    if event is InputEventMouseButton and event.pressed:
        if event.button_index == BUTTON_LEFT:
            Input.set_custom_mouse_cursor(cursor)
        elif event.button_index == BUTTON_RIGHT:
             Input.set_custom_mouse_cursor(null)

Also, turn on the “Mouse Right” in the property “Button Mask”. Otherwise when you right click the button, it will not be in the state “pressed” while the mouse cursor is set back to normal.

Sween123 | 2020-02-12 09:54

Misunderstandin, sry. What i mean is to have my cursor changed when the button is pressed, with a right click to set it back to normal whenever wanted. See?

Syl | 2020-02-12 10:10

Yes, that’s what I mean. If you right click on the Button, the cursor will be back to the original. Otherwise if it’s a left click, it will set the cursor to your custom cursor.

Sween123 | 2020-02-12 10:19

No, i mean cllickin normally with a left click on the button will set the custom cursor, and another function, and after that i want it to be able to change back to normal with a right click.
It’s a ‘Use’ button, as in point and click adventure game, the cursor becomin a hand the time to select the zone or the item to use, and with nothin more to use, a right click settin it back to normal.

Syl | 2020-02-12 10:43

Don’t get it… You mean you want the mouse cursor back to normal once you released the mouse? (only showing custom cursor while pressing down the mouse)
Anyway, Input.set_custom_mouse_cursor(null) will remove your custome cursor.
When you want to remove that and what triggers this action is according to your own game world.

Sween123 | 2020-02-12 11:16

Ok, what i was i sayin is that i want to set my custom cursor back to normal with a right click, after it has been changed by a pressed button.

Syl | 2020-02-12 11:28

You mean once you have pressed a specific button, you need to set your custom cursor back to normal?
Use a variable: var is_cursor_normal = false
(Enable “Mouse Right” in the Button’s “Button Mask” property)

func _on_Button_pressed():
    is_cursor_normal = !is_cursor_normal
func _input(event):
    if event is InputEventMouseButton and event.pressed:
        if is_cursor_normal:
            Input.set_custom_mouse_cursor(null)
        else:
             Input.set_custom_mouse_cursor(cursor)

Don’t know exactly what you want. Is “normal” the original mouse cursor?
It’s about coding, Input.set_custom_mouse_cursor(cursor) will set the cursor to the original one if you pass a null arguement or update it to the new cursor. When and how you want to change the cursor is about how you code. Please give a clear question.

Sween123 | 2020-02-12 12:38

More or less yes. i mean Once the button is pressed, that will change the cursor to custom (a hand to activate special items), and to go back to original i just need a right click when i’ve finished usin the hand. See?

Syl | 2020-02-12 13:29

I see.
So when Button is pressed, update the cursor to your custom cursor.
Create a method that will be called when an item is clicked. (You can use a signal) Inside this method, if it’s right click, set the cursor back to what you used before. It’s pretty straight forward.
If you want it back to normal any time when you right click, it’s simply this:

func _input(event):
    if event is InputEventMouseButton and event.pressed:
        if event.button_index == BUTTON_RIGHT and is_in_state_selecing_item:
             Input.set_custom_mouse_cursor(null)

Sween123 | 2020-02-12 13:34

I’m able to change to the custom cursor, what i need is the syntax to get it back to original with a right click, but inside the script of my button. Here it is so far:

extends Button

var cursor = load("res://Textures/Icons/UseC.png")

func _on_Use_pressed():
	Input.set_custom_mouse_cursor(cursor)

What should i add so that with a right click the player could choose to return to original cursor and end the ‘use’ selection?

By the way, if you could also tell me the syntax to trigger areas2D only with this custom cursor? :slight_smile:

Syl | 2020-02-12 13:52

There are signals like mouse_entered() and mouse_exited() for Area2D.
Create a variable to keep the track of the state of your cursor (If it is selecting, or if it is in normal state)
Still, add this function: (is_in_state_selecting_item should be a variable you create in script)

func _input(event):
    if event is InputEventMouseButton and event.pressed:
        if event.button_index == BUTTON_RIGHT and cursor_state == "selecting":
             Input.set_custom_mouse_cursor(null)
             cursor_state = "normal"

In function _on_Use_pressed():

func _on_Use_pressed():
    Input.set_custom_mouse_cursor(cursor)
    cursor_state = "selecting"

Sween123 | 2020-02-12 13:59

Here we are! Thxs so much for your help, and patience! :slight_smile:

Syl | 2020-02-12 14:40