variable argument scripts

Discussion in 'Programming' started by Pfap, Feb 14, 2019.

  1. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    I have been over the manual sections about scripts and passing arrays to scripts. Maybe I missed something, but I'm making this posting not because I'm having trouble finding a solution I already know of several work arounds. I'm making this posting, because I want to finally figure out how to get a variable number of arguments working with scripts cleanly.

    Here's the relevant code.

    Script create_popup:
    Code:
     
    show_debug_message(argument_count);
    
    if argument_count == 5{
     
    var xpos = argument[0];
    var ypos = argument[1];
    var text = argument[2];
    var button_amount = argument[3];
    var b_string = argument[4];
    show_debug_message(b_string);
    }
    
     
    if argument_count == 6{
     
    var xpos = argument[0];
    var ypos = argument[1];
    var text = argument[2];
    var button_amount = argument[3];
    var b_string = argument[4];
    var c_string = argument[5];
    show_debug_message(b_string);
    show_debug_message(c_string);
    }
    
    And here is how I am calling it:
    Code:
    //create an array with desired amount of arguments
    var args = [room_width/2,room_height/2,"Are you sure?",1,"Ok"]
    create_popup(args);
    
    This proceeds to crash, because none of my var's are being set in the beginning of the script. Also, the argument_count variable is displaying 1 in the console output. Which I take to mean that the array of 5 arguments is being accepted as 1 array.


    This is the portion of the manual that has me confused on how to use the built in argument[n] array.



    tl;dr

    How do you use the built in argument[n] array to create variable argument scripts?
     
  2. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    789
    Last edited: Feb 14, 2019
    SnotWaffle Studios likes this.
  3. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    The manual says
    I am passing in a variable number of arguments as an array of values.
     
  4. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    789
    Yeah, the wording does make you think that you should send in the arguments as an array. But I am pretty sure you send in the arguments normally, and then you can get them using the "argument[0], argument[1]" array.
     
    SnotWaffle Studios, Binsk and Pfap like this.
  5. 8BitWarrior

    8BitWarrior Member

    Joined:
    Jun 20, 2016
    Posts:
    43
    The argument[] array is created automatically when you pass multiple arguments to a script. You do not pass an actual array to make use of this.
    So, if you had a script that added numbers together, you could have this...
    value = AddNumbers(2, 5);

    Code:
    /// AddNumbers(a,b)
    var val1 = argument0;
    var val2 = argument1;
    var result = val1+val2;
    
    But if you wanted it to support a differing number of arguments, you could change it like so...
    value = AddNumbers(2, 5, 10);
    Code:
    /// AddNumbers(a,b,...)
    switch(argument_count)
    {
        case 2: return argument[0] + argument[1];
        case 3: return argument[0] + argument[1] + argument[2];
        case 4: return argument[0] + argument[1] + argument[2] + argument[3];
    }
    
    Passing an array as an argument isn't working as you are expecting it to. I'll show how you would have to use an array if you passed it...
    values = [2, 5, 10];
    value = AddNumbers(values);
    Code:
    /// AddNumbers(valueArray)
    var values = argument0;
    switch(array_length_1d(values))
    {
        case 2: return values[0] + values[1];
        case 3: return values[0] + values[1] + values[2];
        case 4: return values[0] + values[1] + values[2] + values[3];
    }
    
    I hope this makes the difference a bit more clear.
     
    Pfap and Bentley like this.
  6. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    Hmmm, I think I already tried that and it wasn't correct. I'll try again.

    Edit:

    Yea, that was it I must have had some other problem that caused me to think it wasn't working.
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,450
    This is what you should have done:
    Code:
    create_popup(room_width/2, room_height/2, "Are you sure?", 1, "Ok");
    
    About argument being an array --- this simply isn't true given my experience with it. In many ways it is a specially handled construct, not a true array. Though you can reference single entries from it like an array (e.g. argument[0]), there's nothing else you could do with it that you could do with a real array. Functions like array_1d_length() and array_copy() won't work on it, and you can't treat it as a standalone unit. By standalone unit, I mean situations like the following, all of which are currently illegal:
    Code:
    var args = argument;
    Code:
    ds_list_add(global.things, argument);
    Code:
    return argument;
     
    Last edited: Feb 15, 2019
    8BitWarrior, Pfap and Bentley like this.
  8. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551


    Thanks, for the informative answer :)
     
  9. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551

    Your use of the term "alias" is new to me I'll need to do more research. But, if you do that with a normal array what happens? Are gamemaker arrays reference types?
    Code:
    var my_array = [1,2,3,4];
    
    var other_array = my_array;
    
    other_array[0] = 5;
    
    Does the above make 2 separate arrays or does my_array[0] now equal 5?
     
  10. 8BitWarrior

    8BitWarrior Member

    Joined:
    Jun 20, 2016
    Posts:
    43
    Gamemaker arrays are indeed reference types. Your example would create 2 arrays. You could use '@' to prevent a copy and write to the original array.

    other_array[@ 0] = 5; // write to original array -- no copy made
     
    Pfap likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice