Introducción al Desarrollo de Videojuegos con GameMaker · Module 2: Sprites, Objects, Events, Instances, and Rooms

Creating Objects and Their Relationship with Sprites

Lesson 7 of 60 · 50 min

Also available in: ES

Introduction: The Visual and Logical Duality in GameMaker

One of the most common conceptual hurdles when getting started in game development is assuming that on-screen images are independent entities that act on their own. In GameMaker, a static or animated image has no intrinsic ability to move, process collisions, make decisions, or track data such as health or score. To bring a project to life, the engine establishes a clear boundary between two concepts: the graphic asset (Sprite) and the logical entity (Object).

A sprite is a passive asset composed of pixels structured across one or more frames (sub-images), along with origin points and collision masks. In contrast, an object is an abstract behavioral blueprint that holds variables, responds to events, and executes logic on every game tick (frame or step). In this lesson, you will learn how an object is created, how a visual representation is assigned to it, and how the engine keeps both layers in sync.

Schematic diagram illustrating the separation between a sprite as a graphical asset and an object as a logic-bearing entity in GameMaker.
Figure 1: Architectural separation between appearance (Sprite) and behavior (Object). · AI-generated image

Key Technical Differences: Sprites vs. Objects

To prevent confusion when structuring a game, it helps to analyze the technical responsibilities of each element within GameMaker's memory and execution model.

FeatureSprite (Graphical Asset)Object (Logical Entity)
NatureImage data and collision mask.Container for logic, events, and variables.
Default naming conventionPrefix spr_ (e.g., spr_jugador_quieto).Prefix obj_ (e.g., obj_jugador).
Memory presenceTexture stored in video memory (VRAM).Data structure managed by the CPU in RAM.
Computational capabilityNone. Contains no code or events.Full. Executes scripts, computes physics, and handles interactions.
Direct room placementOnly as visual decoration on Asset Layers.As interactive instances on Instance Layers.
MultiplicityA single sprite can be bound to hundreds of distinct objects.A single object can dynamically swap between multiple sprites.

The classic theater metaphor works remarkably well: the sprite is the costume or visual mask, whereas the object is the actor beneath it. The actor can change outfits (switching sprites when jumping, running, or taking damage), perform without any costume at all (an invisible object that manages level audio or wave spawning), or share the exact same outfit with other actors without their thoughts or individual states interfering with one another.

Step-by-Step: Creating and Configuring an Object

In the previous lesson, we imported the base sprite for our retro platformer character, named spr_jugador_quieto. Now, we will transform that visual asset into a living entity using the following steps within the GameMaker IDE:

1. Locate the Asset Browser panel, typically found on the right side of the workspace. 2. Right-click on the Objects folder (or press Alt + O) and select Create -> Object. 3. In the Name field, enter obj_jugador. Staying consistent with the obj_ prefix is an industry standard that prevents naming collisions in your code. 4. In the Object Editor window, find the Sprite box on the left-hand panel. Click on the slot currently displaying 'No Sprite'. 5. An asset picker pop-up will appear. Select the spr_jugador_quieto asset we prepared in the previous lesson.

💡 Important: Never give an object the exact same identifier as a sprite. If you name both the sprite and the object 'jugador', GameMaker will throw reference errors or assign variables unpredictably during compilation, as both asset names are registered in a single global table of integer indices.

Structural Properties in the Object Editor

Inside the Object Editor, several checkboxes fundamentally alter an object's behavior before you write a single line of GML code:

- Visible: When checked (default), the engine automatically draws the assigned sprite on every frame. If unchecked, the object will still execute all internal logic and collisions, but will remain completely invisible to the player. - Solid: A legacy feature for static collisions. In modern GML development, it is best kept disabled, as we will manage terrain collisions through precise code in later modules. - Persistent: Determines whether the object instance is destroyed upon changing rooms or survives intact across different levels. Leave this disabled for standard entities like enemies or the base player character for this level. - Uses Physics: Enables the integrated Box2D physics engine. Because our retro platformer uses custom kinematic arithmetic, this option must remain unchecked.

Native Sprite Variables in GML Code

When you bind a sprite to an object, GameMaker automatically initializes a set of reserved built-in variables. These variables belong to the instance's scope and allow you to modify its visual appearance in real time from any GML code event.

Built-in VariableData TypePrimary Function
sprite_indexSprite asset (ID)Defines which graphic resource the instance is using on the current frame.
image_indexReal (Decimal)The specific sub-image or animation frame currently being drawn.
image_speedReal (Multiplier)Scale factor for animation playback speed (1.0 = normal speed, 0 = paused).
image_xscaleReal (Factor)Horizontal scale of the sprite. A value of 1.0 is default size; -1.0 flips it horizontally.
image_yscaleReal (Factor)Vertical scale of the sprite. A value of 1.0 represents natural scale.
image_alphaReal (0.0 to 1.0)Instance transparency. 1.0 is fully opaque, and 0.0 is completely invisible.
image_blendColor (Hexadecimal/Constant)Color tint multiplied over the pixels (c_white renders original colors).
mask_indexSprite asset (ID)Sprite whose collision mask will be used instead of the active sprite's mask.

