Game Development Community

Gamepad support?

by Jesse Hall · in Torque Game Builder · 07/30/2005 (5:12 pm) · 16 replies

I am just wondering if T2D or TGE for that matter has gamepad support? I found something on a search but it is pretty dated.

- J

#1
07/30/2005 (5:31 pm)
I don't think TGE/T2D has support out of the box, but there are resources on the site you can download to add gampad/joystick support.
#2
07/31/2005 (2:57 am)
Jesse

Gamepads work but you need a little bit of scripting to link the buttons to actions.

Most of what I have has been done by trial and error but the stuff below should get you started.

First, in client/prefs.cs you need to add some preferences:

$pref::Input::GamepadEnabled = "1";
$pref::Input::JoystickEnabled = "1";
$pref::Input::Deadzone = 0.25;
$pref::Input::Sensitivity = 1;


Then you bind functions to the gamepad:

playerMap.bind(joystick, "xaxis", joymovex);
playerMap.bind(joystick, "yaxis", joymovey);

function joymovex(%val)
{
   if ($pref::Input::Deadzone < %val | - $pref::Input::Deadzone > %val)
   {
      $SomeVariable = %val * $pref::Input::Sensitivity;
      //echo($SomeVariable);
   }
   else
   {
      $SomeVariable = 0;
   }
}

function joymovey(%val)
{
   if ($pref::Input::Deadzone < %val | - $pref::Input::Deadzone > %val)
   {
      $SomeOtherVariable = %val * $pref::Input::Sensitivity;
      //echo($SomeOtherVariable);
   }
   else
   {
      $SomeOtherVariable = 0;
   }
}

The first line binds the joystick xaxis (left stick side to side movement) to function joymovex().

%val is the value passed to the function from the gamepad bind and represents the amount of movement the stick makes (varies between -1 and 1 IIRC)

$pref::Input::Deadzone is the amount in the middle of the range of movement that you want to ignore. You can change this and $pref::Input::Sensitivity to suit your game.

To bind buttons you can do a similar thing :

playerMap.bind(joystick, button0, joyexit );

function joyexit(%val)
{
   if (%val)
      escapefromgame();
}

This binds button0 to the function joyexit() for example which calls another function inside when button0 is pressed.

Buttons are either on or off so %val is either 1 or 0.

Hope this helps.

Edit - after posting this I went to check the scripts in T2D (I was using it in TGE), and I found out that these 2 lines are needed too :
$enableDirectInput = "1";
   activateDirectInput();
#3
07/31/2005 (8:00 am)
Awesome!! Thank You!
#4
07/31/2005 (9:37 am)
I thought I had better put the full list of binds in the thread while I was at it :

playerMap.bind(joystick, "xaxis", joymovex); //left stick
playerMap.bind(joystick, "yaxis", joymovey); //left stick
playerMap.bind(joystick, "zaxis", joyyaw);  //right stick
playerMap.bind(joystick, "rzaxis", joypitch); //right stick
playerMap.bind(joystick, button0, joybutton0 );
playerMap.bind(joystick, button1, joybutton1 );
playerMap.bind(joystick, button2, joybutton2 );
playerMap.bind(joystick, button3, joybutton3 );
playerMap.bind(joystick, button4, joybutton4 );
playerMap.bind(joystick, button5, joybutton5 );
playerMap.bind(joystick, button6, joybutton6 );
playerMap.bind(joystick, button7, joybutton7 );
playerMap.bind(joystick, button8, joybutton8 );
playerMap.bind(joystick, button9, joybutton9 );
playerMap.bind(joystick, upov, joypovup ); //POV up
playerMap.bind(joystick, dpov, joypovdown ); //POV down
playerMap.bind(joystick, lpov, joypovleft ); //POV left
playerMap.bind(joystick, rpov, joypovright ); //POV right

That will map both sticks, all the buttons and the POV hat (using a logitech rumblepad II) you just have to provide the functions.

One other thing I have noticed - the logitech has numbered buttons and these dont match the button reported in script! They are all off by one so button 1 on the gamepad is button0 in the script bind, button 2 on the pad is button1 in the script bind etc etc.
#5
07/31/2005 (10:17 am)
Out of curiosity, does Torque support multiple gamepads?
#6
07/31/2005 (10:22 am)
@ Ray - I only have one so I dont know :)

It does detect it as joystick0 though so it may find more than one.
#7
07/31/2005 (10:37 am)
Testing this a little further (and I wonder where my days go sometimes :) ) T2D currently doesnt seem to support the POV hat whereas it works in TGE 1.4 - this may be specific to my machine and gamepad though.

I thought I would post the test script here so anyone who want to get the basics of gamepad support can do.


The following is based on the standard spaceshooter demo :-

in spacescroller/client/client.cs add the following line in bold inside the startgame() function
...
	// Start up our game scripts
	exec( "./scene.cs" );
	exec( "./player.cs" );
	exec( "./enemy.cs" );
	
        [b]exec( "./gamepadbinds.cs"); //<--Add this[/b]
...

in spacescroller/client/player.cs add the following line in bold inside the setupPlayer() function
...
	// Setup Player Controls.
	setupPlayerControls();

        [b]setupGamePad(); //<---Add this[/b]
...


in spacescroller/client/prefs.cs change

$pref::Input::JoystickEnabled = "0";

to

$pref::Input::JoystickEnabled = "1";


cont'd ...
#8
07/31/2005 (10:38 am)
... cont'd

