Page 1 of 1

how do i use game controller

PostPosted: Thu Oct 13, 2011 12:02 am
by pikapika
hi i have a logitech dual action gamepad and i want to make it so that when i go into game mode on my game that i can move my character with the joystick and make it shoot,jump,etc with the buttons. i tried hblades tutorial but it didnt work. i want to know the easiest way to use the controller so please post an answer. i am fairly new to game editor so please make it pretty easy to understand.

Re: how do i use game controller

PostPosted: Thu Oct 13, 2011 12:06 am
by Hblade
Hi! Thats a really nice gamepad! :) I used to have that. Okay so lets get started shall we?
Would you like a text-based tutorial used by reply of this, or would you like me to make a video showing how?

PS: It's the Logitech F130 right?

Re: how do i use game controller

PostPosted: Thu Oct 13, 2011 3:23 pm
by pikapika
ok, actually i just figured out how to use the joystick but for the buttons i need to know how to make it so that when you press different ones that different events are triggered such as being able to shoot or jump. in your tutorial it dosent say much about that. please post an answer in the box below.

Re: how do i use game controller

PostPosted: Thu Oct 13, 2011 4:31 pm
by Hblade
Alright. Heres how it works:
First, you have to make the variables. In our case:
Code: Select all
int BUTTONS[9];
double XAXIS;
double YAXIS;

void SetupPad() {
      int i;
      for (i=0;i<8;i++) {
          BUTTONS[i]=GetJoystick1Button(i); }
   
    XAXIS=GetJoystick1Axis(0);
    YAXIS=GetJoystick1Axis(1);
                  }


This --should-- enable you to use Buttons 1-7 (Xbox style for example: A,B,X,Y,LB,RB,SELECT,START) Now, lets define some functions to use. Save that to first code to global code, name it "Setup". Now in another global code, you'd put this: (I'm using xbox 360 based controles, most common PC controls in todays games)
Code: Select all
#define BTN_A BUTTONS[0]
#define BTN_B BUTTONS[1]
#define BTN_X BUTTONS[2]
#define BTN_Y BUTTONS[3]
#define BTN_L BUTTONS[4]
#define BTN_R BUTTONS[5]
#define BTN_SEL BUTTONS[6]
#define BTN_START BUTTONS[7]

Name this Con-Define, then save it.

Now to use this: In your actor, go to "Draw Actor" Then place this code
Code: Select all
SetupPad();

if (BTN_A) {
    //Jump here }
if (BTN_X) {
    //Shoot Here }


That simple :) To move the player, we'd create an offset. So go back to your Setup, and add int offset=10000 to the code. E.G:
Code: Select all
int OFFSET=10000;
int BUTTONS[9];
double XAXIS;
double YAXIS;

void SetupPad() {
      int i;
      for (i=0;i<8;i++) {
          BUTTONS[i]=GetJoystick1Button(i); }
   
    XAXIS=GetJoystick1Axis(0);
    YAXIS=GetJoystick1Axis(1);
                  }


Now the script for moving the player:
Code: Select all
if (XAXIS>OFFSET) {
    x+=5; }
else if (XAXIS<-OFFSET) {
    x-=5; }


Thats all there is to it ^^ if your confused about a part just ask :)

Re: how do i use game controller

PostPosted: Thu Oct 13, 2011 7:30 pm
by pikapika
i tried that out and it didnt work so i want to know how to make it so that certain buttons on the controller are equivalent to certain keys on the keyboard. that way if a certain event happens when you push a certain key such as x, the exact same thing will happen when you press a button on the controller such as 1. please post an answer in the box below.

Re: how do i use game controller

PostPosted: Thu Oct 13, 2011 7:38 pm
by Game A Gogo
well you could do something like
Code: Select all
char*key=GetKeyState();
ButtonJump=(GetJoystick1Button(0)||key[KEY_SPACE]);


char*key=GetKeyState() will get all the states from the keys. 0 = unpressed 1 = pressed

having the Joystick button and key inside a "||" or statement will make it so it will give out 1 if either or both are pressed