• Home
  • Tutorials
  • Downloads
  • Games
  • Articles
  • Contact

Objects

The most important resources in Game Maker are objects. Objects are normally assigned a sprite to represent them, and they also contain events and actions.

Once you have created and defined an object, you can place instances of that object in your rooms. An instance is a unique copy of the object. For example, if you have an object for a type of enemy, you can place multiple instances of that object with each of those instances executing their own actions.


We will create an object for our player, of which we will only ever need one instance.

Click the "Create an object" button.
Picture
The window that appears is a little bit complex. Ignore the right side for now.

The first step to defining an object is to set the sprite that will represent it in the game.

Click the button highlighted below and choose spr_player as the sprite.
Picture
You have now assigned a sprite to the object.

Th object should now be named. This is important so you can quickly find resources.

Name the object obj_player in the same way you named the sprite previously.

The next step is to define some behavior for the object.

Events and Actions
Every object has "events" and "actions". The concept is that when an event occurs, the actions set under it are executed.

There are a lot of actions in the far-right panel. However, these are known as the "Drag & Drop" actions, and should not be used. The only action you will use is the "Execute Code" action.

We're going to code your new object to move from the left side of the screen to the right at a constant speed.

Click the "Add Event" button.
Picture
You'll see a variety of event icons appear.

Click the Create event button.
Picture
Your object now has an active "Create" event. Whenever an instance of the object is created, it will immediately execute the actions we define.

To define these actions, we will be using a single line of code.

There are many tabs with a variety of actions in the far-right panel, however, we will ignore all of them except for the "Execute Code" action. This action allows us to define our own code, and is the only action we will be using, ever. All of the other actions are "Drag & Drop" actions which should be avoided.

Click the "Control" tab.
Drag an "Execute Code" action into the actions panel.
Picture
Upon dragging the "Execute Code" into the actions panel, the code editor will appear.

To make our object move to the right, we're going to call a Game Maker function named motion_set() which you will likely use frequently in the future.

The function requires 2 "arguments", shown within the parentheses (). If you type "motion_set" into the editor, you will notice down the bottom the term motion_set(dir,speed) appears. The 2 arguments are "dir" and "speed".

The argument "dir" refers to the direction you want the object to move. The unit is in degrees, with 0 degrees being east, 90 degrees being north, 180 degrees being west, 270 degrees being south.

The argument "speed" refers to how many pixels we want the object to move per step (30 steps per second).

Don't worry about necessarily understanding this right now, functions are covered in our GML tutorial later.

Write the line that is shown below, with "dir" as 0 and "speed" as 4.

Press "OK, Save changes".
Picture
You've now written your first line of code!

Now, when an instance of the object is created, your code action will execute, calling the function. The function will then move the object right at speed 4.

Press OK to close the object panel.
Picture
Finished!
You've now created your first object, assigned a sprite to it, defined an events and actions for it, and written your first line of code.

Again, this process might seem lengthy when formatted like this, but you'll be able to do this very quickly soon enough.

Are you ready to see the object in action?
Picture
Picture

How To Make An MMORPG © 2010-2012
Copyright | Privacy Policy