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

GML Are Global variables worth it?

CloseRange

Member
First off I'm sorry if this is the wrong place to post this it just seemed like the most logical place.

Lately I have been thinking about global variables and I have been wondering, what is the point of them? I for one never use them in my programs and haven't for many many years so why do people use them? You could easily get the job done by using pointers to reference variables from main objects. It just seems like a waste to use.

So why do you use global variables? What benefits do you see with them?
 

Simon Gust

Member
global variables are never lost throughout the game. They are faster to access than variables in other instances. You don't need to remember which instance the variable belongs to.
 
  • Like
Reactions: jms

Tornado

Member
just quick thougths:

1. access to them is quicker than access to instance variables

2. it is shorter to write global.defaultMonsterSpeed than obj_mainController.defaultMonsterSpeed

3. they are always there. I don't have to take care of that if the object containing variables is already created or not.
 

rwkay

GameMaker Staff
GameMaker Dev.
Some thoughts on your thoughts.

1. They should be the same speed as there is effectively a hidden object called 'global' that the global variables are inside - though the code paths in the runner will be slightly different, I doubt they would be significantly different in speed.

2. you can use #macro to shorten either

3. no argument there.

Russell
 

rwkay

GameMaker Staff
GameMaker Dev.
I suspect that your measurements in 1) were not strictly comparing apples to apples and something else was getting in the way... I may look later tonight.

Russell
 
D

dannyjenn

Guest
As already mentioned, global variables are good for when you want the variable to be there for the entire game. You could do the same thing with a persistent controller object, but why bother? The only downside I see to global variables is that you cannot deallocate them. However, this generally isn't a problem, since non-string variables are only 4 bytes a piece. You'd have to literally have millions of global variables before RAM becomes an issue.

ad 2)
I hate macros. i dont like seeing stuff in the code which are not part of the programming language.
But macros are part of the language. Regardless, you could just give your main controller object a shorter name. Either way, your third point stands.
 

Tornado

Member
I suspect that your measurements in 1) were not strictly comparing apples to apples and something else was getting in the way... I may look later tonight.

Russell
wow, cool I'm really looking forward to that! thx.
 
Any thread where we get a deeper look at what's under the hood is of particular interest to me, especially when yoyo staff help set the record straight.

My tests always showed globals being about equal to temporary variables, faster than instance variables at any rate. So I'm looking forward to finding out if that is actually true.
 

GMWolf

aka fel666
Faster, slower, whatever the case may be, it doesn't matter. You are getting caught up in small efficiencies.

Use the tool that is right for the job. Does the variable belong to a system? use instances.
Does the variable truely reflect a property of the game in general? use globals. (Hint, almost nothing does not belong to some kind of system.).

Examples:
A score: probably part of some score/objective system.
A countdown timer: Probably part of some timer system.
Time at which the game was launched: Global variable. (Or some sort of meta-info object, but thats what global already is...)
 

Tornado

Member
@Fel666 You are absolutely right! No doubt about it! Thanks for giving us a wake up slap in the face!
The small gain one gets through this at even thousand places is easily eaten up by just one mistake in the "graphics pipeline".
 
R

RetroKingofHarts

Guest
I like to use global variables for tables, sets of data that multiple objects across various rooms need to access. For this purpose, as noted above, it's FAR faster than referencing a controller object EVERY time you want to access such a piece of information. As an aside, if you don't want to type 'global.' in front of a var EVERY time you use it, declare it with 'globalvar' the first time.

Also, I usually init and declare most of my globalvars in an init room's creation code. Up to you whether you want to do that, or leave that to an object. If the object is persistant, and serves other purposes, then sure thing! Otherwise, it's not worth it to create an object JUST for that purpose.
 
B

bobula13

Guest
I always tend to use global variables, for example, when I make an RPG of any kind. Whether it's turn based or action. It makes it a lot easier when trying to access player attributes across a dozen scripts so you don't have to continuously write out obj_player.hp or whatever.
 
R

RetroKingofHarts

Guest
@Tornado fair enough. That is simply down to preference. What works for some may not suit others. For me, I make all Macro/Constants ALL CAPS, but some might not do that. IIRC there is no performance difference in global. and globalvar... or if there is, it's negligible.
 