make a new text file in the spacescroller/client folder called gamepadbinds.cs and paste in all the following
// Bind the functions and enable the gamepad
function setupGamePad()
{
   bindGamePadButtons();
   $enableDirectInput = "1";
   activateDirectInput();
}


//Add gamepad binds to the playermap created in player.cs
function bindGamePadButtons()
{
   playerMap.bind(joystick, "xaxis", joymovex); //left stick
   playerMap.bind(joystick, "yaxis", joymovey); //left stick
   playerMap.bind(joystick, "zaxis", joyyaw);  //right stick
   playerMap.bind(joystick, "rzaxis", joypitch); //right stick
   playerMap.bind(joystick, button0, joybutton0 );
   playerMap.bind(joystick, button1, joybutton1 );
   playerMap.bind(joystick, button2, joybutton2 );
   playerMap.bind(joystick, button3, joybutton3 );
   playerMap.bind(joystick, button4, joybutton4 );
   playerMap.bind(joystick, button5, joybutton5 );
   playerMap.bind(joystick, button6, joybutton6 );
   playerMap.bind(joystick, button7, joybutton7 );
   playerMap.bind(joystick, button8, joybutton8 );
   playerMap.bind(joystick, button9, joybutton9 );


//POV works with TGE 1.4 not with T2D EA build

   playerMap.bind(joystick, "upov", joypovup ); //POV up
   playerMap.bind(joystick, "dpov", joypovdown ); //POV down
   playerMap.bind(joystick, "lpov", joypovleft ); //POV left
   playerMap.bind(joystick, "rpov", joypovright ); //POV right
}


//Basic Functions - the will echo to the console and console.log so you can see what is working
//Change the function contents to whatever you want


//Left Stick
function joymovex(%val)
{
   echo("Left Stick x: "@%val);
}

function joymovey(%val)
{
   echo("Left Stick y: "@%val);
}


//Right Stick
function joyyaw(%val)
{
   echo("Right stick x: "@%val);
}

function joypitch(%val)
{
   echo("Right stick y: "@%val);
}


//Buttons
function joyButton0(%val)
{
   if(%val)
   {
      echo("Button 0 pressed");
   }

   else
   {
      echo("Button 0 released");
   }
}

function joyButton1(%val)
{
   if(%val)
   {
      echo("Button 1 pressed");
   }

   else
   {
      echo("Button 1 released");
   }
}

function joyButton2(%val)
{
   if(%val)
   {
      echo("Button 2 pressed");
   }

   else
   {
      echo("Button 2 released");
   }
}

function joyButton3(%val)
{
   if(%val)
   {
      echo("Button 3 pressed");
   }

   else
   {
      echo("Button 3 released");
   }
}

function joyButton4(%val)
{
   if(%val)
   {
      echo("Button 4 pressed");
   }

   else
   {
      echo("Button 4 released");
   }
}

function joyButton5(%val)
{
   if(%val)
   {
      echo("Button 5 pressed");
   }

   else
   {
      echo("Button 5 released");
   }
}

function joyButton6(%val)
{
   if(%val)
   {
      echo("Button 6 pressed");
   }

   else
   {
      echo("Button 6 released");
   }
}

function joyButton7(%val)
{
   if(%val)
   {
      echo("Button 7 pressed");
   }

   else
   {
      echo("Button 7 released");
   }
}

function joyButton8(%val)
{
   if(%val)
   {
      echo("Button 8 pressed");
   }

   else
   {
      echo("Button 8 released");
   }
}

function joyButton9(%val)
{
   if(%val)
   {
      echo("Button 9 pressed");
   }

   else
   {
      echo("Button 9 released");
   }
}


//POV hat - //POV works with TGE 1.4 not with T2D EA build

function joyPOVUp(%val)
{
   echo("POVUp: "@%val);
}

function joyPOVDown(%val)
{
   echo("POVDown: "@%val);
}

function joyPOVLeft(%val)
{
   echo("POVLeft: "@%val);
}

function joyPOVRight(%val)
{
   echo("POVRight: "@%val);
}
#9
07/31/2005 (10:58 am)
Nice! I'll work on that after I finish going over a vector math tut. =)

shame about the POV hat but im sure its just a matter of time.
#10
08/01/2005 (9:13 am)
I was having problems getting the POV to work as well. I'm using a Logitech controller as well (don't know the name of it off the top of my head).
#11
08/01/2005 (10:14 am)
Yeah.. i got the logitech as well.. the basic one for $20.00 without the rumble.

It really adds to the game experience for certain games. Most of the time im a WASD mouse dude but platformers just arent as fun with that setup.
#12
08/01/2005 (2:39 pm)
------------ edit --------------

was having trouble then found out if using the standard client.cs you have to add an additonal

$pref::Input::JoystickEnabled = "1";

somewhere...

apperently it gets reset somewhere. I have a feeling its in the gui for editing.
#13
12/18/2005 (12:46 pm)
I have followed the directions above and I have tried just about everything I can think of. I can still not get my gamepad to work properly. Direct Input Joystick failed to enable! I am using T2D release 1_0_2. I have an xbox 360 gamepad. It works fine with a few commercial games made with Torque. Any ideas?
#14
12/18/2005 (12:59 pm)
Here's the T2D code diff and samples to 1.0.2 to enable Xbox 360 controllers in Windows . http://www.garagegames.com/mg/forums/result.thread.php?qt=36789
#15
12/18/2005 (1:25 pm)
Thanks for the quick reply!
#16
12/18/2005 (5:21 pm)
No problem. :-)