argument_count doesn't count number of arguments passed

Answered

Comments

8 comments

  • Daniel Willen


    So in the second image I run printCoordiante() and printCoordinate(coordinate2) 

    When running the debugger the argument_count is always as many arguments as specified by the function, but not how many were actually passed.

    0
    Comment actions Permalink
  • Anastasia Melnikova

    Looks like a feature to me. All missing arguments are passed as undefined now.

    0
    Comment actions Permalink
  • Daniel Willen

    Yes, this could be a feature but it's not very well documented. What is then the point of argument_count then?

    For instance if I pass foo(a,b,c) and the argument_count is three, or if I pass foo() which in turn passes three undefined arguments, the argument_count is also three.


    I'ts not like I passed foo(null,null,null) or foo(default,default,default) like in some other languages. I don't know it doesn't seem right to me.

     

    Do you know which is the "correct" new way of having a function take a variable number of arguments?

     

     

     

    0
    Comment actions Permalink
  • Anastasia Melnikova

    You either use argument_count or named arguments. I prefer to combine both:

    /// @function f( [a=0], [b=0] )
    /// @arg [a=0]
    /// @arg [b=0]

    f = function( a ) { // no b declared here
    a = is_undefined( a ) ? 0 : a;
    var b = ( argument_count > 1 ) ? argument[1] : 0; // declare b here
    }

    In this case argument_count can be 1 or 2, but never 0.

    0
    Comment actions Permalink
  • Daniel Willen

    This means you are forced to define your functions with the minimum variable definition so that argument_count can "increment" properly as you call the funciton with more arguments than the funciton has in its definition.


    In my minds eye, if I run f(a) i'm passing one argument. If I'm passing f() I'm passing zero arguments. 
    In your example, argument_count is always at least one, due to your definion. But it can also be 2, if you had written f= function(a,b). 

    There's no way by just looking at "f = function(a)" that you would ever know it could ever take more arguments, unless you peek the code or write the javadoc style comments.

    My gripe is that I've never explicitly stated that I'm passing an undefined variable, but I guess it's just a matter of definition.

     



     

     

    0
    Comment actions Permalink
  • Francisco Dias

    Daniel Willen As the YYG team stated JSDocs are a thing to stay and will  probably be the resposible for the autocomplete from now on. So we better get used to them :)

    some members from the YYG team actually answered to this topic I posted sometime ago.. and it seems to be something like this the path for the future of autocomple

    https://help.yoyogames.com/hc/en-us/community/posts/360010820798--SUGGESTION-Detailed-idea-for-auto-completion

    [NOTE] I'm not saying this is the way it is going to be.. but it seems to also be their idea at the moment.

    so the best way to do it would be

    /// @function f()
    /// @param a
    /// @param b
    /// @param c
    function f(a)
    {
       b = argument_count > 1 ? argument[1] : undefined
       c = argument_count > 2 ? argument[2] : undefined
    }


    looking at this again... you could simply do and you won't acually need argument count:


    /// @function f()
    /// @param a
    /// @param b
    /// @param c
    function f(a, b, c)
    {
       b = is_undefined(b) ? 100 : b;   // 100 is your default value
       c = is_undefined(c) ? 100 : c;   // 100 is your default value
    }

    Is there actually a use case where you really need to know the argument count, for optional arguments?


    regarding supressing the message!! I think that is a bug that is being addressed as the code compiles and it doesn't crash the game whatsoever

    0
    Comment actions Permalink
  • Anastasia Melnikova

    Francisco Dias

    > Is there actually a use case where you really need to know the argument count, for optional arguments?

    /// @function create_list( [...] )
    /// @desc returns pre-populated ds_list
    /// @arg {Any} [...]
    /// @return {ds_list}
    function create_list( ) {
      var _list = ds_list_create();
      for (var i = 0; i < argument_count; i++ ) {
        _list[| i ] = argument[ i ];
      }
      return _list;
    }

    Every function has [...] optional argument at the end of argument list now and I like it.

    0
    Comment actions Permalink
  • YoYo QA Dept

    Hello,

     Thank you all for getting in touch and having discussions on the GMS2.3 beta. We recently released the next beta version IDE v23.1.1.149 Runtime v23.1.1.135 that appears to fix your issue regarding argument_count.

    I have re-opened an issue regarding the error as that was initially fixed but has been reintroduced.

    Regards,
    Scott

    0
    Comment actions Permalink

Please sign in to leave a comment.