Gamepad connect/disconnect issue [SOLVED]

Gamepad detection was added some time ago in Defold 1.2.157. Currently, I’m having trouble getting it to work.

The triggers ‘gamepad-connected’ and ‘gamepad-disconnected’ don’t happen for me, though the gamepad works fine once connected. I’m using a generic Xbox style gamepad on Windows.

Triggers:

image

Detection code:

function on_input(self, action_id, action)
	if action_id == hash("gp_connect") then
		self.gamepad = true
	elseif action_id == hash("gp_disconnect") then
		self.gamepad = false
	end
end

Am I missing something?

I tried this script with “Xbox 360 Controller for Windows”, and I saw
“connected” and “disconnected” on Defold Console.

Maybe you need msg.post(".", "acquire_input_focus") somewhere in your script?

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("gp_connect") then
		print("connected")
	elseif action_id == hash("gp_disconnect") then
		print("disconnected")
	end
end

Thanks for checking that for me.

I already have ‘acquire_input_focus’ in my init function and as I said, the other gamepad inputs work correctly, just not the connect/disconnect messages.

EDIT: I’ve just realised the messages are only designed to occur when the gamepad is connected or disconnected during runtime. There’s no way to detect if a gamepad is connected at startup so I can’t do what I want anyway.

I tried again

  1. First, connect my Xbox Controller with PC
  2. Then choose Debug → Start/Attach on Defold Editor

But seems that connect event is working.

Hmm, why.

INFO:DLIB: Log server started on port 51865
INFO:ENGINE: Target listening with name: DESKTOP-NQMC88H - fe80::ac0e:c1f1:b8a3:dd67 - Windows
INFO:ENGINE: Engine service started on port 51866
INFO:ENGINE: Defold Engine 1.2.189 (8894457)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device ‘default’
DEBUG:SCRIPT: Listening for debugger on 0.0.0.0
DEBUG:SCRIPT: Debugger connected from 127.0.0.1
DEBUG:SCRIPT: connected <<======================== This came from my print(“connected”)
INFO:DLIB: SSDP: Started on address 172.19.0.1
INFO:DLIB: SSDP: Started on address 192.168.0.19
INFO:DLIB: SSDP: Started on address 192.168.56.1

I get a connected input event for each gamepad that was already connected before I run the game. They get sent right after init, before the first update. Sounds like something is buggy with it on your machine.

Yes, this is how it is designed and how it should work on all platforms.

@benjames171 which platform? Html5?

Thanks @jgoamakf @ross.grams it’s fixed now.

My fault, as usual! Had an ‘if’ statement in my ‘on_input’ function that prevented the code above being reached at all.

4 Likes