Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using optional parameters, out of order

Discussion in 'Scripting' started by pmolodowitch, Feb 16, 2019.

  1. pmolodowitch

    pmolodowitch

    Joined:
    Feb 16, 2019
    Posts:
    2
    As I understand it, in C# you should be able to use syntax like this:

    var result = SomeFunction(arg1, arg2, arg4: "aValue")

    ...assuming arg3 and arg4 are optional parameters. However, when I try to do this - specifically with Physics2D.RaycastAll, I get errors:

    Code (CSharp):
    1. var results = Physics2D.RaycastAll(curPosition, new Vector2(1, 0), 0.0f, minDepth: GetZMax());
    Code (csharp):
    1.  
    2. Error    CS1501    No overload for method 'RaycastAll' takes 4 arguments
    3.  
    When I browse to the definition of Physics2D.RaycastAll, I see several overloads, including:

    Code (CSharp):
    1. public static RaycastHit2D[] RaycastAll(Vector2 origin, Vector2 direction, [Internal.DefaultValue("Mathf.Infinity")] float distance, [Internal.DefaultValue("DefaultRaycastLayers")] int layerMask, [Internal.DefaultValue("-Mathf.Infinity")] float minDepth, [Internal.DefaultValue("Mathf.Infinity")] float maxDepth);
    This is a different way of specifying default parameters then I'm used to, and different than what I see in the docs:

    Code (CSharp):
    1.  
    2. public static RaycastHit2D[] RaycastAll([URL='https://docs.unity3d.com/ScriptReference/Vector2.html']Vector2[/URL] origin, [URL='https://docs.unity3d.com/ScriptReference/Vector2.html']Vector2[/URL] direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
    (from https://docs.unity3d.com/ScriptReference/Physics2D.RaycastAll.html)

    Does anyone know what this "Internal.DefaultValue" is all about, or how to specify optional args out of order here?
     
    Last edited: Feb 17, 2019
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
  3. pmolodowitch

    pmolodowitch

    Joined:
    Feb 16, 2019
    Posts:
    2
    Yeah, that's the same document I was reading. However, I don't seem to be able to use it like that demonstrates. In other words, this SHOULD work, according to that page:

    Code (CSharp):
    1. var results = Physics2D.RaycastAll(curPosition, new Vector2(1, 0), 0.0f, minDepth: -20);
    ...but it doesn't. I get the error mentioned above? Does this work for you?

    My best guess is that it doesn't work because the "default" value isn't specified through "normal means", but through sytanx like this:

    Code (CSharp):
    1. [Internal.DefaultValue("DefaultRaycastLayers")] int layerMask
    Anyone know what that is? All I can tell is that it's applying the
    Internal.DefaultValue("DefaultRaycastLayers")
    attribute to the layerMask parameter, but I have no idea what that would mean / how it works / how it's different than just doing
    int layerMask = DefaultRaycastLayers


    And yes, I know I can make this work by just doing:

    Code (CSharp):
    1. var results = Physics2D.RaycastAll(curPosition, new Vector2(1, 0), 0.0f, Physics2D.DefaultRaycastLayers, -20);
    However, I'm trying to get a better understanding of C# / Unity here. Thanks!
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Hmmm, honestly I am not sure.
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Iirc, Unity uses .net 4.6 and c# 6
     
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Well that should be fine then according to this quote from the Microsoft page: