Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamepads cannot be easily manually registered or deregistered #4911

Open
alice-i-cecile opened this issue Jun 3, 2022 · 2 comments
Open
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Controversial There is active debate or serious implications around merging this PR

Comments

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jun 3, 2022

What problem does this solve or what need does it fill?

Users want to manually register and deregister gamepads as part of tests to verify that gamepad inputs are working correctly.

The critical methods / fields on the Gamepads struct are private however, which makes doing so rather convoluted.

What solution would you like?

Make the register and deregister methods on Gamepads public.

What alternative(s) have you considered?

We could expose the underlying HashSet directly, but that flavor of API is somewhat harder to read.

Users can already do this with a horrible workaround where they manually run the public gamepad_connection_system.

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jun 3, 2022
@alice-i-cecile
Copy link
Member Author

The aforementioned terrible workaround:

impl GamepadRegistration for World {
    fn register_gamepad(&mut self, gamepad: Gamepad) {
        // Generate a synthetic event
        let gamepad_event = GamepadEvent(gamepad, GamepadEventType::Connected);
        let mut events_resource = self.resource_mut::<Events<GamepadEvent>>();
        events_resource.send(gamepad_event);

        // Ensure that the Gamepads resource exists to avoid pointless panics
        if self.get_resource::<Gamepads>().is_none() {
            self.init_resource::<Gamepads>();
        }

        // Manually run the gamepad_connection_system on the World to process the event just sent
        let mut system = IntoSystem::into_system(gamepad_connection_system);
        system.initialize(self);
        system.run((), self);
    }

    fn deregister_gamepad(&mut self, gamepad: Gamepad) {
        // Generate a synthetic event
        let gamepad_event = GamepadEvent(gamepad, GamepadEventType::Disconnected);
        let mut events_resource = self.resource_mut::<Events<GamepadEvent>>();
        events_resource.send(gamepad_event);

        // Ensure that the Gamepads resource exists to avoid pointless panics
        if self.get_resource::<Gamepads>().is_none() {
            self.init_resource::<Gamepads>();
        }

        // Manually run the gamepad_connection_system on the World to process the event just sent
        let mut system = IntoSystem::into_system(gamepad_connection_system);
        system.initialize(self);
        system.run((), self);
    }
}

@alice-i-cecile alice-i-cecile added X-Controversial There is active debate or serious implications around merging this PR and removed D-Trivial Nice and easy! A great choice to get started with Bevy labels Jun 3, 2022
@alice-i-cecile
Copy link
Member Author

This is a duplicate of #3808 apparently. I still disagree with the stance taken by @cart there: I think that being able to mock gamepad inputs correctly is a very important goal, and as shown by the workaround, users can cause the list of registered gamepads to desync from the list of "actually connected" gamepads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant