ARTICLE
Written by Chronic
Posted on 2/May/2007
Instance creation codePosted on 2/May/2007
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.
: (STEP)
{
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.
: (COLLISION WITH OBJ_PLAYER)
{
other.x = teleX;
other.y = teleY;
}
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.
: (CREATION CODE)
{
teleX = 100;
teleY = 200;
}
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.
: (CREATE)
{
if (!variable_local_exists("teleX")) { teleX = 16; }
if (!variable_local_exists("teleY")) { teleY = 16; }
}
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.
: (CREATE)
{
if (!variable_local_exists("teleX")) { teleX = 16; }
if (!variable_local_exists("teleY")) { teleY = 16; }
if (!variable_local_exists("newRoom")) { newRoom = -1; }
}
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.
: (COLLISION WITH OBJ_PLAYER)
{
other.x = teleX;
other.y = teleY;
if (newRoom > -1) { room_goto(newRoom); }
}
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
: (CREATION CODE)
{
teleX = 100;
teleY = 200;
newRoom = room1;
}
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.