Search Unity

Why does TryGetFeatureValue() uses out instread of return?

Discussion in 'AR/VR (XR) Discussion' started by Tayfe, Mar 10, 2020.

  1. Tayfe

    Tayfe

    Joined:
    Nov 21, 2011
    Posts:
    103
    I've been working with VR for a few years now and with different frameworks (VRKit and Oculus SDK) and both of them had their pros and cons. Today I started working with the new XR system to switch from Oculus SDK to the new one and I apprectiate that there is one new system that tries to combine all kind of different VR system.

    But there is one thing that already bothers me a lot: When I want to get the state of some button I have to use
    InputDevice.TryGetFeatureValue()
    which expects an out parameter. What's the benefit of this? All other SDKs used "normal" return values which seem much more practical to me. This way I also have to declare a variable first and use a second line to store some value within it:

    Code (CSharp):
    1. int trigger;
    2. controller.TryGetFeatureValue(CommungUsages.trigger, out trigger);
    vs

    Code (CSharp):
    1. int trigger = controller.TryGetFeatureValue(CommunUsages.trigger);
    I just don't like the first way. Is there any good reason for this?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Presumably it's so you can tell if the attempt actually succeeded (you're ignoring the return value in your first example, but you wouldn't have to).
     
  3. StayTalm_Unity

    StayTalm_Unity

    Unity Technologies

    Joined:
    May 3, 2017
    Posts:
    182
    Hello!
    This is because there are multiple reasons a trigger value could be false.
    1) It could not be pressed
    2) It could not be a feature on that device
    3) The device reference could be invalid (no longer connected)

    The developer is going to want to respond to each of these

    And so the out value represents the actual value of the trigger, and the boolean result represents if we were able to retrieve any kind of trigger value at all.

    Btw, I learned this recently, but you can skip the early initialization, and declare the out parameter in-line.
    Code (CSharp):
    1. if(controller.TryGetFeatureValue(CommungUsages.trigger, out int trigger))
     
    JoeStrout likes this.