1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Check if object exists at position

Discussion in 'Programming' started by KetoGames, Sep 22, 2016.

  1. KetoGames

    KetoGames Guest

    I'd figure that this is a relatively simple question, however, I have yet to find an answer. Note, I've already tried using instance_place, however, it returns the instance id instead of true or false.

    Short question: How can I see if obj exists at x, y, and if it does, return true?

    Long question: I am creating a sandbox game (similar to Terraria, for those of you that have played before). I decided to embark on learning how to bitmask, which is something like this:

    https://gamedevelopment.tutsplus.co...ng-to-auto-tile-your-level-layouts--cms-25673 .

    My end goal is to make it so that every time I place a "tile" of one item (let's say wood, for this example) it will bitmask the sprite. That way, the end product will look much cleaner.

    Any help is appreciated, thank you!
     
  2. shadowspear1

    shadowspear1 Member

    Joined:
    Jul 4, 2016
    Posts:
    125
    First of all, if you're just checking if the object exists at a certain position, you shouldn't be using instance_place. Use instance_position instead. If you use instance_place, the function uses the calling object's bounding box to check for collision, instead of just the existence of an object at a point.
    With that being said, since you don't want the id of the object that is being checked, don't use instance_position, but instead use position_meeting.
    These little tips are also all in the manual ;)
     
  3. ajan-ko

    ajan-ko Member

    Joined:
    Aug 22, 2016
    Posts:
    263
    use
    so let's say you wan to check 32x32 grid

    Make sure your origin on the center
     
  4. DariusWolfe

    DariusWolfe Member

    Joined:
    Sep 23, 2016
    Posts:
    32
    instance_position is what I would use. It also returns the instance ID, but you can just say something like "if instance_position(x,y,obj) != noone ..."

    I just got back into GM since last using GM8, and I was making the same mistake (using instance_place)
     
  5. jr carey

    jr carey Member

    Joined:
    Sep 22, 2016
    Posts:
    172
    you could just do something like:

    create event:
    var xx = 0;

    step event:
    if(!place_empty(x,y))
    {
    xx = 1;
    }
    draw event:
    draw_self();
    draw_text(20,20,xx);


    that way it will just let you know if theres an object there or not, all you have to do is get the instance id, which should be pretty simple
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,986
    if instance_position(x,y,whatever)

    is the same thing functionally as

    if position_meeting(x,y,whatever)

    except the first one is more flexible, since you can change the if to a with easily enough if you wanted to later.

    instance_position() always returns a value of -4 or a value greater than 1000000. You will never get 0, 1, 2, 3, 4, ... etc, so instance_position() is functionally the same as position_meeting() when treated as a boolean.
     
  7. lumini

    lumini Guest

    I know I am late, like two months, but here is a simple code for the first question you asked. Hope it is clear. :potato: ;)

    Code:
    /// exists_at_Position(x,y,object);
    
    /*
     Check if there is at least one speicified object at the given coordinates.
    */
    
    var i,check, instNum;
    
    check=false;
    instNum = instance_number(argument2);
    
    for (i=0;i<instNum && check==false ; i++)
    {
        var inst = instance_find(argument2,i);
        
        if (inst.x==argument0 && inst.y==argument1)
            check=true;
    
    }
    
    return check;
    
     

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