Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Xbox controllers order persistency

Discussion in 'Input System' started by bl4st, Apr 4, 2021.

  1. bl4st

    bl4st

    Joined:
    Feb 19, 2015
    Posts:
    6
    Hi, i am trying to create a system to be able to have a dynamic amount of xbox controllers playing the game.

    I had something working by calling Input.GetJoystickNames() and using the position of non empty names in the array to determine the joystick number. But there is a problem with this approach.

    Lets say for example i have 2 controllers and my name array will look like that

    Code (CSharp):
    1. string[] name = Input.GetJoystickNames()
    2.  
    3. name[0] "Xbox Controller" // Player 0 using joystick 0
    4. name[1] "Xbox Controller" // Player 1 using joystick 1
    To access the input of the players i use the positions in the array as their "joystick index".
    in this case, Players 0 and 1 will respectively access the inputs from joysticks 0 and 1, this worked even when the array was filled with "ghost controllers" (this is how we will call the empty names Input.GetJoystickNames() returns when controllers are plugged / unplugged) ex:

    Code (CSharp):
    1. name[0] ""
    2. name[1] "Xbox Controller" // Player 0 using joystick 1
    3. name[2] ""
    4. name[3] "Xbox Controller" // Player 1 using joystick 3
    At first this used to work exactly as i wanted i to, when i disconnected / reconnected a controller, the name at its position in the array became empty and upon reconnection, it would reappear at the same position, there was consistency.

    MY PROBLEM :

    After a bit of testing messing with reconnections / disconnections, i realised "sometimes" the controller don't go back at the same place in the array but instead, gets a new place further, leaving his first position apparently empty, forever.

    I looked if it was possible to find some "controller serial number" or some kind of unique and persistant ID assigned to each controllers but i couldnt find answers so here i am. Thank you for your help.
     
  2. bl4st

    bl4st

    Joined:
    Feb 19, 2015
    Posts:
    6
    I think i found a solution, didin't realised there was a new input system

    ¯\_(ツ)_/¯