Jump to content


Photo

How do scripts work


  • Please log in to reply
2 replies to this topic

#1 Benxamix2

Benxamix2

    GMC Member

  • New Member
  • 278 posts
  • Version:GM8

Posted 24 February 2012 - 02:38 PM

Okay guys, I'm asking this here because I'm confused.

Let's suppose I create a script like this

//example_script( )

for ( i = 0; i < 150; i += 1 ) {
    global.players[i] = instance_find( player, i )
    (global.players[i]).identifier = i
}

It will obviously take good time to be completed.

So, my question is, if I run this code


example_script( )
h = (global.players[149]).identifier

Will it have any trouble?
I mean, how does script calling work? The called script must be finished before executing any line after that script?
  • 0

#2 creativebunch

creativebunch

    The Bunchiest

  • GMC Member
  • 886 posts
  • Version:GM:Studio

Posted 24 February 2012 - 03:02 PM

Scripts are like creating your own functions. Basically the script will work as if you had the code from the script in the place where you're calling it from. And yes just like every else the script has to finish executing before the rest of the code can run.

Scripts also have the ability to return values and have values given to them, I'm not sure if you want this much detail but you're gettin' it!

Returning a value works as such:

// adding_script()
return 2 + 2;
That script returns 2 + 2 (4) to the calling code which you can then use like this:

my_number = adding_script();
// my_number now equals 4
You can give values to a script using variables called 'arguments' and they work as such:

// adding_script2()
return argument0 + argument1;
To make this more clear I'll show you how you would call the script:

my_number = adding_script2(2, 2);
// my_number now equals 4
You put in the parentheses the amount of arguments the script needs, the first value is argument0, the second is argument1 and so on (you can have up to 9 arguments).

As another example I'll re-create the function motion_set
// motion_set(direction, speed)
direction = argument0;
speed = argument1;

Hope this helps.
  • 0

#3 Benxamix2

Benxamix2

    GMC Member

  • New Member
  • 278 posts
  • Version:GM8

Posted 24 February 2012 - 03:50 PM

Yeah, it's a good response, very clear (though too much complete because I already knew most of that, but it's fine :P). Thanks!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users