Jump to content


Photo
- - - - -

Instance Creation Code


  • Please log in to reply
100 replies to this topic

#1 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 27 March 2007 - 10:38 AM

The instance creation code is probably a hugely overlooked part of Game Maker. In this tutorial, I will show you how to access the instance creation code and also an example of a use.

For this tutorial I will be using Game Maker 6.1, however Game Maker 5.3a should also work fine.

Accessing

Gaining access to this part of Game Maker is really simple. First for all, ensure you have Game Maker in Advanced Mode by looking in the File menu. Next to Advanced Mode you should see a check mark, if this check mark is not present select the option.

Next add an object and a room, then add an instance of your object to this room. In the room editor, hold down your control (Ctrl) key and right click the instance. If you have done this correct you will see a context menu with several options in it.


Posted Image
Instance context menu


Clicking Creation Code will bring up an 'Execute a piece of code' type dialog for you to enter in your GML script. Any script that you place here will be specific to that instance.

Once code as been entered here, each time you hover your mouse over this instance in the room editor. The status bar will now say "has code" next to where its instance id is shown.

Example

To demonstrate one use of this, I will be making a generic teleport object and then use the creation code to set its destination point.

In a new project I added the following:
  • 2 sprites - spr_player, spr_telepad.
  • 2 objects - obj_player, obj_telepad.
  • 1 room - room0.
For the sprites I used:


Posted ImagePosted Image
Player and Telepad sprites


Add a step event to the obj_player and add in the following script to give it basic movement.

{
  move_towards_point(mouse_x,mouse_y,5);
}

Add an instance of obj_player in to room0 and then test the game to make sure things are working fine at this point. If the object moves towards the mouse cursor, then you are ready to continue, otherwise follow the steps above again.

Next we need to program obj_telepad. Add a collision with obj_player event and add the following script.

{
  other.x = teleX;
  other.y = teleY;
}

Then add an obj_telepad object in to room0, running the game at this point will fail as teleX and teleY have not yet been defined.

To do this we will use the creation code. Like shown at the start of the tutorial, open the obj_telepad's creation code and add the following script in to it.

{
  teleX = 100;
  teleY = 200;
}

You can now run the game to test the telepad object. Just run over it and the player object should be sent to the coordinates set in your creation code.

To prove that the creation code is instance specific, add another obj_telepad to the room and the creation code script, but this time give it a different desination coordinates.

One thing you should note, is that if you do not set the teleX and teleY variables, when you collide with that instance you will get an error. To prevent this error, you should add the following script in to the obj_telepad's create event. Adding it to the create event is an ideal place as the instance creation code is executed before the create event.

{
  if (!variable_local_exists("teleX")) { teleX = 16; }
  if (!variable_local_exists("teleY")) { teleY = 16; }
}

Taking it an extra step

Its also possible to make your object move to specified location in a totally different room. To do this we need to expand on the scripts a little, in the create event of obj_telepad change the script to the following.

{
  if (!variable_local_exists("teleX")) { teleX = 16; }
  if (!variable_local_exists("teleY")) { teleY = 16; }
  if (!variable_local_exists("newRoom")) { newRoom = -1; }
}

And the collision event to.

{
  other.x = teleX;
  other.y = teleY;
  if (newRoom > -1) { room_goto(newRoom); }
}

In the creation code for obj_telepad, you can now switch to a new room by setting a newRoom varible like in the example below

{
  teleX = 100;
  teleY = 200;
  newRoom = room1;
}

The last change needed is to change obj_player in to a persistant object.

Closing

As you can see, this part of Game Maker can be very useful. Another application for this could be a treasure chest. You could normaly give the player a random item, but by setting the creation code for an instance could give a certain item each time.

You can see the completed result of this example below.Thanks for reading.
  • 1

#2 yellowsix

yellowsix

    GMC Member

  • New Member
  • 315 posts

Posted 27 March 2007 - 10:54 AM

WOW!
Didn't know this function existed at all...
This is VERY VERY useful.
Thanks!

Yellowsix
  • 0

#3 Verion

Verion

    After the Storm

  • New Member
  • 926 posts

Posted 27 March 2007 - 10:56 AM

That actually opens up alot of possibilites.

The short response for your tutorial:

That was extremely good, 9.99/10
(i don't ever give out 10/10 cause nothing can be perfect)

the_Firestorm

Edited by Chronic, 27 March 2007 - 10:57 AM.

  • 0

#4 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 27 March 2007 - 10:59 AM

Thanks for the comments, i actually wrote this along time ago and posted it at Eo along with some others i've not posted here.

I'll be adding a simple expansion to this tutorial sometime today, so check back then.
  • 0

#5 purple_pixie

purple_pixie

    GMC Member

  • New Member
  • 2647 posts

Posted 27 March 2007 - 11:23 AM

Very nice - I have heard this mentioned on the forums before, but I don't think I've used it.

I always got around it by have a "room maker" object, that makes each instance, and assigns it's variables.

Which is good if you want to set them *after* create events, but not half as easy to do as this.
  • 0

#6 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 27 March 2007 - 11:30 AM

I've just added the expansion i mentioned earlier, its a section at the end called "Taking it an extra step".
  • 0

#7 Elmernite

Elmernite

    Kengine

  • GMC Member
  • 421 posts

Posted 27 March 2007 - 01:08 PM

Very nice! I already new about this, but you explained it nicely. I'd read this on Eo when you first posted it there.
I use this method for the AI in Cell Burst. If I every write a tutorial on how to do the AI. You won't care if I link here to explain the Creation Code part of it?
-Elmernite
  • 0

#8 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 27 March 2007 - 09:35 PM

Linking to other tutorials isn't really recommended, as there is no guarantee that what you link to will remain at that location.
  • 0

#9 georg

georg

    GMC Member

  • New Member
  • 319 posts

Posted 27 March 2007 - 11:50 PM

excellent stuff. Thanks chronic, you've just wounded two or three problems of mine. I think i can finish 'em off now... :)
  • 0

#10 the_muffin_man

the_muffin_man

    GMC Member

  • New Member
  • 100 posts

Posted 27 March 2007 - 11:57 PM

Great tutorial, I had no idea about that. Good explanation and very useful information.
  • 0

#11 Vizierde

Vizierde

    GMC Member

  • New Member
  • 96 posts

Posted 28 March 2007 - 02:18 AM


Edited by Vizierde, 14 January 2009 - 08:30 PM.

  • 0

#12 Verion

Verion

    After the Storm

  • New Member
  • 926 posts

Posted 28 March 2007 - 03:38 AM

Woah!

I looked in to those tutorials on EO, the command line one was really helpful!
  • 0

#13 SquareWheel

SquareWheel

    Square Wheel Games

  • New Member
  • 851 posts

Posted 28 March 2007 - 03:43 AM

I didn't learn about this function until about 1 and a half years into GM. It is great for keeping down on objects. Anyways, it is a nice tutorial. Great job!
  • 0

#14 Joerdgs

Joerdgs

    Clavus Studios

  • New Member
  • 393 posts

Posted 29 March 2007 - 10:41 AM

*still laughing about the amount of users that did not know this function*

:D Man, I've been using this function almost as long as I use GM. Found it when looking around the help file.
  • 0

#15 Spartan117John

Spartan117John

    GMC Member

  • GMC Member
  • 579 posts

Posted 29 March 2007 - 10:49 PM

I know you have heard this alot, but "I had no idea that function existed!"
Thanks alot for this excellent tutroial! :D
  • 0

#16 Bawk Bawk Boom

Bawk Bawk Boom

    GMC Member

  • New Member
  • 23 posts

Posted 29 March 2007 - 11:12 PM

Heh. I actually found this out a few weeks ago, but yes, it is very useful. I forget what I used it for, I think for some sort of door system.

Nice tutorial, Chronic. The examples were very good.
  • 0

#17 Big J

Big J

    GMC Member

  • GMC Member
  • 2848 posts
  • Version:GM8.1

Posted 30 March 2007 - 06:44 AM

I've known about this feature for a long time. It's uber-powerful.
  • 0

#18 HaRRiKiRi

HaRRiKiRi

    GMC Member

  • GMC Member
  • 1364 posts

Posted 30 March 2007 - 07:57 AM

Good tut. I am too wondering how people could not know this function. I have used it in ALOT of places. This can be extremely useful for map editors and external maps. I put the map loading in this event, so I don't have any unexpected errors, like can't find instance because its not created yet.
  • 0

#19 georg

georg

    GMC Member

  • New Member
  • 319 posts

Posted 30 March 2007 - 10:02 PM

The amount of new stuff you have to learn and pay attention to is overwhelming to new users. That's why i ignored/forgot this one. Hmm... maybe i should re-read tfm from time to time...
  • 0

#20 zephiel87

zephiel87

    GMC Member

  • New Member
  • 1649 posts

Posted 31 March 2007 - 02:30 AM

wow... ive been using gm for 3 years and i didnt know this... but its really helpful. i will definately be playing around with it in future games.
  • 0

#21 Blazed Pyro

Blazed Pyro

    Pyro Tech

  • New Member
  • 50 posts

Posted 31 March 2007 - 03:18 AM

Wow thanks Chronic, ive been banging my head against my desk wondering how to do this, and wondering why game maker didn't have it...

- Pyro
  • 0

#22 Kapser

Kapser

    GMC Member

  • GMC Member
  • 1636 posts

Posted 31 March 2007 - 03:28 PM

I just learned this function from another tutorial 2 minutes ago. I can't believe I did'nt know that. :D
  • 0

#23 Mattz

Mattz

    GMC Member

  • New Member
  • 57 posts

Posted 31 March 2007 - 11:01 PM

Wow, I never new about this, now I can have 1 object and rotate it within the room... Thankyou so much for this!

Mattz
  • 0

#24 cronus616

cronus616

    Cronus Games, co.

  • New Member
  • 394 posts
  • Version:Unknown

Posted 01 April 2007 - 09:00 PM

wow.
i didnt know that
  • 0

#25 Excalibur333

Excalibur333

    GMC Member

  • GMC Member
  • 444 posts
  • Version:Unknown

Posted 03 April 2007 - 07:02 PM

Cool I just found this a few days ago it is pretty nice.
Excalibur
  • 0

#26 Twisted-Liquid

Twisted-Liquid

    GMC Member

  • GMC Member
  • 499 posts

Posted 03 April 2007 - 08:18 PM

Love it! wish i knew sooner. I already made a billion objects that one single object could have done for my current project.
  • 0

#27 GloryAndBen

GloryAndBen

    Game Programmer

  • GMC Member
  • 944 posts

Posted 04 April 2007 - 10:27 PM

Now I don't need to make so many goddam objects over minor differences...
Thanks for this!
  • 0

#28 Alex

Alex

    3lite Member

  • New Member
  • 3098 posts

Posted 06 April 2007 - 06:00 AM

Wow. I can't believe all this time it was written there in plain English and I never tried it. Thanks for this. This will definitely solve a lot of problems.
  • 0

#29 The_Cookie_Monster

The_Cookie_Monster

    GMC Member

  • New Member
  • 217 posts

Posted 06 April 2007 - 08:50 PM

Wow, nice, cool, this will help alot!

You deserve a cookie now!
  • 0

#30 Daniel-Dane

Daniel-Dane

    GMC Member

  • New Member
  • 3581 posts

Posted 06 April 2007 - 10:17 PM

I can't believe that so many didn't know of this. There are both the manual and the explanation in the room editor. Good someone wrote this :blink:.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users