• 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!

Accessing a "var" from another script

K

Kululu17

Guest
Hi - quick question about what is considered "local". If you declare a local variable in a script, and then that script calls a second script, can you access and change the variable from the first script within the second script? I know var is local, but I thought that when you use a script it is considered local to the object calling it as well... but it doesn't explain in depth in the manual.

So to make the question more general:

Start with Object A, which calls Script B, which then calls Script C.

What is local to what in this example? Can you use "var", or do you have to make it an instance variable? If you make an instance variable within a script, is it "deleted" afterwards like a "var", or does it clutter up your memory?

Thanks!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
var declared variables are ONLY relevant in the script or event calling them. If you need to use a var in another script, you need to pass it in as an argument for that script. In your example, you could have:

ObjectA:
var aa = 1;

and in ScriptB:
var aa = 100;

and in ScriptC:
var aa = "Hello";

In each case var will be declared used and thrown away before returning to the previous scope, and they shouldn't interfere with each other. Instance variables are the only way to use a consistent variable throughout all the events and scripts, but note they should always be declared in the create event of the object to use them. Instance vars exist as long as the instance that created it exists, so even if you create an instance var in a script, it will belong to the instance that called the script and not be removed until after the instance is destroyed.

Note that instance vars take up a minute amount of memory so don't worry about using them. You can have hundreds in an object with no problems
 

FrostyCat

Redemption Seeker
You cannot directly access a local variable set with var from another script. You need to make an instance variable (which will persist with the instance calling it) or global variable (which will persist until the game ends), or set it as an array and then pass it into the other script as an argument.

The memory savings of using var is usually negligible by today's standards, but the logical consequences aren't. Consider this:

Create event:
Code:
{
  num = 0;
  var i;
  for (i = 0; i < 5; i++) {
    num += dice(2);
  }
}
dice(n):
Code:
///dice(n)
{
  var i;
  var total = 0;
  for (i = 0; i < argument0; i++) {
    total += irandom_range(1, 6);
  }
  return total;
}
This works as expected only because of the var keeping the two i's in separate scopes. Removing the var i; lines from both pieces of code and thereby making i an instance variable would result in an infinite loop.

I've seen rookies make similar mistakes time and time again, simply because proper scoping isn't part of the defective video-centric GML education fad that's all the rage right now for the wrong reasons.
 

TheouAegis

Member
You can make the second script take the variable as an argument, augment the argument, and then return the result.

///script0()
var i = 0;
i = script1(i);

///script1(arg)
var i = argument0 & 1;
return argument0 >> 1 | i;
 
Top