GameMaker Creating and destroying cameras (post build 2.2.X) - Added video by @Pixelated_Pope

F

Finn

Guest
Views and cameras have undergone lots of changes with the shift from 1.4 to 2 and then again within the 2.X builds. There are a lot of tutorials and guides available on handling cameras many of which are out-dated.

I found that some of the camera handling I did for GML2 (specifically setting proj. & view matrices directly) would not work properly anymore with any build after V 2.1.4.218 - causing strange flickering effects when using surfaces - even though the camera system worked flawless before.

With any build after 2.1.4.218 whenever I would set the drawing target to any surface GMS2 would start drawing any draw event that came after that line of code in random positions (including tileset layers).


1) DESTROYING CAMERAS

Whenever starting a new room the camera of the previous room will be destroyed using the following script and a new one will be created using the room's properties. The code below is furthermore used in the clean_up event of the camera object to avoid potential memory leaks.

Code:
/// @description destroy camera
/// @argument view_port to reset
    var _i = argument[0];                // viewport to reset
  
    view_visible[_i]    = false;        // disable view visibility
    view_enabled        = false;         // disable view before destroying camera
          
    if (view_camera[_i] != -1) {        // if camera assigned destroy and reset
        camera_destroy(view_camera[_i]);
        view_camera[_i] = -1;
        }
      
    show_debug_message("obj_camera: camera destroyed; view_camera[" + string(_i) + "] = " + string(view_camera[_i]) +".");

2) CREATING CAMERAS

The camera object (obj_camera) follows the player or other relevant objects around. This has the advantage that it allows control over where the camera goes by just moving the camera object for instance by giving it dynamic movement. It furthermore allows for more smooth camera motion than the build-in functions.

The approach of solely setting up the camera using matric operations does not seem viable anymore.

Code:
        //    Setup new camera
            view_camera[0]    = camera_create_view(0,0,viewWidth, viewHeight, 0, obj_camera, -1, -1, 0.5*viewWidth, 0.5*viewHeight);
            view_visible[0]    = true;
            view_enabled    = true;
 
Last edited by a moderator:

samspade

Member
Any opinions or feedback?
The only issue I've had in the recent months with cameras is that sometime after September, using a surface broke the camera and you had to either call camera_apply after using one or manually save the various matrixes to a variable and then reset them.

See this post: https://forum.yoyogames.com/index.p...though-no-code-has-changed.55627/#post-337835

Otherwise, I still use the matrix versions in all my projects. I normally either destroy the camera at room end and recreate it at room start through some persistent controller object or have a user event in the camera that I call when I want to which 'resets' the camera. Mostly this is force of habit and the fact that matrix cameras aren't locked to the room. Also, once you've learned how they work, they're as easy to deal with as the built in camera system and slightly more flexible.
 
Top