TGB/ScriptTutorials/JoystickSetup

From TDN


First add the following script code somewhere into your game. I've inserted mine into startGame...

function startGame(%level)
{
  ... <some script code> ...

  $enableDirectInput = "1";
  activateDirectInput();
  enableJoystick();  

  ... <some script code> ...
}

That's it your good to go. Now you can bind buttons to commands through the global action map. As one example of button bindings here is the action map bindings for a PS2 controller...

GlobalActionMap.bindCmd(joystick0, "button0", "echo(triangle);", "");
GlobalActionMap.bindCmd(joystick0, "button1", "echo(circle);", "");
GlobalActionMap.bindCmd(joystick0, "button2", "echo(x);", "");
GlobalActionMap.bindCmd(joystick0, "button3", "echo(square);", "");

And here's an example for stick or d-pad axis :

moveMap.bind(joystick0, "xaxis", xaxis);
function xaxis(%val)
{
	if (%val > 0.5)
	{
		echo("go right");
	}


	else if (%val < -0.5) 
	{
		echo("go left");
	}
	
	else
	{
		%val=0;
		echo("horizontal stop");
	}	
}

moveMap.bind(joystick0, "yaxis", yaxis);
function yaxis(%val)
{
	if (%val > 0.5)
	{
		echo("go down");
	}


	else if (%val < -0.5) 
	{
		echo("go up");
	}
	
	else
	{
		%val=0;
		echo("vertical stop");
	}	
}