by Ben Tyers
effect_create_above(ef_cloud, 100, 150, 2, c_green);
This would create an effect above the current object. The effect will be a cloud at position x 100 and y 150 in the colour green. The 2 denotes the size of the effect, which is large. You can also create an effect below the current object using
effect_create_below(type, x, y, size, colour);
You can try out these effects. Create an object, obj_example, and put this in the Step Event:
if (mouse_check_button(mb_left))
{
effect_create_above(ef_cloud,mouse_x, mouse_y, 2, c_green);
}
Create a room and place one instance of this of object in it. You can test the effect using the left mouse button.
You can use the following types of effects:
ef_cloud,ef_ellipse, ef_explosion, ef_firework, ef_flare, ef_rain, ef_ring, ef_smoke, ef_smokeup, ef_snow, ef_spark,and ef_star.
You can introduce some randomness into it, for example, change the code to:
if (mouse_check_button_pressed(mb_left))
{
effect_create_above(ef_firework,mouse_x, mouse_y, 2, choose(c_green,c_red,c_white,c_yellow));
}
Now test again.
Try the following code:
if (mouse_check_button_pressed(mb_left))
{
effect_create_above(choose(ef_smoke,ef_ring, ef_explosion, ef_firework),mouse_x,
mouse_y, 2, choose(c_green,c_red,c_white,c_yellow));
}
You can also create a mouse trail using effects. Create a new object obj_trail, and put in the following code in the Step Event:
repeat 2
{
effect_create_above(ef_star ,mouse_x, mouse_y, 2, choose(c_green,c_red,c_white,c_
yellow));
}
Now test the game again. You will see an effect like that shown in Figure A_19_1:
Figure A_19_1: Showing effect trail
Combining various effects at the same time can create some awesome graphics.
Note: Effect code does not need to be in a draw event, it can be in a step event, key press event, etc.
Basic Projects
• A) Allow the user to change the weather by pressing W. Change between a snow and rain effect.
• B) Create a firework display each time the mouse left button is pressed. Have fireworks going off in more than one place, different sizes and colours.
Advanced Projects
• C) Create a line of effects, 20 pixels apart, which start at the top of the screen and fall down to the bottom, then start from the top again.
• D) Create an effect that spreads out from a location when the mouse is clicked. Make the effect move out every 10 degrees from the starting point. Destroy any effects after 3 seconds.
165
Appendix 20 Loops
A common need in programming is to repeat the same code multiple times. This functionality is available through using loops. There are four main loop functions in the GameMaker Language: do, while, for, and repeat. Each of these has its own strengths and weaknesses.
Loops are great for:
Process data in ds lists or arrays
Performing the same action multiple times
Performing a calculation until it is true
Drawing data
A loop can result in many similar actions being executed in a very short notice.
Note: It is important to make certain that loops run the correct number of times and then stop. It is possible through a coding mistake to create an infinite loop that runs forever. This will cause the game to stop working. Care needs to be taken to avoid this from happening.
repeat Loop:
repeat is a very simple control structure, which will repeat an assigned action for a specified number of
times. The following code creates five enemies in a single step, all at random positions in the room.
repeat(5) //Repeat the following code block 5 times
{
instance_create_layer(irandom(room_width), irandom(room_height) ,”Instances”, obj_enemy);
}
while Loop
The while loop will repeat an action as long as the expression assigned to it is true or until you call a break. For example the following checks for an empty space randomly, and when it finds a free space the loop stops.
while (!place_free(x, y))
{
x = random(room_width); y = random(room_height);
}
for Loop:
The for loop will execute different actions, and after each one it will check if its expression is true. If the expression is not true, the loop will end, and none of the following functions will be executed. Like before, we’ll use it to create a 50-point array in a matter of milliseconds. In almost all cases, you’d want to use var i here, as the loop counter generally has no need to be used outside of the block scope.
for(var i=0; i<50; i++)
{
array[i]=i;
}
The first statement (i=0) initializes the loop and sets a starting value. After initialization, the loop will check if i is smaller than 50, and if it is, it will increase the value of i using the i+=1; statement; this can also be written as i++;. After each step, the array’s length will increase by 1 and add i to that array’s position.
Note: Alternatively you can reduce a value by 1 using i--;.
To iterate n times with a for loop:
If you start with i=0, check i
If you start with i=1, check i<=n.
do Loop:
This will repeat until an expression is true. For example, the following will repeat until it finds an empty position:
do
{
x = random(room_width);
y = random(room_height);
}
until (place_free(x, y));
Not too hard to understand, right? It can be used in many ways and it’s one of the most practical functions in the GameMaker Language, so don’t forget about it.
Note: The do loop will always execute at least one time, but in for or while, it can be skipped.
Basic Projects
•A) Place an object in a room at a random position. Create another object that finds a random location within 100 pixels of the first object. Use a while loop for this.
•B) Add 100 random numbers between 1 and 100 to a ds_list, then sort them into order, highest value first. Display onscreen in 4 columns of 25. Use for loops for this.
Advanced Projects
•C) Create four random points in the room. Get an object to visit each point in order. Display a message when all the have been visited.
•D) Store the names of students in your class in an appropriate way. Display names in alphabetical in order, one at a time on the screen for 5 seconds, with a gap with no name for 2 seconds.
Appendix 21 Arrays
Arrays are a useful way of storing data in an organized format. They allow similar information to be stored together. This allows for easy access, manipulation, and displaying of data.
You can store real numbers, integers, strings; and the indexes of sounds, sprites, and objects in a single array.
Some use applications for arrays include the following:
Storing weapon information for multiweapon games
Keeping lists of data
Storing data in order created
Storing facts and information
There are both one-dimensional arrays and two-dimensional arrays in GameMaker. An example of setting a 1D array:
name[0]= "Bob";
name[1]= "Claire";
name[2]= "Steve";
name[3]= "Nigel";
name[4]= "Sue";
A visualization of this would look something like this:
LocationValue
0Bob
1Claire
2Steve
3Nigel
4Sue
The following would draw
the string stored in the array "name" at index 2, "Steve," at position 100, 100.
draw_text(100, 100, name[2]);
An example of a 2D array, storing name, age, and country of residence.//name
info[0, 0]= "Bob";
info[0, 1]= "Claire";
info[0, 2]= "Steve";
info[0, 3]= "Nigel";
info[0, 4]= "Sue";
//ages
info[1, 0]=27;
info[1, 1]=19;
info[1, 2]=52;
info[1, 3]=40;
info[1, 4]=102;
//country
info[2, 0]= "America";
info[2, 1]= "Spain";
info[2, 2]= "Brazil";
info[2, 3]= "Canada";
info[2, 4]= "France";
A visualization of this in table form would look like this:
0
1
2
0
Bob
27
America
1
Claire
19
Spain
2
Steve
52
Brazil
3
Nigel
40
Canada
4
Sue
102
France
So the value at cell [1, 3] would be 40.
You can use and change the values of an array as you would with any variable.
Arrays come into their own when processing data, especially with 2D arrays, as each array entry can be different lengths. Put the code shown previously into the Create Event of an object.
For loops are great ways to sequentially process data. The example below will repeat i three times and each time repeat j five times each time for i; this is known as a nested loop. It will then draw the value of the array cell at that position.
In a Draw Event put:
for (i=0; i < 3; i++)
{
for (j= 0; j < 5; j++)
{
draw_text(i*70, j*80, info[i, j]);
}
}
This code will draw the values of information onscreen in a table format, as shown in Figure A_21_1:
Figure A_21_1: Showing output of data
An example YYZ is available in the resources.
You can add to an array variable just like a regular variable. For example:
info[1, 3]+=1;
Which would increase Nigel's age by 1. You can compare array values:
if (info[1 ,0]>info[1, 1])
{
text_to_show=(info[0, 0]+ " is older than "+info[0, 1]);
}
Which sets text_to_show to Bob is older than Claire.
That is all for this example.
An application of this in GameMaker Studio 2 as an example; arrays are very useful for holding data for multiple weapons, as shown:
Weapon Name Strength
Cost
Current Ammo Sound Effect
Image
Bullet Object
Gun
1
1
200
snd_gun
spr_gun
obj_bullet_ gun
Machine Gun
5
10
400
snd_mach_ gun
spr_mach_ gun
obj_bullet_ mach_gun
Rocket Grenade
250
300
8
snd_rocket
spr_rocket
obj_bullet_ rocket
Nuke
1000
5000
2
snd_nuke
spr_nuke
obj_bullet_ nuke
You can create a data array for the previous code using:
/// @description Put data in array
global.cash=100000;
global.selected_weapon=0;
///declare array
//weapon name
weapon_info[0,0]="Gun";
weapon_info[0,1]="Machine Gun";
weapon_info[0,2]="Rocket Grenade";
weapon_info[0,3]="Nuke";
//weapon strength
weapon_info[1,0]=1;
weapon_info[1,1]=5;
weapon_info[1,2]=250;
weapon_info[1,3]=1000;
//weapon cost
weapon_info[2,0]=1;
weapon_info[2,1]=10;
weapon_info[2,2]=300;
weapon_info[2,3]=5000;
//weapon ammo
weapon_info[3,0]=200;
weapon_info[3,1]=400;
weapon_info[3,2]=8;
weapon_info[3,3]=2;
//weapon sound effect
weapon_info[4,0]=snd_gun;
weapon_info[4,1]=snd_mach_gun;
weapon_info[4,2]=snd_rocket;
weapon_info[4,3]=snd_nuke;
//weapon sprite
weapon_info[5,0]=spr_gun;
weapon_info[5,1]=spr_mach_gun;
weapon_info[5,2]=spr_rocket;
weapon_info[5,3]=spr_nuke;
//weapon bullet object
weapon_info[6,0]=obj_bullet_gun;
weapon_info[6,1]=obj_bullet_mach_gun;
weapon_info[6,2]=obj_bullet_rocket;
weapon_info[6,3]=obj_bullet_nuke;
Create an object, obj_example, and place the above code in a Create Event.
The resources needed for this example are in the downloadable resources, example_1.
You can set up an easy weapon select system using the following in a Step Event.
/// @description control
if (keyboard_check_pressed(ord("0")))
{
global.selected_weapon=0;
}
if (keyboard_check_pressed(ord("1")))
{
global.selected_weapon=1;
}
if (keyboard_check_pressed(ord("2")))
{
global.selected_weapon=2;
}
if (keyboard_check_pressed(ord("3")))
{
global.selected_weapon=3;
}
if (mouse_check_button_pressed(mb_left)) && weapon_info[3, global.selected_weapon]>=1
{
audio_play_sound(weapon_info[4, global.selected_weapon], 10, false);//play firing sound
weapon_info[3, global.selected_weapon]-=1;//reduce ammo
instance_create_layer(mouse_x, mouse_y,”Instances”, weapon_info[6, global.selected_weapon=0]);//create bullet
}
x=mouse_x;
y=mouse_y;
In a Draw Event put:
/// @description drawing
draw_sprite(weapon_info[5,global.selected_weapon],0,x,y);
//draw info
draw_text(10,20,"Strength: "+string(weapon_info[1,global.selected_weapon]));
draw_text(10,40,"Cost: "+string(weapon_info[2,global.selected_weapon]));
draw_text(10,60,"Current Ammo: "+string(weapon_info[3,global.selected_weapon]));
Which is easier to understand.
An example is available in the resources, example_2.
Basic Projects
• A) Create a 2D array with data relating to four students in your group. Include variables name, age, height, eye colour, and favourite food. Display this data onscreen.
• B) Make a 1D array with the values of 10 types of food. Display one at random each time the spacebar is pressed.
Advanced Projects
• C) Create an array to store the starting positions in a game of chess, use letters to represent each piece, that is, K for King, Q for Queen, N for knight, B for bishop, and C for castle; all other squares to have a value of 0. Use UPPER CASE for black and lowercase for white. Represent this board on the screen. Try to use one or more loops to populate the array. Draw a chessboard with the correct value in the middle of each square.
Appendix 22 DS Lists
ds Lists (Data Structure) are effectively one-dimensional arrays that allow you to add
data sequentially. However, they are much more flexible than a one-dimensional array, and you can perform lots of cool functions with them. They are great for the following:
Sorting data alphabetically
An inventory system
Shuffling items
Card games
Music management
Message management
Sort the contents
Delete contents, which will shift the remaining contents
Adding instance id’s and processing them (for example by distance or hp)
Queuing up variables, objects or sprites to process them in order
Add content at the start, anywhere in the middle, or at the end, which again can shift contents
Find where something occurs in the list
Note: ds_lsits start with an index of 0, so if you wanted to insert at position 5, the actual index would be 4.
ds_lists are awesome for making an inventory system. You can easily add items and remove them. So in an RPG type game you may pick up a key, then remove it when you collide with a door. To start you need to declare a ds_list, for example:
example_list = ds_list_create();
Then add something to the list:
ds_list_add(example_list, "cheese");
ds_list_add(example_list, "bacon");
ds_list_add(example_list, "mushroom");
ds_list_add(example_list, "ham");
ds_list_add(example_list, "tomato");
Note: it’s also possible to add many values within one line of code. For example: ds_list_add(example_list, "cheese", "bacon", "mushroom, "ham", "tomato");
This would give the following result:
Index
value
0
"cheese"
1
"bacon"
2
"mushroom"
3