• 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!

GML Visual Check if object is in viewport ?

A

Antera

Guest
Hello,

How can I check if the instance of an object is in viewport, is there a function for this ?

Or do I have to add multiple DND "if" to reproduce something like this ?

if ( (x > view_xview[0]) && (y > view_yview[0]) && (x < (view_xview[0]+view_wview[0])) && (y < (view_yview[0]+view_hview[0])) )

I use Drag & Drop fow now

Thanks for your help
 

Attachments

A

Antera

Guest
Pretty much that.
Okay ... even if I use drag n drop, I saw that I could add script blocks : can I create a function obj_is_in_viewport(obj,wp) ?

Would you mind to help to create my first function ?
 

TheouAegis

Member
///is_inview(id)
with argument0 return (x > view_xview[0]) && (y > view_yview[0]) && (x < view_xview[0]+view_wview[0]) && (y < view_yview[0]+view_hview[0]);


You'd call it with one argument, like
if is_inview(nearest_enemy)
 
Last edited:
A

Antera

Guest
///is_inview(id)
with argument0 return (x > view_xview[0]) && (y > view_yview[0]) && (x < view_xview[0]+view_wview[0]) && (y < view_yview[0]+view_hview[0]);
Thanks TheouAegis
Why is there a warning to view_wview ?
 

Attachments

FrostyCat

Redemption Seeker
Thanks TheouAegis
Why is there a warning to view_wview ?
That's because view_wview[0] and the rest of the lot are obsolete. Use the Camera Getters listed on this page with view_camera[0].

If you're new and you're learning GMS 2, using GMS 1.4 resources is one of the most unwise things you can do.
 
A

Antera

Guest
That's because view_wview[0] and the rest of the lot are obsolete. Use the Camera Getters listed on this page with view_camera[0].

If you're new and you're learning GMS 2, using GMS 1.4 resources is one of the most unwise things you can do.
Thanks. This code was given here just above. It's working now :)
 
A

Antera

Guest
In fact it's not working, still on it :-(
it returns 0 sometimes, even the object is in view

var view_x = camera_get_view_x(view_camera[0]);
var view_y = camera_get_view_y(view_camera[0]);
var view_w = display_get_gui_width();
var view_h = display_get_gui_height();
var reponse = (x > view_x) && (y > view_y) && (x < view_x+view_w) && (y < view_y+view_h);
return reponse;
 
The GUI and the camera are separate, you shouldn't be trying to compare them in this context. This is what you should be checking instead.
Code:
var view_x = camera_get_view_x(view_camera[0]);
var view_y = camera_get_view_y(view_camera[0]);
var view_w = camera_get_view_width(view_camera[0]);
var view_h = camera_get_view_height(view_camera[0]);
 
A

Antera

Guest
Yes got it
var view_x = camera_get_view_x(view_camera[0]);
var view_y = camera_get_view_y(view_camera[0]);
var view_w = camera_get_view_width(view_camera[0]);
var view_h = camera_get_view_height(view_camera[0]);
 
Top