1
\$\begingroup\$

I am currently making a game with the java library LibGDX and want to add XBox 360 controller support using the GDX-Controllers extension. At the moment I have a setup where I create a listener to listen for particular button presses, then attach that listener to the static Controllers class using the method: Controllers.addListener(myListener)

This method works fine for listening for button press events from controllers which were connected at the time the program started but, if I try connecting a controller after the game has started, events for this controller are not registered.

I cannot see from the source code a way to update the listener for new controllers, and it appears from this blog post that disconnects and re-connects may not be supported on the desktop.

My question is: Is there a solution to my problem using GDX-controllers and, if not, what are my options for getting full Xbox 360 controller support (Including controller connects and disconnects) If I want functionality which integrates with the libGDX framework?

\$\endgroup\$
  • \$\begingroup\$ Try recrating the listener. The interface that you create when you start the game querrys for controllers and then give them to you. if you connect one you have to recreate this devie or querry for new ones everyframe. \$\endgroup\$ – Tordin Dec 19 '13 at 13:28
  • \$\begingroup\$ @Tordin My problem is that I am not sure exactly what the process is of updating the Controllers manager. I have tried printing the result of Controllers.getControllers().size every frame but the number printed doesn't change, regardless of whether i connect more controllers or not, so clearly I am missing a step \$\endgroup\$ – Craig Innes Dec 19 '13 at 13:45
  • \$\begingroup\$ then it might bee that you have to recreate the device every frame. It might also be that per Windows Handle the driver only update it once it´s intalized. \$\endgroup\$ – Tordin Dec 19 '13 at 13:47
4
+50
\$\begingroup\$

Read the wiki!

Controllers connects/disconnects don't give listener events (right now), so controllers must be polled manually.

The wiki says controller listeners don't work on the desktop for connects or disconnects. I'm no expert, I've just been reading the wiki in detail, but it looks to me like you must poll the controllers manually.

This code will give you the currently connected controllers:

for (Controller controller : Controllers.getControllers()) {
    Gdx.app.log(TAG, controller.getName());
}

Some refresh type functionality would also be needed to get updated lists.

Edit: Based on the way the source is written, controllers are only detected when the control classes are loaded.

\$\endgroup\$
  • \$\begingroup\$ This would seem to be the correct answer, but I would like to see more a more complete solution. Care to add in some kind of slow-polling function? To really compensate for the missing functionality, I think that creating an event that could be subscribed to from throughout a project would be required. Come on, earn those 50 points! :) \$\endgroup\$ – Seth Battin Mar 30 '14 at 3:51
  • \$\begingroup\$ alright, I'll comb through the source and look for a way. I don't think it's possible with the backends that LibGDX uses though... \$\endgroup\$ – pensono Mar 30 '14 at 5:31
  • \$\begingroup\$ At the moment hot plugging is not supported on desktop. Sad thing. \$\endgroup\$ – Benedikt S. Vogler Oct 11 '14 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.