Check if Controller connected or not

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chain
:warning: Old Version Published before Godot 3 was released.

Hello!

I have a small tutorial in my game, which should tell the player how controlls work. There are not many, but it doesn’t hurt to show them anyway.

So my game supports controller and keyboard + mouse. If the player has a controller plugged in, I want to show him the controller controls and if not, I want to show him the keyboard + mouse controls.

Now I have something like this already implemented which checks the MOUSE MODE. It works fine, but can only detect the controller if a button or a joystick was moved or pressed.

Is there anyway to check if a controller is connected?

:bust_in_silhouette: Reply From: quijipixel

Use the signal joy_connection_changed from the Input object:

func _ready():
	Input.connect("joy_connection_changed", self, "_on_joy_connection_changed")

Then on the _on_joy_connection_changed method you can check wether it’s connected or not:

func _on_joy_connection_changed(device_id, connected):
	if connected:
		print(Input.get_joy_name(device_id))
	else:
	    print("Keyboard")

Thanks a lot. I wanted this for my shooter game that will have controller support

MysteryCoder456 | 2020-08-04 22:55

Hello! This works but if controller is plugged before opening the game it thinks that user is using keyboard. How can I check if controller is connected while entering the game for the first time?

bUguette | 2021-08-21 17:15

to bUguette

There is a function just for that: Input.get_connected_joypads()
If there are no controllers connected the return value’s gonna be an empty Array. If only one controller is connected it’s [0]. Godot docs link is here.

PAndras | 2023-06-11 08:55