GML How To Make Camera Not Reset It Position Upon Entering Another Room



Hi there.
So, i have problem with my camera movement.
I want my camera to not reset it position when i re-entering a room. As you can see it below

When i enter the second room and go back to the first room, you can see that my camera reset it position for a second. I want my camera to stay in its last coordinate when i changing room.
This is the code that i use

if (instance_exists(follow))
    xTo = follow.x;
    yTo = follow.y;

//Update object position
x += (xTo - x) / 12;
y += (yTo - y) / 12;

x = clamp(x,view_w_half,room_width-view_w_half);
y = clamp(y,view_h_half,room_height-view_h_half);

//Update camera view

///This is under create
cam = view_camera[0]
follow = MC;
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;
You probably already saw this code before, because i take it from Spalding. So far, the only code that i write on my own is the portal/door script. I have very little knowledge and experience with coding, so i would love to hear a noob-friendly explanation. Btw, if i ever finish this game i will credit you if you have the answer for my problem. Hahaaa

Thanks in advance.


Do you have one persistent camera? Or a camera in each room?
Hi, thanks for the reply. At first i have 1 camera in each room, but then i only use 1 and if im not mistaken i set it to persistent.
Anyway i pull an all nighter and already solve this issue. I improvised Spalding's code.

x = clamp(MC.x,view_w_half,room_width-view_w_half);
y = clamp(MC.y,view_h_half,room_height-view_h_half);
MC is my character object. That way the camera in first room wil remain in the last position.