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

Instance Position

Hi

I'm trying to detect when a player clicks but doesn't click on an object. Basically I want to detect them clicking on empty space and moving them there. This is the code I have so far:

Code:
if (mouse_check_button_pressed(mb_left))
{
    if (!instance_position(mouse_x, mouse_y, all))
    {
        show_debug_message("Clicked in the grass");
        //if (flight == true)
        //
 
Read the manual...instance_position does not return false if there is no object there, it returns noone (which resolves to -4 internally). !instance_position is asking if instance_position returns 0. Since you do not need the unique ID of whatever is at the mouse position, it's far better to use position_meeting instead, which DOES return false if there is nothing there.
 

Cameron

Member
!instance_position should still work but even better than that is if position_empty(mouse_x,mouse_y)
Code:
if mouse_check_button_pressed(mb_left) and position_empty(mouse_x,mouse_y){
    show_debug_message("Clicked in the grass");
    if flight{
        //do stuff
    }
}
 

Relic

Member
Read the manual...instance_position does not return false if there is no object there, it returns noone (which resolves to -4 internally). !instance_position is asking if instance_position returns 0. Since you do not need the unique ID of whatever is at the mouse position, it's far better to use position_meeting instead, which DOES return false if there is nothing there.
It may be a quirk of the friendly nature of programming in gml but any negative number and zero are all considered false.
 
Hahaha, yeah, forgot about that. Which means the code he posted originally should work fine, unless the specific constants such as noone are treated differently but I've got no idea. I use the actual info given in the manual.
 

Cameron

Member
Hahaha, yeah, forgot about that. Which means the code he posted originally should work fine, unless the specific constants such as noone are treated differently but I've got no idea. I use the actual info given in the manual.
slight correction,
any number equal or less than 0.5000... is considered false.
One more thing to note, the manual has a statement safeguarding the company if they decide to go to "real" boolean data types in the future.
A boolean is simply a value that can either be true or false. Note that currently GameMaker: Studio does not support "true" boolean values, and actually accepts any real number below 0.5 as a false value, and any real number equal to (or greater than) 0.5 as being true. This does not mean however that you should be checking 1 and 0 (or any other real number) for true and false, as you are also provided with the constants true and false which should always be used in your code to prevent any issues should real boolean data types be added in a future update.
So technically it is wise to get in the habit of using true or false macros instead of 0 and 1 or -4 or 0.5 etc., to determine if values are true or false as well as using functions that return boolean values where appropriate. Although, I don't think they will have actually change this, it makes sense to form the habit now to be as correct as possible.
 
Ok. I changed it to

Code:
var _x = argument0;
var _y = argument1;
var _inst = argument2;


if (mouse_check_button_pressed(mb_left))
{
    if (position_empty(mouse_x, mouse_y))
    {
        show_debug_message("Clicked in the grass");
        //if (flight == true)
        //{
            show_debug_message("flight is true");
The debug message still doesn't show up.
 

Cameron

Member
Ok. I changed it to

Code:
var _x = argument0;
var _y = argument1;
var _inst = argument2;


if (mouse_check_button_pressed(mb_left))
{
    if (position_empty(mouse_x, mouse_y))
    {
        show_debug_message("Clicked in the grass");
        //if (flight == true)
        //{
            show_debug_message("flight is true");
The debug message still doesn't show up.
Okay, do this:

Code:
var _x = argument0;
var _y = argument1;
var _inst = argument2;


if (mouse_check_button_pressed(mb_left)){
    if (position_empty(mouse_x, mouse_y)){
        show_debug_message("Clicked in the grass");
    }
    else{
        var inst = instance_position(mouse_x,mouse_y,all);
        if inst>=0{
            show_debug_message(object_get_name(inst.object_index));
        }
        else show_debug_message(inst);
    }
}
 
No go Cameron. Could it be because of my background? The grass background is an object added in the room editor. That's the only thing I can think of that makes this not work.
 

Cameron

Member
No go Cameron. Could it be because of my background? The grass background is an object added in the room editor. That's the only thing I can think of that makes this not work.
Yeah that's why I had you put the debug messages in there, to see what object was being found at that location. That makes sense if your grass is an object that has a collision mask. There are some things you can do, such as adding the grass as a background rather than an object, or drawing the grass from the draw event as a seprate sprite while setting sprite_index=noone;
That code I gave you should tell you what object is being found in the show_debug_message.
 
It shows up as a hidden layer I have for my lightning bat. Its just a big black block. But, GM won't let me delete it. It says I have to unlock it but when I try to unlock it it just gives me an error message saying I have ti unlock it/
 
Top