1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Drag And Drop Check if object is in viewport ?

Discussion in 'Programming' started by Antera, May 14, 2019 at 9:11 AM.

  1. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    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
     

    Attached Files:

    • if.jpg
      if.jpg
      File size:
      28.2 KB
      Views:
      4
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,508
    Pretty much that.
     
  3. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    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 ?
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,508
    ///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: May 14, 2019 at 7:16 PM
    Japster and Antera like this.
  5. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    Thanks TheouAegis
    Why is there a warning to view_wview ?
     

    Attached Files:

    • vvv.jpg
      vvv.jpg
      File size:
      50.8 KB
      Views:
      4
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,960
    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.
     
    Antera likes this.
  7. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    Thanks. This code was given here just above. It's working now :)
     
  8. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    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;
     
  9. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,098
    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]);
     
    Antera likes this.
  10. Antera

    Antera Member

    Joined:
    Tuesday
    Posts:
    24
    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]);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice