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.

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:

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.

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.

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.

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

Kengine Gaming "Kengine Gaming is the best GM site I've seen" - flubmasterQ
"Good games never die" - Elmernite

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.

First, in the current room (if any) all instances get a room-end event. Next the non-persistent instances are removed (no destroy event is generated!). Next, for the new room the persistent instances from the previous room are added. All new instances are created and their creation events are executed (if the room is not persistent or has not been visited before). When this is the first room, for all instances the game-start event is generated. Now the room creation code is executed. Finally, all instances get a room-start event.

I knew this function existed 'cause I did read the whole manual... But I don't know why, I kinda forgot to use it because of everything I was learning at the same time. You tut is well done and did remind me this existed and I'll definetly use it in the futur, if needed.

Great how many people don't know about this,its a key part of lowering the number of objects I have.I'd like it if it were made easier to access and stuff.CTRL+Rclicking and copy pasting gets hard when almost every object needs a creation code with minor tweaks and stuff
I found it reading the panel in the room creator

Impressive... I have been in the dark to this function for so long. I never knew it existed. This will be useful in such things as RPG computers, or in room to room transfer.

0

We are all sinners, everyone has sinned (Romans 3:23 and 3:10). Christ the Lord suffered, and died in our place so we wouldn't have to, because He loves us (Romans 5:8, John 3:16). He rose again, defeating Satan, crushing the head of the serpent. We all can live, all we have to do is admit that we are sinners, believe that Jesus Christ died for us, and confess to God that we need Him in our lives and ask him into our hearts and lives (Romans 6:23, John 1:12, 1 Corinthians 15:3-4). Check out Romans 10:13. If you have any questions, contact me. -CoRoSiLeAnd on a lighter tone:"Eagles may soar, but weasels don't get sucked into jet engines."; "For Sale: Parachute, only used once, small stain, never opened."current production: There, Once...expected production time: 3-4 years