problems with mouse collisions using instance_place(mouse_x, mouse_y, object )

Roobicorn

Member
For some reason, instance_place isn't working even when position_meeting is.

I'm trying to set up interaction where hovering the mouse over an object (obj_shell) in the game will move another object (obj_main_menu) to the x location of that specific instance of obj_shell (there are 3 on the screen), and change the sprite of obj_main_menu based on a variable of that obj_shell.

here is the code I've got:

GML:
mouse_hover = position_meeting(mouse_x, mouse_y, obj_shell);

//...

if (mouse_hover)
{
    shell_hover = instance_place(mouse_x, mouse_y, obj_shell);
    obj_main_menu.x = shell_hover.x;
    obj_main_menu.image_index = shell_hover.shell_pos; //default = 3 = blank image

}
and here is the error message:

Variable <unknown_object>.x(0, -2147483648) not set before reading it.
at gml_Object_obj_game_Step_0 (line 14) - obj_main_menu.x = shell_hover.x;
shell_hover is defined as noone in the create event, and obj_main_menu exists, it just won't recognise that shell_hover has an instance id even when mouse_hover is true.

I've tried a lot of variations to see if that helps - changed shell_hover to var _shell_hover; adding an if (instance_exists(shell_hover)) statement below the shell_hover line. nothing seems to fix it.
if I change that line to obj_main_menu.x = 30; the error just happens on the next line down.

Any ideas why instance_place() isn't passing the id across?
 

Roobicorn

Member
More info:

Here's a gif of the program in action with gui showing how the variables change (or don't!) as the mouse moves.
note that mouse_hover becomes 1 when the mouse moves over each of the objects, but shell_hover does not.

instance_place error gif3.gif

here is the relevant code:

GML:
mouse_hover = position_meeting(mouse_x, mouse_y, obj_shell);
mouse_click = mouse_hover && mouse_check_button_pressed(mb_left);


if (room == rm_start)
{
   
    if (mouse_hover == true)
    {
        shell_hover = instance_place(mouse_x, mouse_y, obj_shell);
        //obj_main_menu.x = shell_hover.x;
        //obj_main_menu.image_index = shell_hover.shell_pos; //default = 3 = blank image
                           
    }
}
 
Last edited:

Merrick82

Member
Hi Roobicorn,
Came across you question while searching for another answer, so I know you probably figured this out already, but I'll post the solution in case others have the same issue.

We simply need to use "instance_position()" instead of "instance_place()".
"instance_place()" is used for collision detection between two instances with masks. A mouse pointer doesn't have a mask, so we need to check for position collision instead.

good luck with you project!
 

Roobicorn

Member
Hi Roobicorn,
Came across you question while searching for another answer, so I know you probably figured this out already, but I'll post the solution in case others have the same issue.

We simply need to use "instance_position()" instead of "instance_place()".
"instance_place()" is used for collision detection between two instances with masks. A mouse pointer doesn't have a mask, so we need to check for position collision instead.

good luck with you project!

Hi Merrick82, Thanks for the advice! I never actually figured this one out, I ended up using a different method as a workaround, so it's nice to finally have an answer!

Unfortunately I haven't had much time for making games lately, but I did manage to publish this project, minimalistic as it was: Shell Game
 

Nidoking

Member
The problem appears to be that even though there was an instance at the indicated position, it didn't collide with the instance placed at that same position. If you're going to use a meeting function to avoid having to check the return result of the place function for noone, you need to use the same function for both. Either use position_meeting with instance_position, or use place_meeting with instance_place.
 

Roobicorn

Member
The problem appears to be that even though there was an instance at the indicated position, it didn't collide with the instance placed at that same position. If you're going to use a meeting function to avoid having to check the return result of the place function for noone, you need to use the same function for both. Either use position_meeting with instance_position, or use place_meeting with instance_place.
Ok, I guess I didn't understand the difference between 'place' and 'position'. Don't think I realised instance_position() was a thing.
 
Top