how do i use game controller

Non-platform specific questions.

how do i use game controller

Postby pikapika » Thu Oct 13, 2011 12:02 am

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.
pikapika
 
Posts: 10
Joined: Wed Oct 12, 2011 12:06 am
Score: 0 Give a positive score

Re: how do i use game controller

Postby Hblade » Thu Oct 13, 2011 12:06 am

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?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: how do i use game controller

Postby pikapika » Thu Oct 13, 2011 3:23 pm

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.
pikapika
 
Posts: 10
Joined: Wed Oct 12, 2011 12:06 am
Score: 0 Give a positive score

Re: how do i use game controller

Postby Hblade » Thu Oct 13, 2011 4:31 pm

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 :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: how do i use game controller

Postby pikapika » Thu Oct 13, 2011 7:30 pm

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.
pikapika
 
Posts: 10
Joined: Wed Oct 12, 2011 12:06 am
Score: 0 Give a positive score

Re: how do i use game controller

Postby Game A Gogo » Thu Oct 13, 2011 7:38 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest