Cameras are EASY!

Game Maker Studio 2Gameplay Tutorials

This week we’re going to briefly discuss Cameras in Game Maker. Cameras in Game Maker Studio 2 were revamped, and for some reason, they seem to be quite a mystery. But in actuality they are SUPER simple.

Just like in Construct, Game Maker cameras follow the SAME pattern.

Cameras don’t care about your layout size.

Something that confuses most people is the width and height of their layout, when really, it does not matter AT ALL.

For example, the size of your room (layout) could be 1×1, or 5,000 x 5,000 and you can still get the exact same result!

The layout is our space to build.

The camera is how we pan around this space.

What we are going to do today is program a camera to follow our player so we don’t get lost.

Game Maker has many easy ways to do this, but I know you want to demystify some GML code, so here we go:

  1. Go into your room 0 and make your layout size 2,000×2,000

Our camera, and our window are now 640×480

You’ll notice that GM has up to 8 viewports, or 8 different Cameras available to us (which is more than enough for most games) and it starts by counting it at 0 and ending at 7. Highly unlikely you will need all 7 early on in your development training.

BEFORE CONTINUING: You should put a Player object into the room first, if you haven’t ever made one, you can see how to make a super easy one right here.

Now that we have our room set-up, we need to make a Camera object.

Make a new object called ob_camera and add 2 events:

  1. Create event

In the Create event we need to write 1 instance variable to find our player like so:

target = instance_find(ob_player, 0);

We now have an instance variable named “target” that is using the GML function instance_find to get the very first player object we created.

Instance_find has two parameters

  1. The object to find (ob_player)
  2. The number of the instance of ob_player to find (0)

In this case we are looking for the ob_player with the ID of 0 to find the FIRST ob_player created.

This is handy but not mandatory (I really just think it’s good for you to learn) but the important thing is that we’ve assigned target to the player object.

Now, we go to the step event in our ob_camera.

If instance_exists(target){

// code goes here

}

This is an additional check, and one you should also get familiar with. As your Game Maker game grows, any camera code we’re about to write, will only run so long as the player exists!

If our player object is destroyed, it won’t run, therefore we won’t get any wild errors with our camera down the line.

Next, inside this if statement, we set our cameras x and y position to our target.

x = target.x;

y = target.y;

We almost have a functioning camera!

We just need to tap into that viewport[0] we set to visible back in the room. You know, that white box that surrounds our layout that’s 640×480?

We need to control that via code, and its easy too!

var cam = view_camera[0];

Boom! Now we have the camera assigned to a local variable named cam. We want it to be local so we only use when the player is on the move. Camera functionality is ALMOST there!

camera_set_view_pos(cam, x, y);

This is telling Game Maker to set our cameras position to whatever our cam variable equals (view_camera[0];) and whatever our x and y equal (the players x and y).

So hit save! And run the game.

Oo0o0o0o0o0oo….

It’s not oriented right…

The player is in the top left, and that makes no sense? Why does it position to the top left?

Because in Game Maker everything goes to 0,0.

We need to divide the Camera in half, to re-center it to our player.

To do this, we need to subtract from the x and y position and then multiply by .5 (or divide by 2) so we set our camera position in half (and subtract 320×240).

camera_set_view_pos(cam, x – camera_get_view_width(cam) * .5, y – camera_get_view_height(cam) *.5);

camera_get_view_width and camera_get_view_height literally get our current cameras width and height (640×480).

Now hit play and watch our Camera follow our Player dead center throughout an infinite room!

While the new camera system has many more functions that look confusing it’s nothing more than a bunch of getters and setters!

DOUBLE CHECK: You should now have a camera following your player infinitely around your map but make sure you put some other objects on there to differentiate where you are in the level!

If you liked this weeks email, please reply and let me know. I will make this into a video later this week.

Jerementor