CloseRange

Member
For me persanolly the way I've always done it is create an object called either Main or Game that's persistent and runs a unique script for each of my systesm such as:
setup_player();
setup_sprites();
setup_particles();
and these would just be scripts that contain all of varables for those systems. It creates a better level of organization when finding variables and means I don't always need to create a new object for each one.

also if you wanna talk about shortness typing out Main. is faster than typing out Global. ;)
 

Tornado

Member
"also if you wanna talk about shortness typing out Main. is faster than typing out Global."

haha, true :)
 
R

RetroKingofHarts

Guest
Are those scripts run only once at the start of your game? and do multiple objects call on the data generated by those scripts? If so... I'd run those in an init room's creation event. The only exception to WHY you could do this is if your controller object itself is persistent through the game and needs to use those as well...?
 

GMWolf

aka fel666
If you are going to use these 'Main' object, I suggest you place them in an initialization room, and rename them with an 'i' prefix or something.
For instance, if you have one of these objects to do with the scree, you could have object oScreen, place it in a room and name the instance 'iScreen'.
This means that throughout your code you can refer to the instance using "iScreen" rather than the object, which is far better form.

Did you guys test performance using instances rather than object references? It should be quite a bit faster given that it would not have to find the first instance. (I suspect as fast as global, as global uses an object internally, much like global pragmas, etc)
 

CloseRange

Member
For me, yes, the controller object is indeed created at the start of the game. Yes it is persistent and yes many objects call on it. the variables within are never global so using it in the room's creation event would be pointless as the variables would be lost and unable to be used.
 

GMWolf

aka fel666
For me, yes, the controller object is indeed created at the start of the game. Yes it is persistent and yes many objects call on it. the variables within are never global so using it in the room's creation event would be pointless as the variables would be lost and unable to be used.
But do you reference the instance rather than the object?
referencing the object is considered bad form.
Not only that, but there is significant overhead too, as GM must first find the first instance of that object (something that as far as i can tell, requires a linear search of all instances.)
 

CloseRange

Member
I'm going to be honest with you Fel... I never knew there was an actual differance between the instance and the object. I know every object has a unique id and i just thought that id was the instance.

The way I do it is i have a Main object and if it has a variable called particle i would call it like this:
Code:
Main.particle
 

GMWolf

aka fel666
I'm going to be honest with you Fel... I never knew there was an actual differance between the instance and the object.
Oh no....

Object define behavior: They have a set of events and respective actions to carry out.

Instances carry out those instructions.

You can think of objects as a script, and instances as actors on the stage, reading that script.

You can have multiple actors (instances) read off the same script (object). They each have a different position, etc, but they will follow the same logic.

A more accurate description would be:

Objects are a mapping of events to actions. (excluding the sprite information, etc).

Instances are Bags of Variables. (They can hold an arbitrary number of variables and their values.)
Instances also hold a refrence to what object they are (their object_index)

Every time a event triggers globally (step event, click event...), the engine will loop through all of the instances, Looks at their object_index, and finds the appropriate action to execute.

So remember: Object define behavior, Instances carry out this behavior.
Objects exist in code. Instances exist at runtime (Its more complicated than that, but whatever)
Objects do not 'run' and are not 'created'. Instances do.

taken from the manual:
In GameMaker Studio 2 you have objects and you have instances. Objects are essentially the base template for an instance, and as such are never present in a room directly... only instances of the object are placed in the room.
 
Last edited:
You know what I really wish we had? Object variables. Not pertaining to a specific instance, but to all instances of that object. Not coppied to every single instance, but existing in one place. You can do this with arrays, but it is kind of tedious.
 
R

RetroKingofHarts

Guest
I know what you mean, and for that, I think you are just best using a global variable, and having it in the object's code for all its instances to read from that variable.

I use this for synchronized animations and movements... especially if I want globally oscillating movement for objects like platforms or other things.

As an example, this is how rings in the classic Sonic games are ALL on the same exact frame, in the level layout.
 

GMWolf

aka fel666
You know what I really wish we had? Object variables. Not pertaining to a specific instance, but to all instances of that object. Not coppied to every single instance, but existing in one place. You can do this with arrays, but it is kind of tedious.
yeah, just use global vars.
Or, build system and use the technique i outlined above.

