• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Clamping the view

S

spaceman_games

Guest
I would use the 'view_xview = clamp...' function to stop the view from going out side of the room. But with the new functions I'm having a hard time figuring out what is a similar solution in GMS:2.

Any suggestions?
 

Perseus

Not Medusa
Forum Staff
Moderator
http://docs2.yoyogames.com/source/_... and display/cameras/camera_set_view_pos.html

Code:
var cw = camera_get_view_width(view_camera[0]);
var ch = camera_get_view_height(view_camera[0]);
var vx = camera_get_view_x(view_camera[0]);
var vy = camera_get_view_y(view_camera[0]);
camera_set_view_pos(view_camera[0], clamp(vx, 0, room_width - cw), clamp(vy, 0, room_height - ch));
It's a lot more complicated, but definitely possible.
 
S

spaceman_games

Guest
Yeah, it for sure if much more complicated! I actually tried using the camera_set_view_pos but I def didn't try making variables to hold the values. Can you not make it happen without make the local vars?

Thanks for the help by the way it does work very well.
 

Perseus

Not Medusa
Forum Staff
Moderator
Yes, simply replace the variables from the camera_set_view_pos() function call with the functions that I've called-and-assigned to them. But as you'd see, the code becomes unreadable and hard to follow, so I've used local variables to increase readability.

Code:
camera_set_view_pos(view_camera[0], clamp(camera_get_view_x(view_camera[0]), 0, room_width -camera_get_view_width(view_camera[0])), clamp(camera_get_view_y(view_camera[0]), 0, room_height - camera_get_view_height(view_camera[0])));
 
S

spaceman_games

Guest
Oh yeah I see now haha. Much cleaner with the vars implemented.
 
Top