GameMaker [Solved] With variable access vs Direct variable access

Azenris

Member
Which of the following do you use?
Does it have a performance impact ( over the course of an entire project )?
Does this answer change between VM/YYC?
Does the answer change if there are more of less variables?
Do you find one more readable than the other?

note: there is only ever 1 obj_scene, gms2

Code:
   with ( obj_scene )
   {
       var _flooring_tilemap = tiles_flooring_tilemap;
       var _collision_tilemap = collision_tilemap;
       var _watered_tilemap = tiles_watered_tilemap;
       var _lyr = depth_sorted_layer;
   }
or
Code:
   var _flooring_tilemap = obj_scene.tiles_flooring_tilemap;
   var _collision_tilemap = obj_scene.collision_tilemap;
   var _watered_tilemap = obj_scene.tiles_watered_tilemap;
   var _lyr = obj_scene.depth_sorted_layer;
I was just curious. I have been doing the first one out of habbit,
but never wondered if changing the context using with would affect readability / performance.

I know not to be craycray over early performance, I just do it everywhere, and if it does play a part
in things, I could switch my habbit earlier.
 

Yal

šŸ§ *penguin noises*
GMC Elder
AFAIK var definitions get moved to the top of the script as part of the processing, so space for all temporary variables you use is allocated first. So it doesn't matter where you place them. (Also, var variables are global, so there's no additional overhead because of scope changes if they're used inside a with loop)
 

TheouAegis

Member
HOWEVER, all the "obj_scene" references DO impact performance. Your second code is slower as a result, but only because you have so many variables being read. If you were only reading 2 or 3 variables, then with(obj_scene) would be slower. And in either case, the difference is slight enough in most situations that you could safely neglect it.
 
R

Rukiri

Guest
This is how your object should be structures.

Create: This is before any function is called, if using Unity this would be at the top of the script.
Step: This is your update method, begin step is before update and end step is after end step
Draw: Self explanitory

So in create you're creating variables for that object, GMS2 added unity like object variables which is what I'd use otherwise you just use something like
Code:
move_speed = 2;
etc.
In step you can call var variable; this will only be accessible in the update method.

globalvar can be used everywhere but honestly not using the var command makes them accessable everywhere really...

You can call object variables like so, o_player.spd and can set them as so o_player.spd = 5;

This is how I handle it, there's many ways to do this tho.
 
Top