Level 09 Lesson 01 [Using Instance Ids]

Each object in your game room is given a variable called id. Each instance of an object in the room will have a unique id value. These id values allow you to remember or find specific instances in the room so that you can perform special tasks with them later. Gamemaker will automatically assign the value of the id variable to each instance as it is added to the room. In this lesson we will take a look at some of the uses for instance id’s and some of the scripts that are commonly used when working with them.

Part A – Instance Id’s

Watch the video: 09-01-A
DoneFile: L09-01-A.gmk

 

Watch the video: 09-01-B
DoneFile: L09-01-B.gmk


Challenge 1 – Instance Id Practice

Use: 09-01-X1-StartHere.gmk to start this challenge
Use: Instance Cheat Sheet in the Level 09 folder for reference

Try adding the following to the project to practice using instance id’s.

1) In the drawObject draw event, find out the number of monsters in the room and display this value in the room. As the monsters leave the room, you should see this value drop to 0.

2) Use the gameControl object Step Event to constantly check if there are 0 monsters left in the room. If there are no more monsters, then go to the game over room.

3) Did you get number 2 working? If so, remove the gameControl object from the room so it doesn’t interfere with the next couple of tasks.

4) When the player presses the “S” key, do the following:
Find out how many monsters are currently in the room.
Leave the script if there are no monsters present.
Find a random number in the proper range so that you can grab one of these monsters (ex. for 10 monsters you will find a number from 0-9).
Use
instance_find to get the id of the random monster number.
Use the id of this monster and destroy it.

Challenge 1 Solution
Solution Video: 09-01-X1
Solution File: 09-01-X1-Done.gmk