Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Two Controllers Same UniqeIdentifier (GetHashCode())

Discussion in 'Scripting' started by Birnchen-Studios, Sep 13, 2017.

  1. Birnchen-Studios

    Birnchen-Studios

    Joined:
    Sep 28, 2014
    Posts:
    4
    Hey @ll,

    i have a big Problem and sorry for my bad english^^.
    I am Creating an Sportssimulator and have two Controller (ActionXL).
    I cant change the COntroller, its a must from the Customer....

    So normaly do something like this:
    Code (CSharp):
    1.  
    2.         string[] names = Input.GetJoystickNames();
    3.  
    4.         Debug.Log("Connected Joysticks:");
    5.         for (int i = 0; i < names.Length; i++)
    6.         {
    7.             deviceUniqueIdentifier[i] = names[i].GetHashCode();
    8.             Debug.Log("Joystick " + (i + 1) + " = \"" + names[i] + "\" ID: " + deviceUniqueIdentifier[i]);
    9.         }
    10.  
    So but currently the Problem is, both Controller have the same deviceUniqueIdentifier (
    Code (CSharp):
    1. names[i].GetHashCode()
    ).

    Now i am looking for a way to identifie them, while the Controllers are on the backside of a Surfboard and fix in the side. Cant go to the Controller and change the Side, i must do them in the backend of my code and save in the Database (controller one = left or right). The complete Callibration prozess is dependent on this (both side are not exactly same in hight and build).

    Can Anyone help me Please??

    My German is better then my english, if anyone here and can help me in German.

    Thx alot.

    Greetings
    Pierre
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    4,704
    From the documentation:
    https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx
    This can be shown to be quite certain since you're getting a HashCode of a string. Thing is a hashcode is an int, giving 2^32 unique values. Where as a string can have an infinite number of unique values (though technically finite since limited by memory constraints, but still of course is more than 2^32).

    Why not use the index of the joystick as its unique identifier?

    Again from the documentation:
    https://docs.unity3d.com/ScriptReference/Input.GetJoystickNames.html
    There can only be 1 joystick in a given slot... so the index of that slot is unique to that joystick.

    ...

    Unless you mean that you don't which index a controller is going to end up in, and you need to work out which is which. So say the first time you configure 'left' is in slot 0, but the next time it happens to be in 1, for whatever reason.

    Are the names themselves unique? Check those, instead of calling GetHashCode.