Object

An object is a prototype definition of an interactive element in your game.

Introduction to Objects in Game Maker

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 local variable, object_index 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 immediately, 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().

Events

Crap happens.

Crap happens to objects. When sh*t happens to an object we could call it a crap event.

In fact, that's exactly how Game Maker works.

When something happens to an object, it is an event. The object can then react to the event by using actions.

Actions

An Action is what an object can do when an event happens to it.

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.

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 angle between two objects can be calculated using point_distance().

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

Object Collision

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

Related Pages

Errors

Game Physics

GML

Tutorials & How To

Challenges

Links

To Do

  • Link objects together
  • Creating objects during runtime
  • object id
  • change objects
  • error : creating instance for non-existing object
  • more needs to be written about parent objects
  • controller objects need to be fully described
  • destroying individual objects (maybe instances?)
  • drag and drop objects (pick up put down)
    • following views
  • find an object's (instance?) position
  • check if an object (instance?) is in a room
  • how to make an object (instance?) chase / follow another
  • make an object visible / invisible
  • push / pull objects
  • magnetic objects
  • sliding objects
  • include links to objects by role (ie, projectile, icon, etc)
  • placing an object at a specific location
  • make a wall
  • parent collisions
Game Maker Elements: Backgrounds | Fonts | Objects | Paths | Rooms | Scripts | Sounds | Sprites | Time Lines
Categories: Game Maker : Objects