MFGG phpBB Message Boards Archive
https://phpbb.mfgg.net/

Gamepad Support In Your Games
https://phpbb.mfgg.net/viewtopic.php?f=10&t=18363
Page 1 of 1

Author:  HylianDev [ Sun Dec 27, 2015 5:36 pm ]
Post subject:  Gamepad Support In Your Games

Gamepad Support

An issue that is near and dear to my heart. Basically, it means that I can plug a controller into my computer and use it with your game. Playing with keyboards just isn't the same! In this topic, I'll outline (with the help of DJ Coco) how you can do it in Game Maker. It's simple!

Detecting if a button is pressed

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
if(gamepad_button_check(0, gp_padr)){
    // code for moving right
}


What does this code do?

gamepad_button_check is a function that tells you whether or not that button is currently down. The 0 is the number of the gamepad you're checking. For instance, if the computer as three gamepads plugged in, 0 will use the first one, 1 the second, 3 the third, etc. I recommend using a global variable for the gamepad ID, for letting the player select which gamepad they want to use. OR, just in case you ever want to expand your codebase later on. It's just good practice. gp_padr is the button to check. The suffix, "gp_", means that it's a gamepad button. padr means the right directional button on the D-pad.

See how simple that was?

Here is a list of buttons:
Spoiler:


But I don't own a gamepad!

It seems that a big portion of us might not own a gamepad. But, you can still put support for it in your game. You might run into an idea or two that needs testing, but for your basic mario platformer I don't see why not just support gamepads anyway. You can likely find a user wiling to test your game to see if all of the buttons respond properly. It's just a good idea, and will make playing your game that much more of an enjoyable experience! :)

For more information, like other functions you can use and etc, check out the official Game Maker documentation on gamepad support.

Author:  CatoNator [ Sun Dec 27, 2015 5:59 pm ]
Post subject:  Re: Gamepad Support In Your Games

Here's how you get the analog sticks working:

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
if gamepad_axis_value(index,gp_axislh) > 0.5
{
    if hsp<mysp{hsp+=0.5}else{hsp=mysp}
    walking = true;
}
else if gamepad_axis_value(index,gp_axislh) < -0.5
{
    if hsp>-mysp{hsp-=0.5}else{hsp=-mysp}
    walking = true;
}
else
{
    if hsp>0 {hsp-=0.5}else if hsp<0 {hsp+=0.5}else{hsp=0}
    walking = false;
   
    if hsp > -0.5 || hsp < 0.5
    hsp = 0;
}

Author:  Mors [ Sun Dec 27, 2015 6:06 pm ]
Post subject:  Re: Gamepad Support In Your Games

Super Mario Flashback supports gamepad so yea
It was a bit glitchy in the NCFC demo buy it's fixed now c:

Author:  Kritter [ Sun Dec 27, 2015 6:21 pm ]
Post subject:  Re: Gamepad Support In Your Games

Simple if you use Game Maker you mean.

Author:  DJ Coco [ Sun Dec 27, 2015 6:26 pm ]
Post subject:  Re: Gamepad Support In Your Games

It's even simpler in Unity. I have no idea how it's done in any other engine, but afaik MMF2 has some customizability thing set up from the start, doesn't it?

Author:  Vimimin [ Sun Dec 27, 2015 6:29 pm ]
Post subject:  Re: Gamepad Support In Your Games

DJ Coco wrote:
afaik MMF2 has some customizability thing set up from the start, doesn't it?

I think there's an extension for gamepad support, but I'm not sure if it has analog stick functionality or not.

Author:  onpon4 [ Sun Dec 27, 2015 9:30 pm ]
Post subject:  Re: Gamepad Support In Your Games

Checking joystick values in the SGE Game Engine:

