Castlevania Dungeon Forums

Off Topic => Off Topic => Topic started by: TheouAegis on December 28, 2012, 10:58:23 AM

Title: Little useful tidbit for Game Maker users
Post by: TheouAegis on December 28, 2012, 10:58:23 AM
You know that annoying issue of trying to set a variable for an object that doesn't exist and getting slapped in the face with an 'Unknown Variable' error? The solution for the longest while has been to precede the variable with an object check like "if instance_exists(n) n.hp=0". There's actually a much faster solution.

with

I've tested this today and all the tests seem to conclude that with has a built-in break if the object doesn't exist. You don't have to think too deeply to realize this is much faster than a separate instance_exists() conditional.


Consider the following standard (for me at least) codes:

n = instance_nearest(x,y,obj_enemy)
if n
n.hp-=2

Now that becomes

with instance_nearest(x,y,obj_enemy)
hp-=2


n = collision_rectangle(bbox_left-16,bbox_top-16,bbox_right+16,bbox_bottom+16,all,0,1)
if n
n.hp = 0

Now that becomes

with collision_rectangle(bbox_left-16,bbox_top-16,bbox_right+16,bbox_bottom+16,all,0,1)
hp = 0


If instance_exists(obj_whip)
obj_whip.x=x
obj_whip.y=y

Now that becomes

with obj_whip
{
x=other.x;
y=other.y;
}