How to disable mouse impacting FPS controller when a GUI is open?

Godot Version

4.3

Question

I have a basic ImGui displayed on toggle and I want to disable my FPS controller mouse input when the Gui is open. I have a FPS controller (mouse is “attached” to the center of the screen like typical FPS) and when I toggle the Gui I set MOUSE_MODE_VISIBLE but the mouse still controls the camera movement as well as the cursor.

Does anyone know if there is a simple way to disable the mouse inputs (including mouse buttons) when the Gui is toggled on? All I can come up with is some jank by referencing the camera and set_process_input but I haven’t gotten it to work either.

Here is my gui script for reference. Thanks for any help you can provide. Cheers!

extends Node

var ai_driver_on := true
var gui_visible := false

func _ready() -> void:
	Engine.max_fps = 120
	var io := ImGui.GetIO()
	io.ConfigFlags |= ImGui.ConfigFlags_ViewportsEnable
	# Capture the mouse cursor initially
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("toggle_gui"):
		gui_visible = not gui_visible
		if gui_visible:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		else:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _process(_delta: float) -> void:
	if not gui_visible:
		return
	
	if ImGui.Begin("AI Driver Control"):
		if ImGui.Checkbox("Turn AI Driver Off", [not ai_driver_on]):
			ai_driver_on = not ai_driver_on
		
		ImGui.Text("AI Driver is currently: %s" % ("On" if ai_driver_on else "Off"))
	ImGui.End()

# Add this function to handle game input when GUI is not visible
func _unhandled_input(event: InputEvent) -> void:
	if not gui_visible:
		# Handle your game input here
		pass
	else:
		# Consume input events when GUI is visible to prevent them from affecting the game
		get_viewport().set_input_as_handled()

This is not the script for the FPS controller, is it? Where do you handle aiming during normal play?

Add an if statement before ur mouse movement code.

if event is InputEventMouseMotion:
	if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED: return
		
	rotate_y(-event.relative.x * 0.001 * SENS)
	head.rotate_x(-event.relative.y * 0.001 * SENS)
	head.rotation.x = clampf(head.rotation.x, -PI/2.0, PI/2.0)

note: i used the inverse of the bool i wanted followed by “return” to reduce code nesting

1 Like

Thank you @dusk !!! :heart_eyes:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.