Syntax: [ Download ] [ Hide ]
Using python Syntax Highlighting
sge.joystick.get_axis(0, 1) # Value of joystick 0, axis 1
sge.joystick.get_hat_x(1, 0) # Horizontal position of joystick 1, d-pad 0
sge.joystick.get_hat_y(1, 0) # Vertical position of joystick 1, d-pad 0
sge.joystick.get_pressed(0, 2) # Whether or not joystick 0, button 2 is pressed
 


Or you can use events. I tend to use these functions for things like movement, and events for things like jumping. The good thing about using events is it avoids the (slim) possibility of a button press not registering.

One nice thing about gamepads is you can usually come up with a configuration that will work for everyone, especially because you don't have to worry about keyjamming. I find that this works on all of them:

- Buttons 1 and 2: primary action (e.g. jumping)
- Buttons 0 and 3: secondary action (e.g. running)

And of course, it's pretty much universal for buttons 4 and 6 to be L buttons, buttons 5 and 7 to be R buttons, button 8 to be Select, and button 9 to be Start.

The best case, though, is always to let the user customize the controls somehow. The way I usually do this is to have two dictionaries (associative arrays): one for keyboard controls, and one for joystick controls. Since joystick controls vary in type, I have a tuple (array) that goes something like (0, "button", 1) for joystick 0, button 1. Then I have some admittedly long code to handle all possible cases. It's a bit less simple than hardcoded control definitions or locking certain actions to certain types of controls, but definitely worth it:

Syntax: [ Download ] [ Hide ]
Using python Syntax Highlighting
class Player(xsge_physics.Collider):

# ...

    def refresh_input(self):
        self.left_pressed = sge.keyboard.get_pressed(left_key[self.player])
        self.right_pressed = sge.keyboard.get_pressed(
            right_key[self.player])
        self.up_pressed = sge.keyboard.get_pressed(up_key[self.player])
        self.down_pressed = sge.keyboard.get_pressed(down_key[self.player])

        # Add current joystick state
        js_controls = [left_js, right_js, up_js, down_js]
        js_states = [False for i in js_controls]
        for i in six.moves.range(len(js_controls)):
            if js_controls[i][self.player] is not None:
                j, t, c = js_controls[i][self.player]
                if t == "axis+":
                    v = sge.joystick.get_axis(j, c)
                    if v > joystick_threshold:
                        js_states[i] = abs(v)
                elif t == "axis-":
                    v = sge.joystick.get_axis(j, c)
                    if v < -joystick_threshold:
                        js_states[i] = abs(v)
                elif t == "axis0":
                    js_states[i] = (abs(sge.joystick.get_axis(j, c)) <=
                                    joystick_threshold)
                elif t == "hatx+":
                    js_states[i] = (sge.joystick.get_hat_x(j, c) == 1)
                elif t == "hatx-":
                    js_states[i] = (sge.joystick.get_hat_x(j, c) == -1)
                elif t == "haty+":
                    js_states[i] = (sge.joystick.get_hat_y(j, c) == 1)
                elif t == "haty-":
                    js_states[i] = (sge.joystick.get_hat_y(j, c) == -1)
                elif t == "hatx0":
                    js_states[i] = (sge.joystick.get_hat_x(j, c) == 0)
                elif t == "haty0":
                    js_states[i] = (sge.joystick.get_hat_y(j, c) == 0)
                elif t == "button":
                    js_states[i] = sge.joystick.get_pressed(j, c)

        self.left_pressed = self.left_pressed or js_states[0]
        self.right_pressed = self.right_pressed or js_states[1]
        self.up_pressed = self.up_pressed or js_states[2]
        self.down_pressed = self.down_pressed or js_states[3]