So if you wanted to synchronize animations, you could create an object named oAnimationSystem, instantiate is as iAnimationSystem, and then in all your synchronized object, do image_index=iAnimationSystem.image_index.

The advantage of this is that you are no longer bound by objects. You could have it per object, or per feature. far more flexible.
Also far mor descriptive. Its clear that that object handles animations.
 
well, I've just been using globally declared arrays for that. Which makes me wonder, what is the difference in speed between accessing an array index vs. an instance variable. Yeah, you'll say it doesn't matter, but suppose you've got a really logic heavy loop, in which case maybe you'll want to squeeze out the best performance that you can.
 

GMWolf

aka fel666
well, I've just been using globally declared arrays for that. Which makes me wonder, what is the difference in speed between accessing an array index vs. an instance variable. Yeah, you'll say it doesn't matter, but suppose you've got a really logic heavy loop, in which case maybe you'll want to squeeze out the best performance that you can.
event then it wont matter. just access those variables less.

Remember that before you can access an array index, you must first access that array, like any other variable.
So the cost is that of accessing a varialbe + that of accessing an array index.
whilst for variables, its just accessing that variable.

Though, if accessing an instance variable, you first have to access that actual instance, then the variable in the instance.
Same for an array (global or otherwise) first access the instance (could be the global instance), then the array, then the index.

Now i dont actually know how GM is written internally, but these are all reasonable assumptions based on logic and my own experience.
 

bml

Member
While I don't really like macros I've been doing this:

#macro g global
#macro player global.__player
#macro metrics global.__metrics


So I can write:

g.dungeon_level = 1;
player = Player();
metrics = Metrics();


I'm still learning my way around GMS.
 

rwkay

GameMaker Staff
GameMaker Dev.
OK I have done some timings and here are my findings

on VM

Instance Write - elapsed time=0.11us
Other Instance Write - elapsed time=0.15us
Object Write - elapsed time=0.12us
Global Write - elapsed time=0.13us
Local Write - elapsed time=0.09us
Instance Read - elapsed time=0.10us
Other Instance Read - elapsed time=0.14us
Object Read - elapsed time=0.12us
Global Read - elapsed time=0.09us
Local Read - elapsed time=0.08us

on YYC

Instance Write - elapsed time=0.01us
Other Instance Write - elapsed time=0.02us
Object Write - elapsed time=0.03us
Global Write - elapsed time=0.00us
Local Write - elapsed time=0.00us
Instance Read - elapsed time=0.01us
Other Instance Read - elapsed time=0.03us
Object Read - elapsed time=0.03us
Global Read - elapsed time=0.01us
Local Read - elapsed time=0.01us

Which is more like what I would expect - where in order of fast to slowest

Locals variable < Global and Instance variable < Other Instances < Object

The project I used to test this is https://www.dropbox.com/s/5gmewcwmdjhyer8/TestVarTiming.yyz?dl=0

if you want to take a look

Russell
 

GMWolf

aka fel666
OK I have done some timings and here are my findings

on VM

Instance Write - elapsed time=0.11us
Other Instance Write - elapsed time=0.15us
Object Write - elapsed time=0.12us
Global Write - elapsed time=0.13us
Local Write - elapsed time=0.09us
Instance Read - elapsed time=0.10us
Other Instance Read - elapsed time=0.14us
Object Read - elapsed time=0.12us
Global Read - elapsed time=0.09us
Local Read - elapsed time=0.08us

on YYC

Instance Write - elapsed time=0.01us
Other Instance Write - elapsed time=0.02us
Object Write - elapsed time=0.03us
Global Write - elapsed time=0.00us
Local Write - elapsed time=0.00us
Instance Read - elapsed time=0.01us
Other Instance Read - elapsed time=0.03us
Object Read - elapsed time=0.03us
Global Read - elapsed time=0.01us
Local Read - elapsed time=0.01us

Which is more like what I would expect - where in order of fast to slowest

Locals variable < Global and Instance variable < Other Instances < Object

The project I used to test this is https://www.dropbox.com/s/5gmewcwmdjhyer8/TestVarTiming.yyz?dl=0

