Page 1 of 3

Tutorial - Setting up a controller

PostPosted: Mon Jul 19, 2010 10:13 am
by Hblade
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.

Re: Tutorial - Setting up a controller

PostPosted: Mon Jul 19, 2010 7:54 pm
by krenisis
thanks i will study this!!!!

Re: Tutorial - Setting up a controller

PostPosted: Mon Jul 19, 2010 8:07 pm
by Hblade
:D

Re: Tutorial - Setting up a controller

PostPosted: Wed Jul 21, 2010 4:49 pm
by Game A Gogo
divide by 32767, so then the maximum speed is 1px, or very very close

Re: Tutorial - Setting up a controller

PostPosted: Wed Jul 21, 2010 5:44 pm
by Hblade
hha, yeah

Re: Tutorial - Setting up a controller

PostPosted: Thu Aug 26, 2010 3:07 am
by pizbi
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 :(

Re: Tutorial - Setting up a controller

PostPosted: Thu Aug 26, 2010 5:18 am
by DilloDude
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.

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 3:36 am
by SuperSonic
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? :)

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 4:14 am
by Hblade
Turn the mode on xD (bottom left button near start and select) :P

If it's off you can use the D-Pad.

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 4:45 pm
by SuperSonic
Nope, still nothing :(

Could the problem be that I used the xbox gamepad drivers instead of logitech? :)

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 5:18 pm
by Hblade
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 :)

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 5:32 pm
by SuperSonic
Nope :cry:

I tried turning mode on and off and I even switched between 'x' and 'd' on the remote :?

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 5:58 pm
by Hblade
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

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 7:10 pm
by SuperSonic
But that's for F510. I have F310. Should I search for that instead? :)

Re: Tutorial - Setting up a controller

PostPosted: Wed Jan 25, 2012 8:52 pm
by Hblade