Objects

Object Guide

Objects in Game Maker

Objects are are core of object oriented programming. Just about anything in your game that can interact with something will be an object. Your main character, enemies, level walls, etc will probably all be objects. Actually, that's not quite true.

An object is actually a definition of a type of something. You use it to create instance of the object in the game. To understand this, let's make an analogy. An object is a cookie cutter. It contains the shape and size of the cookie. You can use a cookie cutter to make as many cookies as you want. Each instance created "knows" which object it was created from because it has a variable, object_id which contains the index of the object prototype.

It is the same with objects and instances: if you define an alien ship as an object, lets say, with the name obj_alien_ship. Then, whenever you need an alien ship, you create an instance of it. If you need more than one alien ship, you can create as many instances of the object obj_alien_ship as you want. All of the instances will have the same qualities as the object it was created from.

Sprites

Objects are assigned sprites using sprite_index. This sprite is displayed during the Draw event every step automatically. An instance of an object without an assigned sprite will not be seen, nor will it be able to "collide" with anything, as collision detection is based on sprites. To draw the sprite immedietly, put the draw sprite under a room start or game start event.

If you are using the Draw event of an object, it's sprite will not be automatically drawn, and therefore the instance will not be seen. If you want to use the Draw event of an instance and have it's sprite displayed, you will have to draw the sprite manually with draw_sprite().

Types of Objects

The types of objects below are not special objects. They are constructed just like any ordinary object. They are considered different because they are put to a special use.

Player Object

The player object is the object that represents the player. Most control code is contained in the player object because it is the object being controlled.

Controller Objects

Controller objects are used to control various aspects of a the game that you do not want to control with the player object. In other words, controller objects run things behind the scenes. Controller objects are used to control and draw such things as menus and huds and perform such functions as change the room based on score.

Parent Objects

Since GML is an object based language, objects can inherit code and properties from their parents. This is a very powerful and time saving feature of Game Maker. If you make a parent object for all enemies, the Collision event for the parent object will apply to all children of the parent object.

Inheritance

However, if you change anything, it will overwrite the entire section of the child. For example, if you had some code in the parent object in a keyboard event and you change that code in the child object, the child will retain all the properties and code of the parent, but it will have it's own code in the keyboard event.

To make the child execute it's parent's original code, you will need to use the Call Parent Event action.

Setting the Parent Object

You can set the parent object for an object in the Object Properties Window.

Weapons

Even if a weapon is not itself an object, it can be said that objects have weapons. Weapons come in two flavors, melee weapons, and projectile weapons. The difference is in how the weapon is used to strike another object.

Object Naming Conventions

Objects are typically named by programmers to help remind them what they are, below is a list of prefixes used in these naming conventions :

Prefix Used For Example
obj_ ordinary object obj_player
con_ or cont_ controller object cont_scroller
par_ parent object par_wall

Changing Objects

You can change objects during runtime, but changing objects for which there are instances in play can cause errors. There is no change_object() function nor is there a Change Object action1 however you can change the definition of objects directly using these functions :

Object Function Does
object_add() Create / Add a new object to your game.
object_delete() Delete an object.
object_event_add() Add an event to an object.
object_event_clear()
object_set_depth()
object_set_mask()
object_set_parent()
object_set_persistent()
object_set_solid() Set the object to handle sprite collisions as solid
object_set_sprite()

Distance Between Objects

The distance between two objects can be calculated using point_distance().

point_direction ( obj1.x, obj1.y, obj2.x, obj2.y );

Object Collision

Objects do not collide. Their current sprites do. See Sprite Collision.

This page is a stub. You can help by expanding it.