–1 vote
var toggle = false

if Input.is_action_pressed("mouse_toggle"):
    toggle = !toggle
if Input.is_action_pressed("mouse_toggle") && !toggle:

Sorry if this is a stupid question, I'm new.

Godot version v3.5.stable.official [991bb6ac7]
in Engine by (40 points)

1 Answer

+1 vote
Best answer

Something like this should work:

var mouse_visible = true

func _process(delta):
    if Input.is_action_just_pressed("mouse_toggle"):
        mouse_visible = !mouse_visible
        if mouse_visible:
by (21,864 points)
selected by

It says that "'mouse_visible' isn't declared in the current scope"? And when I remove "func _process(delta):" the error is gone but then the whole thing doesn't work.

Also, how do I make the cursor not visible at first instead? Sorry but I didn't think of it and want it that way actually.

P.S. Sorry for the late response.

I tested the code I posted, so I know it work (other than the updated question regarding starting with the cursor NOT visible).

For the first issue:

It says that "'mouse_visible' isn't declared in the current scope"?

This code defines the variable at the scope of the entire script (and should be outside of any function). Did you not add this line?

var mouse_visible = true

Anyway, based on the new request to start with the cursor NOT visible, here's a cleaner way (so, a complete replacement for the code above)...

func _ready() -> void:

func toggle_mouse_visibility():
    if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
        Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
        Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

func _process(delta):
    if (Input.is_action_just_pressed("mouse_toggle")):

Here, all the work of toggling cursor visibility is handled by the toggle_mose_visibility() function. Each time it's called, the visibility is changed to the opposite of what it was previously. So, it's called each time the mouse_toggle action is triggered.

Additionally, it's called once from _ready() to set the initial state to HIDDEN

THANKS! This worked! And also I did put "var mouse_visible = true" outside of it, no idea why it didn't work.

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.