Controller Dpad Support

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Controller Dpad Support

Post by tentus »

bartbes wrote:By keeping the old state and detecting changes.
So... a table of signed doubles for every controller and every hat on them? Eugh. I feel like there has to be a more elegant way to do it, but my brain is giving me nothing.

@nevon: you'd be surprised how irritating even a quarter second delay is. I may end up going with that anyway, but it still feels like I'm shorting the player somehow.
Kurosuke needs beta testers
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Controller Dpad Support

Post by nevon »

tentus wrote:
bartbes wrote:By keeping the old state and detecting changes.
@nevon: you'd be surprised how irritating even a quarter second delay is. I may end up going with that anyway, but it still feels like I'm shorting the player somehow.
That's how they do it in just about every console title I have ever played (though it's not a static delay, so it goes faster the longer you keep the joystick tilted).
GloryFish
Prole
Posts: 19
Joined: Tue Jan 11, 2011 4:43 pm

Re: Controller Dpad Support

Post by GloryFish »

I've always done it by keeping track of state.

In fact, if you create a wrapper around the joystick it becomes very straightforward and useful. Create a JoystickManager class and give it an update method a state table and a previousState table.

In update() copy state to previousState, then go through your joystick's buttons and set state values for each of them. You can also set a bunch of values for new button presses (if it's pressed in state and not pressed in previousState then it's a newPress!).

Call update once per tick and you now have a convenient wrapper around your joystick and you can easily check for single button presses without having to muck around with timers and all that.

You can also take it a step further and wrap your JoystickManager class inside an input manager class and have a single place to check for user input.

If you don't want to do all that, just copy my code:

https://github.com/GloryFish/RedditGame ... anager.lua
https://github.com/GloryFish/RedditGame ... /input.lua

The best part about all of that is you can define some simple actions like "jump", "fire", "back" and just check for those in your code, the input manager can handle mapping those actions to either keyboard or gamepad button presses.

Here's an example:

https://github.com/GloryFish/RedditGame ... e.lua#L208

On line 208 I'm checking to see if the player pushed the jump button (on the keyboard or joystick) for the first time (a newpress), and, if so, I trigger a jump. Then on line 217, I check to see if the button is being held down and alter the gravity based on that. Using the input manager allows me to make that distinction and keeps the gameplay code simpler.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Controller Dpad Support

Post by TechnoCat »

I did something similar here too: https://bitbucket.org/dannyfritz/avian- ... Player.lua
Although mine only assumes d-pad + 1 button, after you instantiate the Player class, it doesn't know if it is keyboard or joypad anymore.

Example where it checks to make sure the player isn't pressing any buttons: https://bitbucket.org/dannyfritz/avian- ... othing.lua
Last edited by TechnoCat on Thu Jan 27, 2011 9:42 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Controller Dpad Support

Post by bartbes »

And since we're showing off input libs: https://bitbucket.org/bartbes/jail.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests