Game Development Community

Count the number of joysticks attached?

by Chris Jorgensen · in Torque Game Builder · 10/03/2006 (9:21 pm) · 7 replies

I thought I once saw someone list how you can detect how many joysticks are connected to the computer in TS. Basically, I want my game to count how many joysticks are attached (0 to 4) and then allow a player to select which joystick he'd like to use. Thus far, all I've found is "isJoystickDetected()" but that only tells me if they're connected, not how many.

Does anyone know how to do this?

#1
10/04/2006 (12:47 am)
I posted a hacky sort of way to count the number of connected joysticks/gamepads a week or so ago, but since the search is borked, I can't actually find the thread (nor can I find the resource with the proper command I used).

Anyways, the gist of it was that one of the commands available is something like:
getJoystickPosition()
or something to that effect. Basically it returns the x and y values of the stick (ie, x would be 1.0 if you're holding the stick all the way to the right). The hacky bit come in here:
If the specified joystick (0, 1, 2, 3) is not connected/enabled, there will be a null return.

pseudocode:
for (%n = 0; %n <= 3; %n++) {
if (!getJoystickPosition(%n)) {
  if (%n = 0)
    echo("No joysticks attached.");
  else
    echo("You have " @ (%n + 1) @ "joysticks connected.");
}

Sloppy, hacky, and with pretty much no error-checking. :p
#2
10/04/2006 (1:32 am)
That's a good sollution, but the logic in your pseudocode is a little wonky. If there are 1 - 3 joysticks it will say there is one more joystick than there actually is, and if there are four it won't do anything. I just thought I would point that out before anyone maybe used it as a model and got confused.
#3
10/04/2006 (5:05 am)
Original thread was here Teck:
www.garagegames.com/mg/forums/result.thread.php?qt=51006

Worked like a charm for me :)
#4
10/04/2006 (9:58 am)
$joystickCount = 0;
for(%n = 0; %n < 4; %n++) {
   if (!getJoystickAxes(%n)) {
      echo("Joystick" SPC %n SPC "not detected");
   }
   else {
      echo("Joystick" SPC %n SPC "detected");
      $joystickCount++;
   }
}
echo($joystickCount SPC "joysticks detected");

Wow, that's really similar to how I was thinking of doing it. So I guess it's guarenteed it will return a non-zero if a joystick is detected? What if it's just a digital pad?
#5
10/04/2006 (11:33 am)
Thomas: Doh! My brain was pretty fried by the time I tried to pseudocode that, so yeah, rereading it, it's pretty whacked. >_<

Thanks for the link, Jason. :)

Chris: Good question, that. getJoystickAxes(), iirc, only reads the analogue X and Y axes, so a digital pad may actually return a null value as well, though I'm not too sure. Anyone with a purely digital pad willing to try it out?
#6
10/04/2006 (7:22 pm)
Best I could do is push the little button on my Logitech Dual Analog that turns off the left stick and turns on its D-pad, and it still counted correctly. So for the three controllers I have, it counts perfectly. :)
#7
10/05/2006 (12:32 pm)
@Chris: Yea, the joysticks are by device rather than actual analog sticks.

@Teck: No problem, I do stuff like that all the time. I just didn't want anyone to get confused. =)