if you want to take a look

Russell
When checking other instance, what kind of code did you used?
If it's something like this:
Code:
var inst = instance_create(...);

Repeat(10000) {
  var n = inst.foo;
}
It is normal we see ~2x time over instance, as we first need to access the local var 'inst'.

I wonder if you cannot reclaim the performance if you refer to the instance by it's room editor name.
 
D

dannyjenn

Guest
Faster, slower, whatever the case may be, it doesn't matter. You are getting caught up in small efficiencies.

Use the tool that is right for the job. Does the variable belong to a system? use instances.
Does the variable truely reflect a property of the game in general? use globals. (Hint, almost nothing does not belong to some kind of system.).

Examples:
A score: probably part of some score/objective system.
A countdown timer: Probably part of some timer system.
Time at which the game was launched: Global variable. (Or some sort of meta-info object, but thats what global already is...)
This really depends on your coding style. Strictly speaking, GML is not an object-oriented language, and you really don't need to break it up into "systems" and keep everything localized. You could always go for a more procedural approach, in which many of your variables are global (or at least in one big controller object).
 

GMWolf

aka fel666
This really depends on your coding style. Strictly speaking, GML is not an object-oriented language, and you really don't need to break it up into "systems" and keep everything localized. You could always go for a more procedural approach, in which many of your variables are global (or at least in one big controller object).
But i would not recommend it.
You end up clogging the namespace, and it gets pretty messy.
Systems keep thing nice and neat.

The main andvantge of the system aproach is that you can turn them on or off. very useful if you dont want to end up with lots of random flags everywhere.
An example would be game modes. One with a time limit, the other one with a score limit.
Would you rather have checks everywhere to check the game mode? Or just create the timer / score limit system as needed?
What if now you want to add a mode with both limits? Do you want to go in all your code, adding extra flags and checks? Or just create both systems at game start?

Systemic aproaches in GM are very powerful, and can be very naturally expressed due to the way objects are kept in memory. There isnt a complex heirarchical structure to GM, its all very flat.
This is reminicent of ECSes, whereeverything is very systemic too.
 
W

Wraithious

Guest
These are the results I got when using an old tool called Benchmarkvars, not sure who made it but it was someone on the old gmc forum. I tested this in 2 os environments, using gamemaker early access 1.99.551 on windows 10 latest update in a dell quad core laptop, no hardware virtualization, and also tested on android version 7.1 on a galaxy s6 active phone. first the windows test:
gm var test.png

Next the Android test:
gm var test android.png

Talking micro seconds there's some big differences depending how you code, and also notice some differences in these 2 os's results!! even the operating system itself matters when you are deciding how to most efficiently code your game.

txts = ds_list_create();
vals = ds_list_create();
show_message ("Starting tests!");
var numiterations = 1000000;
var t;
t = get_timer();
repeat(numiterations) x = 1;
t=get_timer()-t;
show_debug_message("Setting existing GM var: "+string(t)+" us");
ds_list_add(txts,"Setting existing GM var: ");
ds_list_add(vals,t);

