Question: How do you implement lighting in GameMaker?


GameMaker Studio provides a range of tools and functions that allow the creation of lighting effects, which can enhance the atmosphere and visual appeal of a game. Here is a comprehensive guide to implementing lighting in GameMaker:

Surfaces for Lighting

Surfaces in GameMaker act like canvases where you can draw anything, including light and shadows. They are crucial for dynamic lighting.

// Create a surface if it does not exist if (!surface_exists(light_surface)) { light_surface = surface_create(room_width, room_height); } // Set the target surface to light_surface surface_set_target(light_surface); draw_clear_alpha(c_black, 0); // Clear the surface with a fully transparent color // Reset drawing target surface_reset_target();

Basic Light Sources

You create a light source by drawing a bright shape (like a circle for a soft light) on the light surface.

// Draw a white circle on the light surface for a basic light var radius = 128; var x = mouse_x; var y = mouse_y; surface_set_target(light_surface); draw_set_blend_mode(bm_add); draw_circle_color(x, y, radius, c_white, c_yellow, false); draw_set_blend_mode(bm_normal); surface_reset_target();


Shadows can be created by using primitives or sprites to represent the absence of light. This usually involves subtractive blending.

// Draw shadows on the light surface; they must be drawn after the lights surface_set_target(light_surface); draw_set_blend_mode(bm_subtract); draw_sprite(spr_shadow, 0, shadow_x, shadow_y); // Assuming spr_shadow is a sprite representing your shadow draw_set_blend_mode(bm_normal); surface_reset_target();

Applying Lighting to the Game

Once the light and shadows have been drawn onto the surface, apply it over the screen.

draw_surface_ext(light_surface, 0, 0, 1, 1, 0, c_white, 1); // Apply the surface with normal alpha


To optimize performance, only update the lighting when necessary and use lower resolution surfaces if full resolution isn't required.

Advanced Lighting

For more advanced techniques like normal mapping or dynamic shadows, you may need additional scripts and shaders. These can give a pseudo-3D effect and react realistically to moving light sources.

Remember to release any surfaces when they are no longer needed to prevent memory leaks:

if (surface_exists(light_surface)) { surface_free(light_surface); }

In conclusion, creating lighting in GameMaker involves understanding and working with surfaces, blend modes, and sometimes shaders. Light and shadow interactions significantly improve the mood and depth of a game.

Note: As GameMaker updates, new functionalities may improve or change the way lighting is handled. Always check the latest documentation.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.