JInput Multiple Controllers?

1.5k Views Asked by At

i'm somewhat new to jinput and java in general and was wondering, what's the easiest way to set up multiple xbox 360 controllers (particularly 4) with jinput? currently, i'm currently going off of theuzo007's tutorial on jinput with controllers, and have a basic working controller setup going on. it would be fantastic if i could set what controller moves certain entities around. (i'm using my friend's homemade library, just so you know.)

screenshot -

https://i.stack.imgur.com/HiJLy.jpg

  • top one is the main block of code, last one is the header (sorry for putting them in the wrong order, imgur does that sometimes!)

if anyone could help me out, that would be great, thanks!

edit: if there's no possible way to do it, if anyone could try to reccomend a new library to me, that would be cool.

1

There are 1 best solutions below

0
On

There is a possible way
That tutorial is pretty good. Furthermore, I think you can do the 4 controllers stuff by copy-pasting some code inside the zip theuzo007 provides you and a bit more. By the way, that page that you liked says that there is a better version of that tutorial where you can download an also better version of his code -> theuzo007's JInput tutorial V2

Once you download the code you can see that in JoystickTest.java there is a method called searchForControllers() that you can put (With the corresponding private ArrayList<Controller> foundControllers; as field) in a class called ControllerChecker or some cooler name. Make them all static and you will get something like this:

public class ControllerChecker {

private static ArrayList<Controller> foundControllers = null;

/**
*   Just used for checking all available controllers.
*/
private static void searchForControllers() {
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();

    for(int i = 0; i < controllers.length; i++){
        Controller controller = controllers[i];

        if (
                controller.getType() == Controller.Type.STICK || 
                controller.getType() == Controller.Type.GAMEPAD || 
                controller.getType() == Controller.Type.WHEEL ||
                controller.getType() == Controller.Type.FINGERSTICK
           )
        {
            // Add new controller to the list of all controllers.
            foundControllers.add(controller);

            // Add new controller to the list on the window.
            window.addControllerName(controller.getName() + " - " + controller.getType().toString() + " type");
        }
    }
}

/**
*   Returns null if there is no controller available. Otherwise, it retrieves the last controller in the list by removing it.
*/
public static Controller getController() {

    if(foundControllers == null) {
        foundControllers = new ArrayList<Controller>();
        searchForControllers();
    }

    return foundControllers.size() == 0 ? null : foundControllers.remove(foundControllers.size() - 1);
}

}

You would use the static method getController() to make the players have a different controller, checking if the returned controller is null, meaning that there is no available controller. Also you can change my code and check for controllers everytime you ask for one, but you have to check if the controller is already in use.

I hope this helps you in your purpose. This solution just checks for all available controllers and returns then in the last order it found them (maybe using a Stack is more efficient). But probably you will want more functionality like being able to tell the program to select a specific controller by pressing a button, maybe in a screen that says "Please, connect your controller and press any key/button". This can be achieve easily if you understand theuzo007's code (the JoystickTest.java has a lot of useful lines!).

Also you can make some mechanism to detect unpluged controllers and just by plugging in them again the system recognize it. Maybe there is some controller id, I haven't found it yet.

Finally, there is more code here.