Tutorial - Setting up a controller
Posted: 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
The ls_XAxis is now a float variable and stores the infromation of the left joysticks current X-Axis.
This will get the left joysticks YAxis, as you can see in the script part "GetJoystick1Axis(1)", its 1 this time, not 0.
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
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
That means this variable can store 4 different values, starting with 0, 1, 2, 3.
In other words you can do this.
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
You can replace the rgb editing with any other code you like that was just an example. Alright heres all the code together
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.
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
- 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.