GM:S 1.4 How to insert an n-number of arguments in script?

Discussion in 'Programming' started by Edwin, Dec 7, 2018.

  1. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    144
    Hey, pals. I started to learn scripting in GM:S very recently.

    I've created a script and want to insert in n-number of arguments there.
    Code:
    scr_instances_exist(obj1, obj2, obj3, ...);
    
    So when you write an objects in brackets it will be set as arguments, so how I can perform it? Should I create an array or something else?
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    5,651
    Instead of argument0,argument1,argument2, use argument[0], argument[1], argument[2].

    You can pass a whole array as an argument, but you'll need to fetch that array inside the script before you can actually use it in the script.
     
  3. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    667
    Yeah, for infinite arguments, you can create your own array of arguments (my_args = [true,false,10,"Hello"]) and pass that array as an argument. You'll have to pull the array out of the argument in the script:

    scr_something(my_args);
    Code:
    var temp_arr = argument0;
    for (var i=0;i<array_length_1d(temp_arr);i++) {
       my_stats[i] = temp_arr[i];
    }
    Or you can use the built-in arguments in array style:

    scr_something(my_hp,my_mp);
    Code:
    var hp = argument[0];
    var mp = argument[1];
    Or you can loop through the default arguments with a for loop:
    Code:
    for (var i=0;i<argument_count;i++) {
       my_stats[i] = argument[i];
    }
     
    Last edited: Dec 7, 2018
    ph101, Edwin and Amon like this.
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    5,651
    You don't need to put [ i] if you put it in code blocks the first time around. lol
     
    RefresherTowel likes this.
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    667
    Hahaha, trolled myself, I was wondering wtf was going on.
     

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