# ...

    def event_joystick_axis_move(self, js_name, js_id, axis, value):
        js_versions = [(js_id, "axis+", axis), (js_id, "axis-", axis)]
        if value > joystick_threshold:
            js = (js_id, "axis+", axis)
        elif value < -joystick_threshold:
            js = (js_id, "axis-", axis)
        else:
            js = (js_id, "axis0", axis)

        if js == tuple(action_js[self.player]):
            self.action()
        if js == tuple(pause_js[self.player]):
            sge.game.current_room.pause()

    def event_joystick_hat_move(self, js_name, js_id, hat, x, y):
        js_versions = [(js_id, "hatx+", hat), (js_id, "hatx-", hat)]
        if x > 0:
            js = (js_id, "hatx+", hat)
        elif x < 0:
            js = (js_id, "hatx-", hat)
        else:
            js = (js_id, "hatx0", hat)

        if js == tuple(action_js[self.player]):
            self.action()
        if js == tuple(pause_js[self.player]):
            sge.game.current_room.pause()

        js_versions = [(js_id, "haty+", hat), (js_id, "haty-", hat)]
        if y > 0:
            js = (js_id, "haty+", hat)
        elif y < 0:
            js = (js_id, "haty-", hat)
        else:
            js = (js_id, "haty0", hat)

        if js == tuple(action_js[self.player]):
            self.action()
        if js == tuple(pause_js[self.player]):
            sge.game.current_room.pause()

    def event_joystick_button_press(self, js_name, js_id, button):
        js = (js_id, "button", button)

        if js == tuple(action_js[self.player]):
            self.action()
        if js == tuple(pause_js[self.player]):
            sge.game.current_room.pause()
 

Author:  Thunder Dragon [ Sun Dec 27, 2015 10:39 pm ]
Post subject:  Re: Gamepad Support In Your Games

Vitiman wrote:
DJ Coco wrote:
afaik MMF2 has some customizability thing set up from the start, doesn't it?

I think there's an extension for gamepad support, but I'm not sure if it has analog stick functionality or not.

It does! There are several extensions for gamepad support, all of which have analog integration. I should know -- I'm working to include more analog options in Sporklops as we speak. ;)

As far as the "customizability thing set up from the start" as you call it... eh, it's absurdly limited. It's fine for new users, I suppose, but for advanced users, you'll want to create a custom control system of some sort. The Sonic Worlds Engine has a fine demonstration of a custom control engine that is totally worth checking out.

Author:  No Name [ Mon Jan 04, 2016 4:39 pm ]
Post subject:  Re: Gamepad Support In Your Games

DJ Coco wrote:
It's even simpler in Unity. I have no idea how it's done in any other engine, but afaik MMF2 has some customizability thing set up from the start, doesn't it?

Blender Game Engine supports gamepad

Author:  Sloshy [ Thu Jan 07, 2016 8:09 pm ]
Post subject:  Re: Gamepad Support In Your Games

i've been using JoyToKey which assigns gamepad buttons as keyboard keys and it works very well.however i hadn't had a need to for an analog stick so i don't know how well it works or how. i suspect that it doesn't support "deadzones" or analog sensitivity.

Author:  onpon4 [ Thu Jan 07, 2016 10:02 pm ]
Post subject:  Re: Gamepad Support In Your Games

I wouldn't rely on tools like that. Never mind the inconvenience to the user, they're unreliable in my experience. Interesting fact: the thing that convinced me to put gamepad controls in BLS was because JoyToKey was causing jumps to be prematurely aborted when you turned around, for some reason. To be fair, I haven't used JoyToKey in years (since I don't use Windows, obviously), and QJoyPad (a similar tool for X systems) hasn't given me as much trouble as I remember JoyToKey giving me, so it might depend on the tool. In any case, I wouldn't depend on it. Getting proper native gamepad support working isn't that hard.

Author:  Sloshy [ Fri Jan 08, 2016 12:36 am ]
Post subject:  Re: Gamepad Support In Your Games

thats nice. it has never done anything like that to me so I have no problem using it. its not like its really that inconvenient to just open the program up and set the keys real quick. besides I wasn't even saying it was the ideal method or anything, more like its better than nothing if a game isnt programmed to support gamepad input.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/