• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Variable scoping with scripts

Z

ZombieTom

Guest
Hi guys,

This is not a problem, just something I found confusing that cleverer peeps may know more about.

In an object, I create an array - just a local array, not a global one. I then call a script within that same object, not passing any parameters to it. I have found that within the script, I can still access and modify the array as normal. Is this right? I thought scripts were like functions an therefore any variables created outside the script would be 'out of scope' unless passed to the script. Are scripts like php include files where the code they contain is just copied into the main program at run time? If this is the case, what is the need for the parameters; argument0, argument1, argument2 and so on?

I hope that makes sense.
 
You are right. Scripts basically function like the code is copied and pasted into the line they are called. The purpose of arguments is to make them more flexible and reusable. You don't want a function that specifically modifies the x variable for your object only, right? What if you want to adjust the y value? What if you want them to change by different amounts? There are a million reasons to have arguments in scripts even if the code works like you guessed.
 

FrostyCat

Redemption Seeker
No, scripts are not automatically inlined the way include or require does in PHP. The argument variables are for temporarily holding values passed to a script, and in the case of numbered argument variables, also used to mark a script as accepting some fixed number of arguments.

In short, the following are in-scope within a script:
  • Built-in functions
  • Global variables
  • Instance variables belonging to the instance calling the script
  • Local variables declared using var within the same script
This is a very different scoping situation compared to PHP.
 
C

CptChuckles

Guest
So if I understand FrostyCat, when a script is called by an instance, this code is at the same scope as any of that instance's event code, which is why it is able to access instance variables? Just like I can define blah in Create and then read blah in Step, this is the same reason I can also read blah from a script i call in Step? Therefor if I call the same script from an instance of a different object which never defines blah, I get a runtime error since blah is not defined within that instance?
 

TheouAegis

Member
Object A:
blah = 0;


Object B:
//nothing about blah here


====RESULT ====

Object A:
script_with_blah();
//no error


Object B:
script_with_blah();
//unknown variable blah error



Also keep in mind the following situation will cause an error:

Object A:
var blah = 0;
script_with_blah();
//unknown variable blah error


Since blah was declared locally, the script cannot read it even though both are in the same object.
 
R

RapTi

Guest
You are right. Scripts basically function like the code is copied and pasted into the line they are called. The purpose of arguments is to make them more flexible and reusable. You don't want a function that specifically modifies the x variable for your object only, right? What if you want to adjust the y value? What if you want them to change by different amounts? There are a million reasons to have arguments in scripts even if the code works like you guessed.
Hey guys. I actually still have some questions about this topic. I was following Pixelated_Pope's post on FSMs and there I found a script that initializes certain variables. Does that mean that when I run the script inside an object these variables become just like any other variables declared in the object itself?
Thank you!
 

samspade

Member
Hey guys. I actually still have some questions about this topic. I was following Pixelated_Pope's post on FSMs and there I found a script that initializes certain variables. Does that mean that when I run the script inside an object these variables become just like any other variables declared in the object itself?
Thank you!
Yes. You can check this pretty easily in the debugger.

This is the most basic way of understanding it: "Scripts basically function like the code is copied and pasted into the line they are called."

It is a little more complicated than that though as FrostyCat explains:

In short, the following are in-scope within a script:
  • Built-in functions
  • Global variables
  • Instance variables belonging to the instance calling the script
  • Local variables declared using var within the same script
In general, I would say that if you think of it as just copied and pasted you'll be fine with two exceptions - local variables created outside a script (for example in the event that calls the script or in a script which calls a script) do not carry over into the script and script arguments are also specific to the script (e.g. if a script_a calls script_b argument0 in script_a is not the same as argument0 in script_b).
 
Top