var v1;
t = get_timer();
repeat(numiterations) v1 = 1;
t=get_timer()-t;
show_debug_message("Setting var defined var: "+string(t)+" us");
ds_list_add(txts,"Setting var defined var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) v2 = 1;
t=get_timer()-t;
show_debug_message("Setting instance var: "+string(t)+" us");
ds_list_add(txts,"Setting instance var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) global.v3 = 1;
t=get_timer()-t;
show_debug_message("Setting global.var: "+string(t)+" us");
ds_list_add(txts,"Setting global.var: ");
ds_list_add(vals,t);

globalvar v4;
t = get_timer();
repeat(numiterations) v4 = 1;
t=get_timer()-t;
show_debug_message("Setting globalvar var: "+string(t)+" us");
ds_list_add(txts,"Setting globalvar var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) id.x = 1;
t=get_timer()-t;
show_debug_message("Setting id.existingvar: "+string(t)+" us");
ds_list_add(txts,"Setting id.existingvar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) id.v2 = 1;
t=get_timer()-t;
show_debug_message("Setting id.instancevar: "+string(t)+" us");
ds_list_add(txts,"Setting id.instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) object0.x = 1;
t=get_timer()-t;
show_debug_message("Setting object.existingvar: "+string(t)+" us");
ds_list_add(txts,"Setting object.existingvar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) object0.v2 = 1;
t=get_timer()-t;
show_debug_message("Setting object.instancevar: "+string(t)+" us");
ds_list_add(txts,"Setting object.instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) with(id) v2 = 1;
t=get_timer()-t;
show_debug_message("Setting with(id) instancevar: "+string(t)+" us");
ds_list_add(txts,"Setting with(id) instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) with(object0) v2 = 1;
t=get_timer()-t;
show_debug_message("Setting with(object) instancevar: "+string(t)+" us");
ds_list_add(txts,"Setting with(object) instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) (instance_find(object0,0)).v2 = 1;
t=get_timer()-t;
show_debug_message("Setting (instance_find(object,0)).instancevar: "+string(t)+" us");
ds_list_add(txts,"Setting (instance_find(object,0)).instancevar: ");
ds_list_add(vals,t);

var read;

t = get_timer();
repeat(numiterations) read=x;
t=get_timer()-t;
show_debug_message("Reading existing GM var: "+string(t)+" us");
ds_list_add(txts,"Reading existing GM var: ");
ds_list_add(vals,t);

var v1;
t = get_timer();
repeat(numiterations) read=v1;
t=get_timer()-t;
show_debug_message("Reading var defined var: "+string(t)+" us");
ds_list_add(txts,"Reading var defined var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = v2;
t=get_timer()-t;
show_debug_message("Reading instance var: "+string(t)+" us");
ds_list_add(txts,"Reading instance var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = global.v3;
t=get_timer()-t;
show_debug_message("Reading global.var: "+string(t)+" us");
ds_list_add(txts,"Reading global.var: ");
ds_list_add(vals,t);

globalvar v4;
t = get_timer();
repeat(numiterations) read = v4;
t=get_timer()-t;
show_debug_message("reading globalvar var: "+string(t)+" us");
ds_list_add(txts,"reading globalvar var: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = id.x;
t=get_timer()-t;
show_debug_message("reading id.existingvar: "+string(t)+" us");
ds_list_add(txts,"reading id.existingvar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = id.v2;
t=get_timer()-t;
show_debug_message("Reading id.instancevar: "+string(t)+" us");
ds_list_add(txts,"Reading id.instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = object0.x;
t=get_timer()-t;
show_debug_message("Reading object.existingvar: "+string(t)+" us");
ds_list_add(txts,"Reading object.existingvar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = object0.v2;
t=get_timer()-t;
show_debug_message("Reading object.instancevar: "+string(t)+" us");
ds_list_add(txts,"Reading object.instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) with(id) read = v2;
t=get_timer()-t;
show_debug_message("Reading with(id) instancevar: "+string(t)+" us");
ds_list_add(txts,"Reading with(id) instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) with(object0) read = v2;
t=get_timer()-t;
show_debug_message("Reading with(object) instancevar: "+string(t)+" us");
ds_list_add(txts,"Reading with(object) instancevar: ");
ds_list_add(vals,t);

t = get_timer();
repeat(numiterations) read = (instance_find(object0,0)).v2;
t=get_timer()-t;
show_debug_message("Reading (instance_find(object,0)).instancevar: "+string(t)+" us");
ds_list_add(txts,"Reading (instance_find(object,0)).instancevar: ");
ds_list_add(vals,t);
show_message ("All Tests Done! See debug output");
 
Last edited by a moderator:

rwkay

GameMaker Staff
GameMaker Dev.
I gave the project to you so you could take a look I tried all the variations of referencing variables for both Read and Write

var a = 10;
a = 10;
objRef.a = 10;
ints.a = 10;
global.a = 10;

and for var foofoo

foofoo = a; // a is a var
foofoo = a;
foofoo = objRef.a;
foofoo = inst.a;
foofoo = global.a;

Take a look at the code - my timings are for each iteration (rather than the overall time)

Russell
 

Pfap

Member
But do you reference the instance rather than the object?
referencing the object is considered bad form.
Not only that, but there is significant overhead too, as GM must first find the first instance of that object (something that as far as i can tell, requires a linear search of all instances.)
Just saw this recently and was wondering if this still stands if you are only ever going to use one instance of an object?


Code:
// i'm only going to create one instance of this object

my_id = id;
I get that this may be a pointless question and something not worth thinking about, but....


Code:
// other object i'm using to change the above object

with(obj.my_id){   do stuff       like fire an       event_user(0);  }

// or is the below the same??

with(obj){ do stuff }

So yea, pretty much just curious I try to break my objects up and generally just organize as best as I can to keep things easier to follow.

I'm currently using the below block in a project:


Code:
if turn_control.swap = 1{
 //trigger the event_user in the turn_control to initiate 2nd swap
 with(turn_control){ event_user(1); }
 exit;
}


EDIT:
I just read the chart from Wraithious and kind've answered my own question.
 
Last edited:

Joe Ellis

Member
So the results show that global variables are the 3rd fastest, local variables being the 1st, 2nd being built in instance variables like x, y image_speed
And if a really heavy script using one of the slower techniques like instance.variable, causes a lag (over 16666 microseconds) you could possibly halve this time by using one of the faster techniques, so this is big difference really, lag is the main importance

But after comparing the fastest ones, if a process took a whole 5 seconds to complete, the difference between the very fastest (local vars) and global vars would only be an extra half a second

Also from rwkay's results you can do 208325 local var reads per frame and only 185177 global var reads per frame - with no lag, (I think he's got a really fast computer) but even still, the difference in a normal game situation is nothing, (you've still got thousands of microseconds left before lag starts)
I think most lag people have is from graphics\draw related things, or some of the pathfinding\mp grid functions, or having wayy too many instances, which in fairness does make alot of variable reads\writes like we're talking about, but the problem is really from having too many instances, not variables- or maybe ifs, or just badly coded\repetitive\unnecessary repetitions of the same code-- my point is, changing which way your dealing with variables wont save this!
 
Last edited:

Joe Ellis

Member
I gave the project to you so you could take a look I tried all the variations of referencing variables for both Read and Write

var a = 10;
a = 10;
objRef.a = 10;
ints.a = 10;
global.a = 10;

and for var foofoo

foofoo = a; // a is a var
foofoo = a;
foofoo = objRef.a;
foofoo = inst.a;
foofoo = global.a;

Take a look at the code - my timings are for each iteration (rather than the overall time)

Russell
I always test things like this by performing each operation in solo continuously until theres no frame time left and see how many reps each one can perform in one frame, do you think this is more accurate, less, or would it not make any difference?

Only just realized how old this thread is!
 
Last edited:

Evanski

Raccoon Lord
Forum Staff
Moderator
I use globals to call,change and store information about what the player does/doing what the player has picked up, ect.. and I can edit that information from anywhere, I dont use them unless I need to use that same variable somewhere else that inst related, like saving a status effect the player has
 

Simon Gust

Member
Here's another trick.
If you have data structures that are loop-heavy like a large inventory, terrain data, an endless list or similar and the data structure is stored in an instance variable, in another object or global you can shortcut it and copy it's refrence to a local variable first before entering a loop.
Example. I have a 3000x3000 array that is global. I want to fill this array with random numbers
Code:
// first, shortcut array refrence
var array = global.my_array;

// now loop
for (var i = 0; i < 3000; ++i) {
for (var j = 0; j < 3000; ++j) {
    array[@ i, j] = irandom(10);
}}
VERY IMPORTANT:
with array, it's required to add an @ inside as otherwise it would copy the array and not edit the real one. Inside a script you would return "array" back to global.my_array but
this is outside a script where return has no function.

It's not that noticable with the original being global, but if you have instance variable in other objects it will be faster by a lot than to use a dot-operator.

Another example I want to show you is how I'd approach putting a lot of instances into a list.
I used to be not very smart about it
Code:
with (obj_enemy)
{
    ds_list_add(other.list, id);
}
Each iteration of reading other.list takes a lot of time.
So to make this faster I copy the refrence into a local variable first
Code:
var list_ = list;
with (obj_enemy)
{
    ds_list_add(list_, id);
}
note that this example has little functional value and is just for show.
 
Top