Hands-on Implementation: Modifying Appearance via Code

To reinforce how an object controls its sprite, let's set up our first initialization block in obj_jugador using GML Code. While we will explore the Create event in depth in the next lesson, this initial hands-on exercise is essential.

Inside the Object Editor for obj_jugador, click the Add Event button, choose the Create event, and make sure to select GML Code if prompted between GML Code and GML Visual. Enter the following statements:

// Initialize visual properties for obj_jugador
// Event: Create

// Ensure the active sprite is the one assigned initially
sprite_index = spr_jugador_quieto;

// Ensure the animation starts strictly on its first frame
image_index = 0;

// Temporarily freeze the animation cycle until the player moves
image_speed = 0;

// Ensure horizontal rightward orientation and default 1:1 scale
image_xscale = 1;
image_yscale = 1;

// Full opacity and natural tint without color alteration
image_alpha = 1.0;
image_blend = c_white;

If you run a quick test by pressing F5, the obj_jugador instance will render on screen paused on its first frame, proving that the object's logic holds full authority over the visual resource's playback settings.

Invisible Objects (Managers and Controllers)

Not every object requires a sprite. One of the most important architectural patterns in GameMaker is the logical controller, commonly referred to as a 'Game Manager'. These are objects created without any assigned sprite ('No Sprite').

An object without a sprite still possesses coordinates within the room and runs all of its events as usual. It is commonly used to stream background music, manage level timers, count collected coins, or verify whether the player has satisfied stage completion objectives. Separating global logic from visual actors keeps your game code modular and maintainable.

GML Visual vs. GML Code for Visual Management

GameMaker allows you to manage these properties using either drag-and-drop visual nodes (GML Visual) or direct code statements (GML Code). In GML Visual, changing a sprite involves dropping the 'Set Instance Sprite' block and filling in text fields for the asset and frame. However, in professional production and throughout this course, we will use GML Code, as writing direct statements such as 'sprite_index = spr_salto;' is significantly faster, easier to debug, more scalable, and cleaner when coordinating complex gameplay mechanics.

Practical Exercises

Complete the following three activities to solidify your understanding of the relationship between objects and sprites in your current project:

Exercise 1 (Base Entity Setup): Create a new object in your project named obj_bloque_suelo. Assign it a static 32x32 pixel sprite representing a stone or dirt tile (e.g., spr_bloque_tierra). Ensure that Visible remains enabled and Solid is disabled. Verification: When you open the object properties, the tile thumbnail should appear on the left, and the identifier should contain no spaces or special characters.

Exercise 2 (Visual Manipulation Through Code): Open the obj_jugador object created during this lesson. In its Create event, adjust the built-in variables so the character starts at double scale horizontally and vertically (image_xscale = 2; image_yscale = 2;) and at 50% opacity (image_alpha = 0.5;). Verification: Place an instance of obj_jugador in your test room and press F5; the character should appear twice as large and semi-transparent on screen.

Exercise 3 (Creating an Abstract Controller): In the Asset Browser, create an object named obj_control_juego. Do not assign it any sprite (leave the slot as 'No Sprite'). Add a Create event and insert an explanatory GML Code comment noting that this object will manage background audio and global scoring. Verification: In the Asset Browser, the object should display the default empty or question-mark icon indicating the absence of a graphic asset, confirming it is a purely logical entity.

Summary

  • A sprite is a passive texture asset; an object is an active logical blueprint that runs events and code.
  • An object's visual rendering is controlled via built-in instance variables: sprite_index, image_index, image_speed, image_xscale, image_yscale, image_alpha, and image_blend.
  • Sprite and object identifiers must never share the same name; standard prefixes (spr_ and obj_) are used to distinguish them cleanly.
  • An object can exist and execute without an assigned sprite, which serves as the design standard for system controllers, audio managers, and global game state.

Practise what you learned

Exercise 1 · 1 point

Match each built-in GML variable to the visual property it controls on an instance:

  • sprite_index
  • image_speed
  • image_xscale
  • image_alpha
You are practising without an account: nothing is saved. Sign up to keep your progress.

Exercise 2 · 2 points

In GML Code, write the assignments needed in the Create event to: stop the animation cycle (speed 0), flip the character horizontally (horizontal scale -1), and set its opacity to 50% (0.5).

Your code is not executed: specific requirements are checked on the text.

You are practising without an account: nothing is saved. Sign up to keep your progress.

Exercise 3 · 1 point

What happens to an object in GameMaker if the 'Visible' checkbox is unchecked in the Object Editor?

You are practising without an account: nothing is saved. Sign up to keep your progress.

Sign in to save your progress — Sign in