Legacy GM Question about the with() statment

RyanC

Member
Hi Guys,

I often use the with(obj_something) statement and was wondering if anyone knows what kind of performance hit there is if the obj_something does not exists in the game.

for example lets say obj_enemy_1 does not exist in the game yet.

would these two functions have the same performance hit.

with(obj_enemy_1)
{
x +=1
}

with(obj_enemy_1)
{
if place_meeting(x,y,obj_floor) etc etc and lots of heavy stuff...
}
 

zbox

Member
GMC Elder
I'd say almost definately yes but you can find out for yourself - put them in two different scripts, call them X thousand times, and use the profiler to see if there is a differennce :)
 
M

Mr Tim the Wizard

Guest
You could say

if instance_exists(obj_enemy_1) {
with(obj_enemy_1)
{
x +=1
}
}

if instance_exists(obj_enemy_1) {
with(obj_enemy_1)
{
if place_meeting(x,y,obj_floor) etc etc and lots of heavy stuff...
}
}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
There is virtually no overhead from calling "with" with "noone" as an argument. So calling "with (object)" on an object that has no instances in the room is the same as saying "with (noone)"... I use this type of with construction all the time and have noticed no performance issues because of it (nor will it error if no instance of the object exists... only if you are checking for specific instances will it error).
 
M

Mr Tim the Wizard

Guest
Well, if you are using if instance_exists then it will only have to check that code, making it slightly faster, not really at all though. But yeah, I didn't know it wouldn't error if they didn't exist, that's interesting. But I suppose it makes sense as it is only referencing it's code, so the instance_exists probably would be a bad idea
 

TheouAegis

Member
Well, if you are using if instance_exists then it will only have to check that code, making it slightly faster, not really at all though. But yeah, I didn't know it wouldn't error if they didn't exist, that's interesting. But I suppose it makes sense as it is only referencing it's code, so the instance_exists probably would be a bad idea
Sorry, but you're wrong there. Using if instance_exists(object) prior to a with call is the worst thing you can do. The with call already does exactly that, so by preceding with with an instance_exists() check, you're basically saying

if instance_exists(obj)
if instance_exists(obj)
{ //with obj

do stuff
}

This is actually a very common rookie mistake I see a lot around these forums. Not sure if some tutorial on YouTube is propagating it, but it needs to stop.

The fact that with already performs an instance_exists() check itself is the reason why the first code here is slower than the second code.

Code 1:
with obj image_speed=0;

Code 2:
obj.image_speed=0;

However obviously (2) would fail if no instance of obj existed when that code was run. When it is guaranteed that an instance of obj will exist at the time that code is run, then (2) is better. When there is no guarantee that an instance will exist, then (1) is better, since (2) would have to be augmented with an instance_exists() check beforehand, which negates the speed difference.
 
Last edited:

RyanC

Member
This answers my question. So the code between the curly brackets after using with(obj) {code} is not processed if an instance does not exist.
Thanks to everyone, much appreciated!
 
Top