What would a first episode be if not Hello World? In this episode we are going to create a simple Hello World application using GML only. For anybody who has used GML before this is going to be eyerollingling obvious but for those Drag & Drop developers this is for you.
So let's get started and create a new project helloWorld
and create a new object.
draw_set_color(c_red);
draw_text(x,y,"Hello World");
Drop obj_hello into a new room and run.
Really simple but impressive what can be done with 1 event and only 2 lines of GML.
In order to do anything fun in GM it is important to understand a few more events.
Since games involve user interaction we need to introduce another event that we will use on a regular basis.
Introducing the Step Event.
"The step event is actually comprised of three sub events but for most things the standard step event will be fine to use"
-GM docs
Even though it is broken down into 3 seperate event Begin Step, Step, and End Step we will only need to use the Step Event to accomplish the task at hand. We will save the explainatin of the differences between the 3 for a later date.
In essence the step event is that it will be called frequently while the object is in the room. Each room has a room_speed
which is the target steps per second of the current room. (NOTE: this is not going to be exeact for slower devices).
Since we have access to an event that is called every step of the game it is logical that we want to update any values of our object in this event.
What we are going to do with the step event this time is check for mouse input and keyboard input. We are looking to capture some change based on the input that the player puts in. To do this we will use a conditional and leverage the keyboard_check_pressed
and mouse_check_button_pressed
to accomplish the goal. (Bonus points available: Check out keyboard_check_direct
and look up Microsoft Virtual keys and try keyboard input when the window isn't in focus :D)
if(mouse_check_button_pressed(mb_left)) {
// CHANGE TEXT COLOR
}else if(keyboard_check_pressed(vk_up)) {
// INCREASE TEXT SIZE
}else if(keyboard_check_pressed(vk_down)) {
// DECREASE TEXT SIZE
}
Before we go ahead and add the actual code that will make things happen its good to look at the skeleton of the event to see what we are trying to do here. It looks like we are trying to catch a mouse event and 2 keyboard events all in the same if else block.
To create each of the effect we will need to setup some variables to use and also refactor our original draw event to reflect the changes that we made.
First let's setup our variables that we will use in the Step event.
CURRENT_COLOR = c_red;
COLORS[0] = c_aqua;
COLORS[1] = c_fuchsia;
COLORS[2] = c_red;
COLORS[3] = c_lime;
RATIO = 1;
So we went ahead and defined a variable to hold the current text color (CURRENT_COLOR
) and an array of COLORS
that we will be selecting from when we change colors. The last variable RATIO
we will be using in the Draw Event to handle the xscale
/ yscale
which will control the size of the text.
Now let's fill in the step event with the logic to make this happen. Everything here is nice and simple. I am using random_range
to return an integer that I will use to get a new color which will be set to the CURRENT COLOR
if(mouse_check_button_pressed(mb_left)) {
index = random_range(0,4);
CURRENT_COLOR = COLORS[index]
}else if(keyboard_check_pressed(vk_up)) {
RATIO *= 1.1;
}else if(keyboard_check_pressed(vk_down)) {
RATIO *= 0.9;
}
And finally we will update our Draw Event to use CURRENT_COLOR
and draw_text_transofrmed
to allow us to specify the desired scaling.
draw_set_color(CURRENT_COLOR);
draw_text_transformed(x,y,"Hello World", RATIO, RATIO, 0);
And now we can run that and should see the desired results.