Page 1 of 1

Function: get controller state()

PostPosted: Tue Oct 25, 2011 6:31 pm
by lcl
Hello!

So, some of you may know that GE has GetKeyState(); function, which returns the state of the keyboard.
And well I thought that it's simpler to create same kind of for controller than call getJoystick1Button() million times.. xD

So, here's what I got:
Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14

char joystickKey[15];

char * getControllerState()
{
    int i = 0;
 
    for (i = 0; i < 14; i ++)
    {
        joystickKey[i + 1] = GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }

    return joystickKey;
}


Usage (just the same as using GetKeyState()):
Code: Select all
char * key = getControllerState();

if (key[BUTTON_1])//do some stuff ... xD


And another version with sticks treated as buttons, simply on/off.
Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14
#define STICK_L_R 16 //stick left, pushed right :D
#define STICK_L_L 17
#define STICK_L_U 18
#define STICK_L_D 19
#define STICK_R_R 20
#define STICK_R_L 21
#define STICK_R_U 22
#define STICK_R_D 23

char joystickKey[24];

char * getControllerState()
{
    int i = 0;
 
    for (i = 0; i < 14; i ++)
    {
        joystickKey[i + 1] = GetJoystick1Button(i);
    }
    joystickKey[16] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == -1);
    joystickKey[17] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == 1);
    joystickKey[18] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == -1);
    joystickKey[19] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == 1);
 
    joystickKey[20] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == -1);
    joystickKey[21] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == 1);
    joystickKey[22] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == -1);
    joystickKey[23] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == 1);
 
    return joystickKey;
}


Usage:
Code: Select all
char * key = getControllerState();

if (key[STICK_L_R])xvelocity = 5; //If left stick is moved to right, set xvelocity to 5 :D


Sorry for not putting that much comments to the code, I just can't think of anything to put there.. :P

And please tell me if there is something that I could/should optimize. :)

-lcl-

Re: Function: get controller state()

PostPosted: Tue Oct 25, 2011 8:02 pm
by Hblade
neat! :)

Re: Function: get controller state()

PostPosted: Thu Oct 27, 2011 12:36 am
by Jagmaster
Very useful, thank you!

Re: Function: get controller state()

PostPosted: Thu Oct 27, 2011 11:33 am
by Game A Gogo
Code: Select all
char * key = malloc(sizeof(char)*15);

Do I see memory leakage?
I'm wondering because it's never getting freed and the function always gets run :P
someone could clarify this for me, someone like Evan xD

Re: Function: get controller state()

PostPosted: Thu Oct 27, 2011 5:53 pm
by lcl
Game A Gogo wrote:
Code: Select all
char * key = malloc(sizeof(char)*15);

Do I see memory leakage?
I'm wondering because it's never getting freed and the function always gets run :P
someone could clarify this for me, someone like Evan xD

Yeah, that's mostly what I meant with:
lcl wrote:And please tell me if there is something that I could/should optimize.

I just don't know where I can free it because I need the function to return it before freeing it.
So, help with this would be helpful :D

Re: Function: get controller state()

PostPosted: Thu Oct 27, 2011 9:07 pm
by skydereign
Simply put, don't use malloc. Just make it an array. Generally speaking you don't need to allocate memory for simple things. If all you are doing is returning the temporary array, then that means you are storing it elsewhere, which means you don't need to allocate the memory for it in the function.

Re: Function: get controller state()

PostPosted: Thu Oct 27, 2011 10:15 pm
by lcl
skydereign wrote:Simply put, don't use malloc. Just make it an array. Generally speaking you don't need to allocate memory for simple things. If all you are doing is returning the temporary array, then that means you are storing it elsewhere, which means you don't need to allocate the memory for it in the function.

But I can't return a normal array.
If I try:
Code: Select all
char key[14];

The function returns nothing.
Do you know any way to do this?

Re: Function: get controller state()

PostPosted: Fri Oct 28, 2011 12:16 am
by skydereign
Oh, sorry about that. I didn't actually look at what you were doing , just the problem you were asking about (I was in class). Anyway, it is because you are returning a char*. All it is is an address to the actual array, and if you don't allocate memory for it, the array will disappear when the function ends. So, there are two ways to do this. One is to pass the array you want altered into the function.
Code: Select all
void
function (char string[])
{
    int i;
    for(i=0;i<20;i++)
    {
        //
    }
}


Or the other is to use your function with the memory allocation, and then free the char* you set the function to when you are done. I wouldn't suggest this method as it if unattended will cause a memory leak, and the above method is self contained.
Code: Select all
char* key = getControllerState();
// do stuff with key
free(key);

Re: Function: get controller state()

PostPosted: Fri Oct 28, 2011 11:28 am
by Game A Gogo
Code: Select all
char jkey[15];
char * getControllerState()
{
    int i;
    for (i=0;i<14;i++)
    {
        jkey[i+1]=GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }
    return jkey;
}

I expect this to work, since you're already allocating/un-allocating every time you run the function, which is most likely every frame, might as well allocate it for the time the game runs no?

Re: Function: get controller state()

PostPosted: Fri Oct 28, 2011 12:18 pm
by lcl
Game A Gogo wrote:
Code: Select all
char jkey[15];
char * getControllerState()
{
    int i;
    for (i=0;i<14;i++)
    {
        jkey[i+1]=GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }
    return jkey;
}

I expect this to work, since you're already allocating/un-allocating every time you run the function, which is most likely every frame, might as well allocate it for the time the game runs no?

Thanks, got to try that, I don't know why I hadn't thought of it before.. :lol:

Re: Function: get controller state()

PostPosted: Thu Jun 26, 2014 10:45 pm
by Zivouhr
Thanks for the info on programming for a controller handheld, LCL.

Re: Function: get controller state()

PostPosted: Sun Nov 02, 2014 3:32 pm
by Zivouhr
lcl wrote:Thanks, got to try that, I don't know why I hadn't thought of it before.. :lol:


Thanks for the tips. My PS3 controller DPad is registering on my computer now (after figuring out how to get it recognized on a computer). That other controller, the super nintendo one, might be broken or something for the DPad, as this one works on PS3.

EDIT: Here's the GED Game Editor file for all to test a PS3 Controller with.
Includes functioning for:
DPad
All buttons (excluding the PS Power on button)
Analog (have yet to figure out how to get both functioning at same time, but can get either one plus Dpad at same time working, just need to figure out how to have an Analog without the character slowly floating; any tips appreciated on how to do this). To have the character stop floating, I have to "//" the ANALOG code in this file, and just use the DPad to control your character.

PS3 GAME CONTROLLER ON GAME EDITOR
PS3gamepadGE2014.ged
(8.21 KiB) Downloaded 514 times


I hope that helps, thanks again for all the help here LCL and the Game Editor Forum! It's awesome to finally get a game working with a PS3 controller.