GameMaker: How to Wrap the Room

By Michael Rohde

The following procedure shows you how to create a Script to wrap the Room as part of the Step Event. In the classic videogame Asteroids, the player’s ship could leave one side of the Room only to reenter on the opposite side of the Room (for example, it might leave on the left and reenter on the right).

  1. From the Resource tree, right-click Scripts and select Create Scripts.

    The Script window appears.

  2. In the Name field, type phy_wrap.

  3. In the code section, type the following code:

    if phy_position_x < 0 phy_position_x += room_width;
    if phy_position_x > room_width phy_position_x -= room_width;
    if phy_position_y < 0 phy_position_y += room_height;
    if phy_position_y > room_height phy_position_y -= room_height;
  4. Click the green check mark.

    The Script window is saved and closed.

This code is different from what you might use when working with X and Y positions, because this example is based on physics and not the basic movement and collision system (non-physics).

Now you need to call this Script from an Object. To apply a Script to an Object, follow these steps:

  1. From the Resource tree, in the Objects section, double-click the obj_Player Object (or the object you want to call the Script).

    The Object Properties window appears.

  2. Choose Add Event→Step→Step.

    A Step Event appears in the Events section.

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

    An empty code window appears.

  4. In the code window, type the following code:

    phy_wrap();
  5. Click the green check mark.

    The code window is saved and closed.

This code calls the Script and applies it to the Object. Now, when an Instance of this Object is placed in the Room, the player can fly the Instance through one side of the Room only to reenter on the opposite side.