This lesson will be a short introduction on how you can ‘remember’ the id of an instance in your game and then perform some task on it later on. For our example we will pick a monster with the mouse and when we press the space bar we will fire at the monster that was picked.
Part A – Remembering Instances
Watch the video: 09-02-A
DoneFile: Lv09_02_A.gmk
Challenge 1 – Select and Remember
Use: 09-02-A.gmk to start
a) When the player presses the “F” key, randomly select a monster on the screen and fire an arrow toward it. Remember the exit command if you need it!
b) We are going to make the player pick an enemy.
In the playeràcreate event, give the player a variable called target. Set it to -4.
When the player hits the “T” key, set target equal to the id of a randomly selected monster. If there are no monsters in the room, exit the code.
c) When the player hits the “M” key (for move), check to make sure that the target monster still exists in the room. If it does, set the players direction so the player will move toward the target monster. If it doesn’t, exit the code.
When you test your code press “T” then press “M”. Test again without pressing “T” first.
Challenge 1 Solution
Solution Video: 09-02-X1
Solution File: 09-02-X1-Done.gmk
Challenge 2 – Homing Missile
Watch the Preview: 09-02-X2-Preview
Use: 09-02-X2-Start to start
This challenge is slightly longer than most. In this challenge you will create a homing missile that the player fires at the enemies that are floating around the screen. When the player presses “T”, the player will randomly select and remember the id of an enemy. When the player presses “SpaceBar” the player will fire a missile and that missile will remember which enemy that it is supposed to go towards. If you think you can do it without any guidance, go for it! If not, here are the steps you can follow:
a) Give the player a variable called global variable called target. Set it to -4.
b) When the player presses “T”, randomly select a monster and set the target variable to this monster’s id.
c) Give the missile object a variable called target. Set it equal to -4.
d) When the player presses “SpaceBar” you will check to see if the global.target variable represents an existing monster or not. If it doesn’t represent a monster that exists, exit the code. If it does represent a monster that exists, set the missile’s target variable equal to the player’s target variable. This way the missile will be able to remember which monster it is trying to follow. Also make the missile move upwards at a speed of 6.
e) In the step event of the missile, check to see if the missile’s target still exists in the room. If it does, find the direction toward the target; make the missile go this direction. If it doesn’t, and there are other enemies in the room, set the missile’s target variable to the nearest enemy’s id. If no enemies remain in the room, destroy the missile.
f) In the step event of the missile add code to check if the missile is within 30 pixels of it’s target. If it is, destroy the missile and the enemy and create an explosion.
Challenge 2 Solution
Solution Video: 09-02-X2
Solution File: 09-02-X2-Done.gmk
Challenge 3 – Cycle Selection
Watch the Preview: 09-02-X3-Preview
Use: 09-02-X3-Start to start
a) Add some code so that the currently targeted enemy (found with the “T” key) has a circle drawn around it so that we know it is selected.
b) Modify the player’s “T” key selection so that instead of randomly selecting a monster to target it will cycle through the monsters by their number order. When this is working properly each press of the T key should select the next monster (according to monster number assigned by GameMaker). For example, if the currently selected monster is monster 0, then the next monster to be targeted should be monster 1. Then monster 2, and so on. If the next monster number doesn’t exist, then go back to monster 0. This is a tricky one. Hint: create a variable called targetNum that remembers the which monster number you are currently tracking. Increase targetNum by one every time the T key is pressed. If targetNum gets too large, put it back to zero.
Challenge 3 Solution
Solution Video: 09-02-X3
Solution File: 09-02-X3-Done.gmk