This video is part of my Udemy courses. See the description for more info. Hey there! In this lecture we're going to program an
object using Events. Let's go into our project. We have a few instances in the room here,
and we wanna make them respond to the player's input. So we can do that with Events. Every object can have a set of events,
and each event can have its own piece of code. So this code belongs to this event, and when
the event runs, this code runs. That is basically what events do. They run the code inside them
and when the event runs depends on what kind of event it is. To understand this relationship between Event
and Code, you can imagine this event to be a Space Key
event, so it runs whenever the player presses the
Space key, and the code in that event makes the player
jump. Now this jumping code doesn't run all the
time, but when you press the Space key,
this event is triggered and its code runs, making the player jump. So that's an example of Input Events, but
there are other kinds of events as well, and we are gonna use them in this lecture. --
Going back into our project, I'm gonna open the object here,
and go into the workspace so we can edit it. Here we have a window for Events, and this
is where we manage all the events for this object. To add a new event we can click here, and
you can see a lot of events in this list. Let's add the Create event,
then I'm gonna add the Step event, this one, and then I'll add the Clean Up event. The code window for each event is gonna up
here. Now let's see what these events mean. These events have to do with the lifetime
of an instance. The Create event runs when the instance is
created, so it runs only once. After that it doesn't run again for the same
instance. The Clean Up event runs when the instance
is destroyed or the room ends. And the Step event runs every frame, so it
repeatedly keeps running as long as the instance is in the room. So you can see that this is the Creation of
an instance, this is the End of the instance,
and this is the Update Loop that runs every frame. --
Now let me demonstrate the difference between Create and Step with a simple example. In the Create event, I'm going to enter this
code: x += 50; Now you don't need to understand what this
is or how it works, because we are gonna go through all the basics
from the next lecture. For now just understand that this increases
the x position of the instance by 50 pixels. So for example if you go in the room, and
open the properties of an instance, then increase its x position by 50, you'll
see that it moves slightly to the right. So that is going to happen in the Create event,
and as I mentioned earlier, this event runs only once for each instance. I'm gonna place the instances near the left
border, so we can easily spot any differences,
and now I'll run the game. If you compare the positions of these instances
to the positions in the room, you'll see that they've moved slightly to
the right. So they've moved 50 pixels on the x axis in
the Create event. It happened only once because of the event. --
Now let's see what happens if we run the same code but in the Step event. So I'll select the code, and cut it, then
go into the Step event and paste it here. This event runs every frame. So the instance moving 50 pixels to the right
is going to happen every frame, so just imagine for a second what that's gonna
look like, and now let's run the game and see for ourselves. As you saw the instances simply went off screen. They moved all the way to the right and just
disappeared. Now that happened because they were moving
every frame. By default our game runs at 60 frames per
second, so this code was executed 60 times in one second. And that's why the instances moved so fast. Let's change this 50 to a 5, and see what
happens. I'll run the game, and now the instances are
moving slowly and they just keep moving to the right. They're moving at 5 pixels per frame,
and that's how the Step event allows us to program the behavior of an object. --
So that is the difference between the Create and the Step events. This runs only once, but this runs every frame. Now let's look at some Input events. I'll go into Add Event, and then go under
Key Down. These are events that run when you hold a
key. So I'll add the event for the Right key. This event runs when the Right arrow key is
held down. I'll go back in the Step event, take the code
from here, and paste it in the Key Down event. So now this should only run when we have the
Right arrow key held down. Let's run the game, and you can that see the
instances are not moving on their own now, and when I press the Right arrow key, they
start moving. So our input event is working. Now you can move only to the right, so to
move left as well, I'm gonna go under Add Event, then under Key
Down, I'll select this Left event. This of course is the event for the left arrow
key. Now in the event, instead of increasing the
x, we need to decrease it, to make the instance move left. So in this line I'll simply change this plus
sign to a minus sign, and now the x will go down by 5 pixels. I'll run the game again, and now you can see
that I'm able to move both left and right. ---
Now we're briefly going to talk about one more important event before getting into GameMaker
Language, And that event is the Draw event. This is similar to the Step event, as it runs
every frame, But this event is specifically used to draw
something on the instance. So you can draw another sprite, some text
or a shape. You can't do any of that in the Step event,
so that's why this Draw event is special. As we learn more about coding we're gonna
be using this event to draw text and sprites. Now we have some code over here in these Key
Down events, and I'm going to delete these events for now. From the next lecture we're gonna learn about
the basics of coding from the ground up, so you can understand how code works and why
it works. So I'll see you there. I hope you enjoyed the video. You can watch more tutorials from here, and
make sure to subscribe for more GameMaker content. I'll see you in the next video.