Detects what type of Game Pad the player is using.

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Neyunse
Newbie
Posts: 8
Joined: Fri Aug 17, 2018 7:08 pm
Projects: Roses Of Love
Organization: KagariSoft
Github: Neyunse
itch: KagariSoft
Location: Argentina
Contact:

Detects what type of Game Pad the player is using.

#1 Post by Neyunse »

Hi! This is my first post here, I want to share with you this code I made. This code detects the type of joystick the player is using to play, it's a simple code and can be improved. In this case this detects if the joystick is from PS4 or XBOX, and if it is not from any of these brands it returns "Error".
My idea was to be able to detect the type of control and thus show in pictures the corresponding icons on the menus instead of text.
Of course this code only works if the player already has the controller connected before running the game.
This code uses the pygame module that you are integrating into renpy
pygame joystick doc: https://www.pygame.org/docs/ref/joystick.html

Code: Select all

init python:
    import pygame
    discon = False
    def GamePadType(): 
        global discon
        pygame.joystick.quit()
        pygame.joystick.init()
        joystick_count = pygame.joystick.get_count()
        for i in range(joystick_count):
            joystick = pygame.joystick.Joystick(i)
            joystick.init()
            name = joystick.get_name()
            GAMEPAD = None
            # Words to search for in the string
            sony = "PS4"
            wirl = "Wireless"
            micr = "Microsoft"
            xbox = "XBOX"
            if sony in name or wirl in name:
                # If True then the joystick is PS4
                print "PS4"
                GAMEPAD = True
            elif micr in name or xbox in name:
                # If False then the joystick is Xbox
                print "XBOX"
                GAMEPAD = False
            else:
                # If the joystick is not a PS4 or Xbox GAMEPAD it is set to None
                GAMEPAD = None
                
            if GAMEPAD:
                #PS4
                return True
            elif GAMEPAD == None:
                #Here returns error to show a message that the joystick is not a PS4 or XBOX
                return "Error"
            else:
                #XBOX
                return False
        if not joystick_count:
            if not discon:
                print "joystick no connected"
                discon = True
        else:
            discon = False
Example on a screen

Code: Select all

screen gpad():
    modal True
    frame:
        background Solid("#000")
        left_padding 10
        right_padding 10
        top_padding 10
        bottom_padding 10
        has vbox
        if GamePadType()==True:
            text "ps4" color "#fff"
        elif GamePadType()=="Error":
            text "We do not detect the controller, this controller may not be XBOX or PS4" color "#fff"
        elif GamePadType()==False:
            text "xbox" color "#fff"
        textbutton "Return" action Return()
Example on dialog

Code: Select all

label start:
    if GamePadType()==True:
        e "ps4 controller"
    elif GamePadType()=="Error":
        e "We do not detect the controller, this controller may not be XBOX or PS4"
    elif GamePadType()==False:
        e "xbox controller"
Last edited by Neyunse on Sun Jul 25, 2021 12:07 pm, edited 3 times in total.
----------------------------------------------------
My Twitter: @Neyunse
My Discord: ねゆんせ#7916
----------------------------------------------------
KagariSoft Twitter: @KagariSoft
My Discord Server

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Detects what type of Game Pad the player is using.

#2 Post by Imperf3kt »

This looks interesting.
I've noticed though, that the same controller reports a different name on different operating systems through the game pad calibration screen.

Will this affect anything?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Neyunse
Newbie
Posts: 8
Joined: Fri Aug 17, 2018 7:08 pm
Projects: Roses Of Love
Organization: KagariSoft
Github: Neyunse
itch: KagariSoft
Location: Argentina
Contact:

Re: Detects what type of Game Pad the player is using.

#3 Post by Neyunse »

Imperf3kt wrote: Wed Jan 20, 2021 10:48 pm This looks interesting.
I've noticed though, that the same controller reports a different name on different operating systems through the game pad calibration screen.

Will this affect anything?
The truth is I don't know, I tried this on Windows 10. But the different names can be added in the if, I followed what the pygame documentation said.

Code: Select all

if name == "PS4 Controller" or name=="Wireless Controller" or name=="(Another name for the ps4 controller)"

elif name=="Xbox 360 Controller" or name=="Controller (XBOX 360 For Windows)" or name=="(Another name for the Xbox controller)" 
----------------------------------------------------
My Twitter: @Neyunse
My Discord: ねゆんせ#7916
----------------------------------------------------
KagariSoft Twitter: @KagariSoft
My Discord Server

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Detects what type of Game Pad the player is using.

#4 Post by Imperf3kt »

I'd suggest changing it to a conditional that checks if "PS4" or Xbox or Microsoft, etc is in the name.

I was actually halfway through making such a thing a few days ago, so I'll finish it when I get time and report my findings, if I get a chance.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Neyunse
Newbie
Posts: 8
Joined: Fri Aug 17, 2018 7:08 pm
Projects: Roses Of Love
Organization: KagariSoft
Github: Neyunse
itch: KagariSoft
Location: Argentina
Contact:

Re: Detects what type of Game Pad the player is using.

#5 Post by Neyunse »

Imperf3kt wrote: Thu Jan 21, 2021 12:16 am I'd suggest changing it to a conditional that checks if "PS4" or Xbox or Microsoft, etc is in the name.

I was actually halfway through making such a thing a few days ago, so I'll finish it when I get time and report my findings, if I get a chance.
I updated the code with your suggestion. :D
----------------------------------------------------
My Twitter: @Neyunse
My Discord: ねゆんせ#7916
----------------------------------------------------
KagariSoft Twitter: @KagariSoft
My Discord Server

Post Reply

Who is online

Users browsing this forum: No registered users