Multiple Controllers ~ Who pressed the A Button?

I've been looking at the Controller demo project in the ODK and it's very good about illustrating how to work the controller with button presses and joystick movements.  What's not as clear is how to tell which controller is doing the press; with all 4 controllers linked, pressing A on any of them will light up the same virtual "A button" on the screen.

In the documentation, there's a way to get a controller object from a player number:
OuyaController c = OuyaController.getControllerByPlayer(playerNum);
There must be a way to do the opposite upon a button press, but the answer is eluding me.  Anyone have a solution to this?

Best Answer

  • PelzMorphPelzMorph Posts: 5Member
    edited January 2013 Answer ✓
    Hi,

    there is an easy way to achieve this.

    You need to override your onKeyDown Method in your MainActivity. At this position the OuyaController Class needs to be updated with:

    boolean handled = OuyaController.onKeyDown(pKeyCode, pKeyEvent);

    The KeyDownEvent should be redirected to a Method where input for each player is handled.

    At this position you can read the deviceId with: event.getDeviceId();

    Then you call:

    OuyaController controller = OuyaController.getControllerByDeviceId(deviceId);

    And here you are you got every Event for the right Controller and Player.

    If you want to see a simple codesample you can take a look at this C++-Example here,

    I hope this helps.

    Best

    PS: Best pratice would be to create an abstract Activity which handles this input for you. Each other Activity derives from this Activity and can handle the event.

    PPS: If you want to check for a KeyDown Event in your update-Method in your game its best to register the device id before the game starts. To achive this, you need to implement the above process and some gamelogic to find the device id for each players controller.
    Post edited by PelzMorph on

Answers

  • ReaperOfShibuyaReaperOfShibuya Posts: 3Member
    edited January 2013
    Thanks for the advice.  In theory, that sounds like a solid approach, but I seem to be running into another hiccup.

    Presently, the OuyaController.getControllerByDeviceId(deviceId) call returns null.  (code snippet below, added to ContollerView.java in the controller testing project of the ODK)

    public boolean onKeyDown(int keyCode, KeyEvent event) {    

        int deviceID = event.getDeviceId();

        OuyaController controller = OuyaController.getControllerByDeviceId(deviceID);

        System.out.println("Controller number " + controller.getPlayerNum());

        ...

    }

    The deviceID is coming thought fine (it's like 5 at the moment).  Could the null controller be caused by what you described in the PPS, or is it a different problem?

    In response to your PS, I was planning on making a "smart ouya activitiy" class that would handle all of these button stuff; also a solid approach.

    Thanks for your help.

    Post edited by ReaperOfShibuya on
  • PelzMorphPelzMorph Posts: 5Member
    So if i see it correctly you need to change your code to this:

    public boolean onKeyDown(int keyCode, KeyEvent event) {    

        boolean handled = OuyaController.onKeyDown(keyCode, event);

        int deviceID = event.getDeviceId();

        OuyaController controller = OuyaController.getControllerByDeviceId(deviceID);

        System.out.println("Controller number " + controller.getPlayerNum());

        ...

    }

    I think the OUYA Controller Class recognizes Controller with this codeLine: OuyaController.onKeyDown(keyCode,Event) .

    But i am not sure. In my code this codeline is always executed first and the controller isnt null.


  • ReaperOfShibuyaReaperOfShibuya Posts: 3Member
    Ah, that's a sneaky little side effect.  That was the issue; thanks again!
Sign In or Register to comment.