Get gamepad by player number?

46 views
Skip to first unread message

Will Blanton

unread,
Nov 14, 2014, 4:06:43 PM11/14/14
to haxef...@googlegroups.com


Using this:

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;

class MenuState extends FlxState
{
var text:FlxText;
override public function create():Void
{
text = new FlxText(10, 10, 100);
add(text);
super.create();
}
override public function update(elapsed:Float):Void
{
if (FlxG.gamepads.lastActive != null) text.text = "" + FlxG.gamepads.lastActive.id;
else text.text = "No controller detected";
super.update(elapsed);
}
}

I'm testing this with one controller, and I am getting different behavior in different targets.

in Flash, it first tells me my controller id is 0. If I unplug the controller and plug it back in, it remains 0.
in Windows and Android (OUYA), the controller id goes up by 1 every time I unplug it and plug it back in.

This makes it impossible for me to determine which controller is for player 1 and which is for player 2 by using the id. (Aside from some wonky code that would just assign it to the first unused id)

Is there a way to get a controller by player number instead of ID?

Gama11

unread,
Nov 17, 2014, 7:13:12 AM11/17/14
to haxef...@googlegroups.com
What flixel version? There've been some improvements to this on the dev branch IIRC.

Jeru Sanders

unread,
Nov 17, 2014, 1:19:53 PM11/17/14
to
I had this problem a few months ago, it's a known issue in the master branch, but if you move to the dev branch it's been resolved.

Will Blanton

unread,
Nov 26, 2014, 4:32:14 PM11/26/14
to haxef...@googlegroups.com
Just updated everything to be sure (sorry I've been busy all week), still getting new ids with this code:

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;

class MenuState extends FlxState
{
var text:FlxText;
override public function create():Void
{
FlxG.autoPause = false;
text = new FlxText(10, 10);
add(text);
super.create();
}
override public function update(elapsed:Float):Void
{
if (FlxG.gamepads.lastActive != null) text.text = "Controller ID : " + FlxG.gamepads.lastActive.id;
else text.text = "No controller detected";
super.update(elapsed);
}
}

Will Blanton

unread,
Nov 26, 2014, 4:53:35 PM11/26/14
to haxef...@googlegroups.com
Also now it's tough to test this because unplugging a controller crashes the game with this code:

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.input.gamepad.FlxGamepad;
import flixel.input.gamepad.XboxButtonID;
import flixel.text.FlxText;

class MenuState extends FlxState
{
var text:FlxText;
override public function create():Void
{
FlxG.autoPause = false;
text = new FlxText(10, 10);
add(text);
super.create();
}
var pad:FlxGamepad;
override public function update(elapsed:Float):Void
{
if (FlxG.gamepads.lastActive != null) text.text = "Controller ID : " + FlxG.gamepads.lastActive.id;
else text.text = "No controller detected";
if (pad == null) pad = FlxG.gamepads.lastActive;
else {
if (pad.buttons == null) text.text = "BUTTONS DEAD";
else if (pad.pressed(XboxButtonID.A)) text.text = text.text + " + A BUTTON PRESSED";
}
super.update(elapsed);
}
}

When the controller is unplugged, the buttons become null, but the controller does not. Not sure if this is intended or not.
Reply all
Reply to author
Forward
0 new messages