Author [EN] [PL] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [ID] Topic: Little useful tidbit for Game Maker users  (Read 1579 times)

0 Members and 1 Guest are viewing this topic.

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Little useful tidbit for Game Maker users
« on: December 28, 2012, 10:58:23 AM »
+2
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;
}
« Last Edit: December 28, 2012, 11:06:22 AM by TheouAegis »
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Tags:
 

anything