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:
toggle_mouse_visibility()
func toggle_mouse_visibility():
if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
else:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func _process(delta):
if (Input.is_action_just_pressed("mouse_toggle")):
toggle_mouse_visibility()
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