Introducing Dragonfly Cloud! Learn More

Question: How do you configure and use viewports in GameMaker?

Answer

In GameMaker, viewports are used to display portions of your game room on the screen, allowing for multiple views or camera systems. To configure and use viewports in GameMaker, follow these steps:

  1. Enabling Viewports: First, you need to enable and configure the viewports within your room settings.

    // Enable the viewport room_set_view_enabled(true); // Configure the viewport var viewportIndex = 0; // Viewport 0 is the first viewport var visible = true; var view_xview = 0; // X position of the top-left corner in the room var view_yview = 0; // Y position of the top-left corner in the room var view_wview = 1024; // Width of the view in the room var view_hview = 768; // Height of the view in the room var view_xport = 0; // X position of the top-left corner on the display var view_yport = 0; // Y position of the top-left corner on the display var view_wport = 1024; // Width of the viewport on the display var view_hport = 768; // Height of the viewport on the display // Apply the configuration view_set_visible(viewportIndex, visible); view_set_viewport(viewportIndex, view_xport, view_yport, view_wport, view_hport); view_set_region(viewportIndex, view_xview, view_yview, view_wview, view_hview);
  2. Camera Setup: Each viewport can be linked to a camera instance, which controls what portion of the room is shown.

    // Create a camera for the viewport var cam = camera_create_view(view_xview, view_yview, view_wview, view_hview, 0, 0, 0, 0, 0, -1); // Assign the camera to the viewport view_set_camera(viewportIndex, cam);
  3. Following an Instance: You might want to have a camera follow a player or object in the room.

    // Set up a camera to follow an instance (for example, an object named obj_player) var hborder = 100; // Horizontal border before the camera starts following var vborder = 50; // Vertical border before the camera starts following var hspeed = -1; // Horizontal speed of the camera (-1 means instant follow) var vspeed = -1; // Vertical speed of the camera (-1 means instant follow) var inst = instance_find(obj_player, 0); // Find the player instance view_object_follow(viewportIndex, inst, hborder, vborder, hspeed, vspeed);
  4. Multiple Viewports: If you want to use multiple viewports, repeat the above steps for each viewport with different viewportIndex values and positions.

Remember that GameMaker allows for up to 8 viewports (0-7) and that the configurations can also be done directly through the Room Editor's interface in the IDE if you prefer a visual setup over coding.

When working with viewports, keep in mind aspect ratios and resolutions of different devices to ensure that your game looks consistent across platforms.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

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