GMS 2 [Solved] ways to change variable inside an object


Hello everybody !

I have a question about good practices in programing : when you want to change an object's variable, do you use "with" or "my_object.my_variable" ?

the two ways seem to work :

my_variable_health = 10 ;
} ;

Obj_Player.my_variable_health = 10;
Which one is the less costly, processing wise? Are there different effects? I prefer the second one, but it seems to produce unexpected results in long coding sheets, but it could be a bug from me. Any thoughts of advices would be very welcome !


produce unexpected results in long coding sheets
This would be a problem on your side.

calling obj_player.variable = n; is the proper way to go about it and what most people will generally do.

Some rules that would be causing your problems to keep in mind.

If you are setting a variable:
obj_player.variable = 10;
if you have more than 1 obj_player then this will change ALL of their variables, so if you want it to change for only one instance, you have to reference that one instance.

If you are getting a variabe:
var temp = obj_player.variable;
if you have more than 1 obj_player it is not guaranteed who's variable you are going to get it from. It could come from any of them, so you'd want to reference one specific instance if this is your problem.

Which one is the less costly, processing wise?
I'd assume they are the exact same. If not then the second method would be more efficient. (only reason i'm not 100% sure is because I don't know how 'with' works behind the scenes I can only make an educated guess)

how with works is it jumps into the object (or objects) you are refrencing and calls the code as if it was inside that object so you can do things like this:
with(obj_player) {
     scr_custom_script(); // only way to call a script on a seperate object is with "with"
it also makes it easier when you have to do a lot of variable manipulation in those objects and don't want to type out obj_player. for every single one.

Other than that do as you please nobody will think more or less of you if you choose one way over the other.


Would refer to the first instance of obj_player that was placed in the room, not all, and not a random one.

If that havent been changed.


unless you are setting an variable.
obj_player.variable = 10;
will still set ALL it for all objects:
With the above code you are setting the speed of an instance of "obj_ball". However if you have more than one instance of the given object in the room, then it will apply to ALL of them equally - unless you are using one of the JS targets or HTML5, in which case it will affect only one, but you have no way of knowing which one it will affect
and the example above it was:
obj_ball.speed = 0;
so if you are using js or html targets then yes use with if you need to access every single obj_player
see the docs

as for getting a value then yes it should be the first instance that was created that is reference but seeing as how most games involve deleting objects creating new ones its generally better practice to think of it as you can't predict it.
It makes life easier in the long run and because this was a question about best programming practices I'm still gonna stick with it because there is no reason to try and take advantage of that small detail.

I also found this thread about with vs the dot operator that I found interesting. I guess the dot operator is a bit faster until you add a bunch of variables that need to be manipulated at once.