Creating Objects In GameMaker Studio 2

Object Basics

Objects are the blueprints for content you want in your game. For example, you might have an object that describes a character. Our character object would contain a sprite and events that define how to move the character around the screen. When you want to use your object that's called creating an instance. If the object is like a blueprint for a house, an instance could be considered the physical house. More on instances later. Objects are very important to your game and along with rooms, are going to make up the majority of your game.

Creating An Object

To create an object, find the node labeled "Objects" in the resource tree, right click and select "Create Object". If you start typing immediately you can enter a name for your object.
Creating an Object with GameMaker Studio
Creating an Object with GameMaker Studio from the workspace
You can also create an object by right clicking in the workspace and selecting "Resources -> Create Object", or by pressing the keys "ALT + O" on your keyboard.

Object Properties

You can enter a name for your object by changing the value in the box underneath "Name:". By default it will be set to object0, object1, etc. It is a good idea to use descriptive names for objects, and most resources in your game, that include a prefix to identiy what type of resource they are. For example, if we were creating a character object I would use the name "obj_character".
Naming and Object with GameMaker Studio
Assigning a Sprite to an Object with GameMaker Studio
Objects can have sprites associated with them so that they are displayed on the screen, but it isn't a requirement. Think of it like this. In the example above we described a character object. We would want to associate the sprite for our character with the object so that the character is displayed on the screen. But what if we're creating a map for our character to walk around in? We might have an object called "obj_wall" that doesn't have a sprite associated with it. We'd place this object over the top of areas on the map the character shouldn't be able to walk, and then test for collisions between it and "obj_character" to see if the character is allowed to move or not. To associated a sprite with an object, under the label "Sprite:", click in the area that says "No Sprite" and select the appropriate sprite from the pop-up box.
Further down the object properties panel you'll see a box you can check or uncheck next to the word "Visible". This is a useful property for assigning whether or not the sprite for the object is initially displayed on the screen. As an example, you may have a treasure chest that only appears once the player has stepped on a button. You would uncheck the box for visible on your treasure chest object, and then use some code in a collision event to change the visibility of the object when the player steps on the button.
Changing the visibility of an object in GameMaker Studio

Object Events

Adding an Event to an Object in GameMaker Studio
Events are actions that happen under specific conditions in your game. I'll go into more detail on specific types of events in later tutorials, but we'll talk about a couple just to give you an idea of what they're about. To add an event to an object, select "Events" in the object properties panel. Then click "Add Event" and select the type of event you want to create.
A "Create" event is a type of event that happens any time an instance of an object is created in the game. Usually you use this type of event to setup defaults for your object. For example, you might use the create event to default a character's hit points when a game starts.
A "Step" event is a type of event that happens once for every frame of your game. So if your game runs at 60 frames per second, each step event for each instance of an object in your game will also run 60 times per second. It's a very powerful event that you'll use to do things like check to see if the player is pressing the arrows keys to move the character around the screen. Something to keep in mind, since this code is executed once per frame it's important to keep the code concise. If you think about this you'll see why. Say you have 10 instances of the enemy object in your game, plus your character object, a treasure check, two doors and a button. If your game runs at 60 frames per second that means you have 15 step events executing every frame, or 900 events happening every second. Only 15 objects would be considered a very small game, so you can see how this can get out of hand quickly.
A "Draw" event is very similar to the "Step" event in that it executes once per frame. This type of event is what actually draws content to the screen. If you don't create a draw event GameMaker Studio will check to see if you have a sprite assigned to the object and if it should be visible, and if so it will automatically draw the sprite. The unique thing about creating a draw event is the it overrides that functionality, so unless you explicitly state in your draw event that you want the sprite to be drawn, GameMaker Studio will not draw the sprite. You would use this event to do something like draw a health bar on the screen since the players health will change over time. Keep in mind that even though "Step" and "Draw" both execute once per frame, "Step" should be used for processing changes to your object while "Draw" should be reserved for performing actions that draw things to the screen. It is easy to get the two mixed up when you're getting started.

Parent Objects

Below the "Events" button in the object properties panel, you'll see a button labeled "Parent". This is a mechanism for you to implement something called "inheritance". I'll cover this in more detail in a later tutorial since this can be a little confusing when you're first learning, but it is very powerful once you grasp the idea. Let's look at a quick example. In your game you have enemies, and all enemies in the game have the same basic functionality. They all chase your character, and when they come into contact with your character we launch into battle. But they're each a little different as well. They probably have different sprites as well as a different number of hit points. This is where parents come in. What you can do is create what you would call a "base" object that implements the chasing and launching into battle. Then you would create your child object that specifies the sprite for the character as well as a "Create" event that defines how many hit points it has. You would then select "Parent" from the object properties panel of the child object, click where it says "No Object" under "Parent" and select the object you created as your "base" object.
Assigning a Parent to an Object in GameMaker Studio


Download the resource file below, and using the provided resources complete the following steps.
  • - Extract the resources pack and open the provided GameMaker Studio project file.
  • - Create a new object by right clicking in the resource tree and selecting "Create Object". Name the sprite "obj_character".
  • - From the object properties panel, select the area where it says "No Sprite" under the heading "Sprite" and select "spr_character".
  • - Create another new object by right clicking in the workspace and selecting "Resources -> Create Object". Name the object "obj_enemy".
  • - From the object properties panel, again, select a sprite only this time select "spr_enemy".
  • - Create a third object by pressing "ALT + O" while in the workspace. Name the object "obj_child_enemy". Just like before, select a sprite only this time select "spr_child_enemy".
  • - Back in the object properties panel for "obj_character" create a new "Step" event by clicking "Events -> Add Event -> Step -> Step". For now just copy the following code into the code editor. This is what's going to control our character using the arrow keys.
    move_speed = 3;
    x = x - move_speed;
    else if(keyboard_check(vk_right))
    x = x + move_speed;
    else if(keyboard_check(vk_up))
    y = y + move_speed;
    else if(keyboard_check(vk_down))
    y = y + move_speed;
  • - Still in "obj_character", add another event but this time select "Add Event -> Collision -> obj_enemy". Enter the following code into the code editor.
  • - Open the object properties panel for "obj_enemy" and create another "Step" event. Enter the following code.
    x = x + random_range(-1, 1);
    y = y + random_range(-1, 1);
  • - Now in the Resources panel, expand the node for "Rooms" and double click on "rm_demo" to open the room editor. Now, to create instances of the object you just created drag "obj_character" from the resources panel over onto the room editor somewhere. Repeat that for "obj_enemy" and "obj_child_enemy". It doesn't matter where you put them as long as the enemies aren't touching the character.
  • - Go ahead and run the game by selecting the run button from the toolbar, or by pressing F5. You should be able to move the character around the screen using the arrow keys, and the enemies should bounce around the room unintelligently. You'll notice if you chace down either of the enemies the game restarts.

Further Research

  • - Try creating a second child enemy object and placing it in your game.
  • - Try changing the child enemy object to be controlled by the "A", "S", "D" and "F" keys. Hint: The child enemy object will need it's own step event. You can check for the "A" key using the function "keyboard_check(ord("A")) ".
  • - See the tutorial on Creating Sprites