In the last level you practiced using methods/scripts that are already made for you. In this level you will learn how to write your own scripts. You will also start to understand why writing your own scripts can improve the readability, re-useability, and efficiency of your code.
Part A – Creating Simple Scripts Watch the video: 08-01-A
DoneFile: Lv08_01_A.gmk
Challenge 1 – Make Some Scripts
Use: 08-01-A.gmk to start
Lets add three scripts to the current game project. These scripts basically take existing code and place it in a script.
a) The ghost object currently has code to fire two different objects, a laser and a rock. Create two scripts called fireLaser and fireRock that can be used in the program to simplify the existing ghost firing code in the step event.
b) The player currently gets points when one of their arrows hits a ghost. Create a script called givePlayerPoints that will replace the current point code.
In this lesson you will build upon your basic script writing by creating scripts that require arguments (or sometimes called parameters). Arguments and parameters are pieces of information that you give a script in order for it to complete its task. Designing a script that uses arguments usually gives the script more flexibility in the way that it can perform it’s task. Adding arguments to your scripts can greatly reduce the amount of code you have to type with larger projects. Check out the videos to see how.
Part A – Scripts and Arguments Watch the video: 08-02-A
DoneFile: Lv08_02_A.gmk
Challenge 1 – Make Some Scripts
Use: 08-012-A.gmk to start
Take the existing project and modify it to include these three scripts.
a) There are several objects that can give the player hp; apples, hamburger, and potions. Take a look at the existing code when a player collides with these three objects. Replace the increasing hp code with a script called givePlayerHealth that requires one argument: the amount of hp to give the player. The script should give the player the specified amount of hp and also check to make sure that the player doesn’t go over 100 hp. Then replace the code in the apple, hamburger, and potion with your script.
b) The player currently gets 1 point for hitting a ghost with an arrow. Write a script that allows you to give the player any amount of points. The script will require one parameter which represents the amount of points to give the player. The script will check to see if the player has reached 10 points, and if they have go to the game over room. Once you have this script written, use it to give the player 1 point when they hit a ghost and 5 points when they destroy a ghost. Test it out!
c) Write a script called dropRandomItem that requires no parameters, and creates either an apple, burger, or potion when a ghost is destroyed (after being hit by a player arrow). Add this script in the appropriate location in the program.
In Level 07 we took a quick look at some GameMaker methods/scripts that return values back to you after you use them. In this lesson you will learn how to make your methods return values back to you. It will be an important concept to understand because in future lessons you will write scripts that do this when you are challenged with more complicated tasks in your games.
Part A – Returning Values Watch the video: 08-03-A
DoneFile: Lv08_03_A.gmk
Challenge 1 – hpStatus and getBarColor Watch the video: 08-03-X1Preview to preview the challenge. Use: 08-03-A.gmk to start
Here are two tasks to practice a bit of value returning.
a) Give the player a new variable called healthStatus. healthStatus is either “healthy” (50-100 hp), “warning” (25-49 hp), or “danger!” (below 25 hp). Use the draw object to draw this variable above the health bar.
Now write a script called hpStatus that returns the appropriate word based on the player’s hp. This script requires no arguments.
Each time you set, give, or take away hp from the player (in the givePlayerPoints and damagePlayer scripts) you should call the script so that it determines the appropriate value for hpStatus.
Hint: you’ll use a line like healthStatus=hpStatus() somewhere in there!
b) The health bar is currently always drawn in white. Write a script called getBarColor that requires one argument; the player’s hp. It will use the argument and return a color so you can draw the health bar in an appropriate color (green if 50 or above, orange if 25-49, and red if below 25).
To return a color in your script you can use a line like this: return(c_red)
Once the script is made, can you figure out how to use it in the draw object to easily set the color of the health bar rectangle when it is drawn? If not just peek at the solution video. This is a nice example of how scripts can be used.
This level is all about a variable that every game object gets when it is placed into the room: an instance id.
Every object in the game room has a unique instance id assigned to it. This makes it possible for you to ‘find’ or ‘remember’ certain game objects (instances) so that you can perform special tasks on them later. The videos will show you what this means.
Using the id variable of instances can let you do a very wide range of programming tasks that just aren’t possible with what you know to this point. Give a good effort to learn how to use instance id’s to take your games to the next level!
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.
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.
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.
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.
In this lesson you won’t write any code. You are going to read it and try to predict the game behaviour. Code reading and learning from reading code is a valuable skill to practice. You will watch a short refresher video and then you will be given three tiny programs to analyze. Hopefully you will be able to describe the behaviour of each program without every running the code.
Part A – With, Other, and Local Variables In this video we will refresh some past knowledge about using the with statement, the keyword ‘other’, and creating local variables when using with statements.
Watch the video: 09-03-A
Matching Project: 09-03-MarchOn.gmk (just for reference)
Challenge 1, 2, 3 – Read and Learn
Open up the document Lv09_ReadTheCode_Ids.doc from the Level 09 Resource folder. On here you will find FOUR sample programs. The first program is ‘March On’, which you saw as an example in the video 09-03-A. The remaining three programs are Detonator, Mother Coin, and Chain Reaction. For each program, go through the code slowly and try to determine what the program does without running the projects or watching the solution video. It could take you 2 minutes or it could take you 20 minutes to figure each one out. It is important that you try to get through it on your own to really make your brain work at understanding how the instance id variables are being used in these programs.
Detonator (Easy/Medium)
something about land mines that blow up.
Mother Coin (Easy/Medium)
a mother coin that makes baby coins?!?
Chain Reaction (Hard)
the name says it all, tricky one!
Having a piece of paper to sketch on or write ideas out on is always useful. Good luck!
Many games make use of the mouse. Drag and drop, player selection, menu use all use the mouse. There are many other game tasks that you can do with the mouse. In this level we will start to explore some basic player selection and drag and drop behavior to give you an idea of how you can start using the mouse in your games.
Introduction
In this lesson you will learn how you can combine knowledge of the mouse coordinates and states to add some great game features.
Part A – Mouse X and Y The mouse position is always available to you by using the mouse_x and mouse_y game variables. You can use mouse_x and mouse_y to accomplish some simple drag and drop behaviour in your game.
Watch the video: 10-01-A
Matching Project: L10-01-A.gmk
Challenge 1 – Drag and Drop Watch the Preview Video: L10-01-X1-Preview
Preview File: L10-01-X1Preview.exe
Start with the file: L10-01-X1Start.gmk.
There are three parts to the this challenge.
a) Give rock objects a state variable that is 0 when then rock is not being dragged and 1 when the rock is being dragged. When the user left clicks on a rock, the rock should change its state to 1 and the rock should now change its x and y position constantly to match the position of the mouse. Of course you also have to add that another click on a rock that is being dragged should let go of the rock by changing its state back to 0.
b) To avoid the problem of being able to grab more than one rock at a time, we will create a global variable called global.dragState=0. When no objects are being dragged, the variable will equal 0. When you start to drag a rock, change this global variable to 1 so you can remember that an object is currently being dragged. Go back to your code from part a and add use this global variable in your code so that you don’t pick up rocks unless this global variable is 0, you change the variable to 1 when you start dragging a rock, and you change this variable back to 0 when you release a rock.
c) We don’t want to be able to drop rocks on top of other rocks. When the user clicks to let go of a rock, you can check if the current location would be free of a collision with another rock by using the place_meeting script. Try to use place_meeting to make sure that the current position would be a rock free collision location. If so, drop the rock. If not, don’t drop the rock (maybe play a bad sound)!
Challenge 1 Solution
Solution Video: L10-01-X1-Solution
Solution File: 10-01-X1-Done.gmk (has all three parts solved)
Challenge 2 – Select Units and Way Points
Here’s a great little game task. Select a game unit and tell it where to walk with a way point.
Watch the Preview Video: L10-01-X2Preview
Preview File: L10-01-X2Preview.exe
Start with the file: L10-01-X2Start.gmk.
Watch the preview video and run the preview file. Try to imitate the program without any hints! If you need hints, here are some – and if they don’t help, give the solution video a watch.
a) give each bones a state variable that determines whether or not they are selected. Selecting and deselecting is done with the left click. Also add some code in the draw event of the bones so that when a bones is selected you draw the bones sprite and also a circle around the bones (remember to change draw color so you’re not drawing black on black).
b) just before you change the state variable of a bones to 1, use a with statement to tell all the bones objects to change their state to 0. This way selecting a unit deselects all other units.
c) give each bones a variable that determines whether or not they are moving and also give them variables to keep track of an x and y destination coordinate.
d) when a bones detects a global right click, and if they are selected, they will change their moving variable to 1. The step method should then be coded to check whether or not they are moving. If they are, find out the direction toward their x and y destination coordinates and move that way.
e) if a bones is moving and the distance between it’s destination is less than it’s speed value, it should stop moving.
Hopefully these hints help you out a bit. They try to bring together a few of the concepts from earlier in the course – especially the idea of states.
Challenge 2 Solution
Solution Video: 10-01-X2-Solution (has all parts solved)
Solution File: 10-01-X2Done.gmk
Creating variables to remember values is great. But what happens when you want to store lots of data and don’t want to have to create variables for each individual value? This is where lists and arrays can help you out. This level will introduce you to the former; lists. Lists will store numerical and text values under one id and also have a set of pre-made scripts that allow you to perform simple tasks on the values inside.