Tutorial - Setting up a controller

Learn how to make certain types of games and use gameEditor.

Tutorial - Setting up a controller

Postby Hblade » Mon Jul 19, 2010 10:13 am

Setting up a controller

Ever want to use a controller with GE? Now you can, this does require the latest version so head on over to game-editor.com/Download if you dont already have version 1.4

Alrighty lets begin shall we? Let me go over the functionality fist.

In the script you'll see later on in this post, you'll notice that you have these things new people might not know about called "double, float, int, long, short, char", these are all variables. double, and floats can store decimals such as 1.45738, int's or Integers, can store solid numbers, such as 1, 2, 3, 4 etc... char is a variable that stores text, its a string variable, or a character variable. Long and short limit the other types of variables as far as I know, they go together such as long int, short int. Alrighty lets get to the controller setup! :D

You'll need to use this script every time you want something to move using the controller so I suggest you learn how to make voids, click here to do that.

Alright first I'm going to go over the lines 1 by 1 then throw all the code at you once you understand it :D Put this in Draw Actor of the person you are trying to move. Or you can start a new project and make a character called Player, doesn't matter if he has any graphics or not :3 Instead of making tons of draw actors, simply break a new line, in other words place all these scripts right after eachother, dont press "Ok" after every script lol xD :3
Code: Select all
float ls_XAxis = GetJoystick1Axis(0);

The ls_XAxis is now a float variable and stores the infromation of the left joysticks current X-Axis.
Code: Select all
float ls_YAxis = GetJoystick1Axis(1);

This will get the left joysticks YAxis, as you can see in the script part "GetJoystick1Axis(1)", its 1 this time, not 0.

    0 = left stick Xaxis
    1 = left stick Yaxis
    2 = right stick Xaxis
    3 = right stick Yaxis

Now lets run a test on that shall we? Put this code before you test it or you character is gonna go flying off screen xD
Code: Select all
xvelocity=ls_XAxis/10000;
yvelocity=ls_YAxis/10000;

This will limit the speed and divide the total joystick axis by 10,000 preventing super fast movement lol. Other people have been known to use the Vector function.

Test that out.

After that we move on to the buttons :D This will be using an Array, which I'll also go over to prevent confusion :D An array is pretty much like, 1 variable that can act as many variables. An example is
Code: Select all
int Array[4]

That means this variable can store 4 different values, starting with 0, 1, 2, 3.
In other words you can do this.
Code: Select all
int Array[4];
Array[0] = 3;
Array[1] = 89;
Array[2] = 49;
Array[3] = 189;

That 1 variable is now storing the values 3, 89, 49, and 189. Alright now that you understand arrays a little bit, lets get started with the buttons :D To be able to use the buttons all you have to do is assign the button to the array. Place this code after the first 2 floats we put in Draw Actor, above the xvelocity= and yvelocity=, or you will get an error :o
Code: Select all
int i;
int Button[14];
for(i=0; i<14; i++){
Button[i] = GetJoystick1Button(i);
}

    Thanks DST xD
There are more buttons on a controller than you think :P And some people use more advanced controllers that have even more buttons than this xD This is a total of 14 buttons. Now you have access to 14 buttons on a controller. You can look at your controller to see the numbers, and if your using an Xbox360 controller, or a PS3 controller, then you'd have to do some testing to find out which buttons are which. Now to access and use these buttons all you'd have to do is call a script like this.
Code: Select all
if (Button[0])
{
    r = 89;
    g = 0;
    b = 0;
}

You can replace the rgb editing with any other code you like that was just an example. Alright heres all the code together
Code: Select all
float ls_XAxis = GetJoystick1Axis(0);
float ls_YAxis = GetJoystick1Axis(1);
int i;
int Button[14];
for(i=0; i<14; i++){
Button[i] = GetJoystick1Button(i);
}


xvelocity=ls_XAxis/10000;
yvelocity=ls_YAxis/10000;
if (Button[0])
{
    r = 89;
    g = 0;
    b = 0;
}


