Create a Surface with Shadows in GameMaker: Studio - dummies

Create a Surface with Shadows in GameMaker: Studio

By Michael Rohde

One problem with drawing objects in GameMaker: Studio that you might come across is that shadows don’t tend to be square with rigid lines. The following procedures show you how to create a Surface to draw shadows for the walls.

To create a new Object for the Surface effect, follow these steps:

  1. With the Surfaces tutorial open, from the Resource tree, right-click Objects and select Create Object.

    The Object Properties appear.

  2. In the Name field, type obj_Shadow_Surface.

  3. In the Depth field, type –900.

    This Depth will draw the shadow under the wall Instances so that you won’t ever see the shadow effect on or over the wall itself.

  4. Choose Add Event→Create.

    The Create Event appears in the Events section.

  5. Drag and drop an Execute Code Action from the Control tab to the Actions section.

    A code window appears.

  6. In the code window, type the following:

    surf = -1;

    This code creates a variable for the Surface but does not create the Surface itself.

  7. Click the green check mark.

    The code window is saved and closed.

You’ll create the Surface effect in the Draw Event, which is triggered every step of the game.

First, though, the following procedure shows how to remove the Surface effect when you no longer need it.

  1. From the obj_Shadow_Surface Object Properties, choose Add Event→Other→Room End.

    The Room End Event appears in the Events section.

  2. Drag and drop an Execute Code Action from the Control tab to the Actions section.

    A code window appears.

  3. In the code window, type the following:

    if surface_exists(surf)
    {
    surface_free(surf);
    }
  4. Click the green check mark.

    The code window is saved and closed.

This code frees up the memory that the Surface was using.

If the memory is not freed when the Surface is no longer needed, the game could develop a memory leak, which will eventually crash your game.