Detect controller usage?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
papillon
Arbiter of the Internets
Posts: 4107
Joined: Tue Aug 26, 2003 4:37 am
Completed: lots; see website!
Projects: something mysterious involving yuri, usually
Organization: Hanako Games
Tumblr: hanakogames
Contact:

Detect controller usage?

#1 Post by papillon »

Because I have a minigame that works only with the mouse, I need to be able to switch to an alternate minigame if someone is using a gamepad/controller. Right now this works by making the player set an option in the menu to say that they do not have a mouse.

It would be a lot tidier to detect whether a player is using a controller and switch to controller mode. HOWEVER, I do not wish to detect simply whether a gamepad exists, because just because it's plugged in doesn't mean the player actually uses it for VNs. I would like to fire off a bit of code only when the player has actually pressed a button on the controller.

However, all I can figure out of what happens when a button is pressed on the controller is that it fires an event as specified in the keybindings. Once the event is fired the game no longer knows nor cares that the gamepad was used, and I can't see any way to insert code to do anything before the event fires.

How can I catch and set a flag for "the player is actively using a controller"?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2397
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Detect controller usage?

#2 Post by Ocelot »

My solution was to create a custom displayable listening for keyboard/gamepad events and set a variable which shows, what was used last. For it to work properly, undocumented config variable config.pass_joystick_events had to be set to True, otherwise RenPy would consume events. In the end it would look like:

Code: Select all

init python:
    class InputChecker(renpy.Displayable):
        def __init__(self, **kwargs):
            super(InputChecker, self).__init__(**kwargs)

        def render(self, width, height, st, at):
            return renpy.Render(0, 0)

        def event(self, ev, x, y, st):
            import pygame
            if ev.type in (pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEBUTTONUP, pygame.MOUSEBUTTONDOWN):
                store.current_input = "KB"
            elif ev.type in (pygame.JOYBUTTONUP, pygame.JOYBUTTONDOWN, pygame.JOYAXISMOTION, pygame.JOYBALLMOTION, pygame.JOYHATMOTION):
                store.current_input = "GP"
            return None

define config.pass_joystick_events = True
default current_input = "KB"

screen test_input():
    add InputChecker()

init python:
    config.overlay_screens.append("test_input")

screen display_current_input():
    label "[current_input]"

label start:
    show screen display_current_input
    "...   "
    ".. .  "
    "..  . "
    "..   ."
    ". .  ."
    ".  . ."
    ".   .."
    " .  .."
    "  . .."
    "   ..."
    return
< < insert Rick Cook quote here > >

User avatar
papillon
Arbiter of the Internets
Posts: 4107
Joined: Tue Aug 26, 2003 4:37 am
Completed: lots; see website!
Projects: something mysterious involving yuri, usually
Organization: Hanako Games
Tumblr: hanakogames
Contact:

Re: Detect controller usage?

#3 Post by papillon »

Thanks for this! We had already put in a quick kludge (a screen catching key-releases) but this might be a better approach in the long run, I'll test it out later.

Post Reply

Who is online

Users browsing this forum: Google [Bot]