Test it out! :D And enjoy, I hope this helped

Oh, and remember that not all controllers will have buttons "0, 1, 2, 3", instead it'll be "1, 2, 3, 4", dont worry about that its still 0, 1, 2, 3, so you dont have to worry about not mapping it 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: Tutorial - Setting up a controller

Postby krenisis » Mon Jul 19, 2010 7:54 pm

thanks i will study this!!!!
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Tutorial - Setting up a controller

Postby Hblade » Mon Jul 19, 2010 8:07 pm

:D
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: Tutorial - Setting up a controller

Postby Game A Gogo » Wed Jul 21, 2010 4:49 pm

divide by 32767, so then the maximum speed is 1px, or very very close
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

Re: Tutorial - Setting up a controller

Postby Hblade » Wed Jul 21, 2010 5:44 pm

hha, yeah
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: Tutorial - Setting up a controller

Postby pizbi » Thu Aug 26, 2010 3:07 am

how do I do things similar to key-down key-up? and also I was wondering if theres any key for joystick because the joystick doesn't change the animation or specify a speed :(
User avatar
pizbi
 
Posts: 37
Joined: Thu Jul 08, 2010 3:36 am
Location: oregon
Score: 7 Give a positive score

Re: Tutorial - Setting up a controller

Postby DilloDude » Thu Aug 26, 2010 5:18 am

For key up/down, you can make another variable (or array in this case) to store the previous value of the button.
Code: Select all
if (button[0])//if the button is pressed
{
    if (prevButton[0] == 0)//if the button wasn't pressed last frame
    {
        //keydown script
    }
}
else//the button ISN'T pressed
{
    if (prevButton[0])//if the button WAS pressed last frame
    {
        //keyup script
    }
}


To work with the joystick you'd have to make your own comparisons with the variables.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Tutorial - Setting up a controller

Postby SuperSonic » Wed Jan 25, 2012 3:36 am

Sorry for bumping :oops:

@Hblade: I finally got my own Logitech f130 gamepad to work by downloading the microsoft's xbox drivers. Now I tried going through the first part of your tutorial with the analog sticks (I copied your code exactly). However, when I went into game mode and pressed on my analog stick, nothing happened. Could you help me out? :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial - Setting up a controller

Postby Hblade » Wed Jan 25, 2012 4:14 am

Turn the mode on xD (bottom left button near start and select) :P

If it's off you can use the D-Pad.
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: Tutorial - Setting up a controller

Postby SuperSonic » Wed Jan 25, 2012 4:45 pm

Nope, still nothing :(

Could the problem be that I used the xbox gamepad drivers instead of logitech? :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial - Setting up a controller

Postby Hblade » Wed Jan 25, 2012 5:18 pm

I doubt it o-o Try this code in a simple draw actor


Global Code:
Code: Select all
int BTN[10];
double AXIS[2];
void SetupPad() {
    int i; for(i=0;i<9;i++) {
        BTN[i]=GetJoystick1Button(i);
        }
    AXIS[0]=GetJoystick1Axis(0);
    AXIS[1]=GetJoystick1Axis(1);
}


Now for Draw Actor:
Code: Select all
SetupPad();
if (AXIS[0]>10000) {
    x+=5;
}

Move the joystick (Or Dpad) to the right and see if he moves :)
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: Tutorial - Setting up a controller

Postby SuperSonic » Wed Jan 25, 2012 5:32 pm

Nope :cry:

I tried turning mode on and off and I even switched between 'x' and 'd' on the remote :?
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial - Setting up a controller

Postby Hblade » Wed Jan 25, 2012 5:58 pm

D: Then i dunno T.T its gotta be something to do with your drivers. Try this:
http://www.logitech.com/en-us/441/7359? ... t=&osid=14
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: Tutorial - Setting up a controller

Postby SuperSonic » Wed Jan 25, 2012 7:10 pm

But that's for F510. I have F310. Should I search for that instead? :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial - Setting up a controller

Postby Hblade » Wed Jan 25, 2012 8:52 pm

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

Next

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest