Just to be clear, in your code you’re using MOUSE_MODE_CAPTURED
, which is what you want to use to hide the mouse. The CONTAINED
version doesn’t exist.
I have a different solution to recommend. Separate out the mouse and gamepad processing to two different files that just handle that and then update your inputs so that they get the same raw data from either input, and you can query which input device it came from (if you care). Then you don’t need to turn one or the other off and on, and you don’t drop inputs. It also allows you to change things like your UI that tells you what button to press. If you move your mouse, those inputs pop on screen - if you move your gamepad joystick, those inputs replace them immediately.
For example, here’s how I handle controls for looking in first person and third person 3D games:
mouse.gd excerpt
Handles mouse movement and funnels it to Controller class.
func _unhandled_input(event: InputEvent) -> void:
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
Controller.look = -event.relative * sensitivity
gamepad.gd excerpt
Handles gamepad joystick movement and funnels it to Controller class.
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventJoypadMotion:
if event.axis == JOY_AXIS_RIGHT_X:
Controller.look = Vector2(-event.axis_value * horizontal_look_sensitivity, Controller.look.y)
if event.axis == JOY_AXIS_RIGHT_Y:
Controller.look = Vector2(Controller.look.x, -event.axis_value * vertical_look_sensitivity)
controller.gd excerpt
Has a look variable the Player can call every frame without having to do the work itself. Also monitors input being used and lets anything interested know if inputs change. Allows a call for the last input type used and a function that can get the icon related to any action mapped. (Which delegate back to the Gamepad, Mouse and Keyboard classes to get the icons.)
## Allows anything listening (like UI) to know when the input method being
## used changed for something new. Primarily for changing what interaction
## hints and control icons are shown on screen.
signal input_method_changed(last_input_type: LastInput)
## Enumerates the kinds of inputs that are possible.
enum LastInput {
KEYBOARD_AND_MOUSE,
GAMEPAD
}
## Stores the amount of movement in the x/y direction that the player is trying
## to look in a 3D game.
var look := Vector2.ZERO
## Stores the last input used by the player for UI interaction hint updates.
## Private so that it cannot be modified extrenally. This is a read-only value
## outside this singleton. (Or would be if GDScript enforced that.)
var _last_input_type := LastInput.KEYBOARD_AND_MOUSE
## Updates the last input type for use throughout the game.
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton or event is InputEventMouseMotion or event is InputEventKey:
if _last_input_type != LastInput.KEYBOARD_AND_MOUSE:
input_method_changed.emit(LastInput.KEYBOARD_AND_MOUSE)
_last_input_type = LastInput.KEYBOARD_AND_MOUSE
elif event is InputEventJoypadButton or event is InputEventJoypadMotion:
if _last_input_type != LastInput.GAMEPAD:
input_method_changed.emit(LastInput.GAMEPAD)
_last_input_type = LastInput.GAMEPAD
## Returns the last input type used by the player.
func get_last_input_type() -> LastInput:
return _last_input_type
## Returns the correct Texture2D representation of the action passed based on
## the last input type used by the player - which can be specified. (Useful when
## the type changes and you want to update the UI immediately - resovles race conditions.)
func get_action_icon(action_name: String, input_type: LastInput = _last_input_type) -> Texture2D:
var events = InputMap.action_get_events(action_name)
for event in events:
if input_type == LastInput.KEYBOARD_AND_MOUSE:
if event is InputEventKey:
return Keyboard.get_key_icon(event)
if event is InputEventMouse:
return Mouse.get_mouse_icon(event)
else:
if event is InputEventJoypadButton or event is InputEventJoypadMotion:
return Gamepad.get_gamepad_icon(event)
return null
Anyway, just a thought. If you want to see all the code, I created a plugin that I use for my game jam games. I just finished a jam and will be making a number of updates soon from what I learned. Dragonforge Controller 0.10.1 I made it for myself, but figured it might help others in the community at some point. I actually just used it for a 2D game most recently. Was great for the UI.