1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 [SOLVED] How to use position_meeting?

Discussion in 'Programming' started by Lobos, Dec 2, 2019.

  1. Lobos

    Lobos Member

    Joined:
    Feb 22, 2017
    Posts:
    27
    A super basic question: how to use position_meeting properly?

    My GUI contains tabs: if the player clicks on one, that is activated (and the others deactivated). I tought that would work:

    Code:
    if ((mouse_check_button(mb_left)) && position_meeting(mouse_x, mouse_y, obj_controller_gui_mine))
    {
    ...
    for each tabs - but apperently thats not working. My question: what is the problem?

    I tried some workarounds (like making a one-pixel obj_mouse and checking that, using collision_point, position_meeting), but none of them worked either (using numbers/letters works, but I would like to use the mouse too).
     
  2. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    222
    Well, since it's a GUI element, I'm guessing you're using draw_rectangles, etc do show it? place_meeting and collision_point, etc, are going to work off of the object's collision mask. If the object itself has no sprite and is just drawing some primitives stuff, it wont have a mask either (by default, the mask is the sprite). So you'll either have to add a sprite/mask or do some math involving the mouse location and object's expected width/height yourself.

    Also you probaly want mouse_check_button_pressed, though I don't think that is the issue, you'd notice some weirdness. And I'm assuming by position_meeting you mean place_meeting
     
    Last edited: Dec 2, 2019
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    No, he means position_meeting. But yeah,
    obj_controller_gui_mine needs a sprite_index assigned to it in order to work. Also if that's in the GUI event at all, you need to make sure the GUI layer is the same size and scale as everything else. mouse_x and mouse_y are coordinates in the room, not coordinates in the screen, so it's possible that the mouse roses room and the mouse relative to the screen are so vastly different that the mouse is not even registered as being over a button.

    But just a heads up, position_meeting is a boolean check - true or false. It will not tell you which button you clicked on. You can use position_meeting(x,y,self.id) inside each button. Otherwise use onstance_position, which does the same thing but returns an ID (or noone).
     
  4. Lobos

    Lobos Member

    Joined:
    Feb 22, 2017
    Posts:
    27
    I use a separate, empty GUI controller object with draw_self(); on the Draw GUI event and make it the parent of all HUD objects.

    Each GUI object has two sprites (active, not active), image_speed = 0; on the Create event and image_index = 0 or 1 in the step event in the appropriate places.

    By position_meeting I meant position_meeting.

    My GUI size is different to the zoom, but even if I set them to equal, it still not work.

    I know position_meeting not equal to instance_meeting, but this doesn’t matter for me, because I have simply 8 different GUI objects (maybe not elegant, but it is easy to adjust in the minimum viable product phase).
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Instead of mouse_x,mouse_y try using device_mouse_x_to_gui(0) and device_mouse_y_to_gui(0)

    Edit: Never mind. Let's just back things up here, because you have potentially multiple issues.

    First off, WHERE is this code located:
    Code:
    if ((mouse_check_button(mb_left)) && position_meeting(mouse_x, mouse_y, obj_controller_gui_mine))
    {
    ...
    ?
    Inside which object and inside which event?

    Is obj_controller_gui_mine an object which is a child of obj_controller_gui or whatever you named the parent?

    Is that code inside the parent or is it inside the child? If it's inside the child, use self.id instead of obj_controller_gui_mine. Maybe you used the wrong object_index in the other buttons.

    If that code is in the parent, then you pretty much already messed up, but more importantly make sure the child doesn't have the same event with different code. If the child has the same event as a parent, the child will not run the parent's event by default unless you call event_inherited() inside the child's event. Regardless, if the code is all in the parent, you should use self.id instead of obj_controller_gui_mine here and then have a switch statement to decide what to do next based on what the object_index of the child is; or better yet, put the child's code inside User Events and then just call the user event (e.g., event_user(0) to call User Event 0).

    As for the mouse being in the right spot, we can debug that by using this onMouseOver code:
    Code:
    image_index = position_meeting(mouse_x,mouse_y,self.id)
    Then just figure out how far off the mouse coordinates are from the button, if at all.
     
    Last edited: Dec 3, 2019
    Lobos likes this.
  6. Lobos

    Lobos Member

    Joined:
    Feb 22, 2017
    Posts:
    27
    Luckily the device_mouse_x_to_gui solved the problem.
    The problem is (aside the lack of programming knowledge), that I don't have much time to work on the game, so I totally forgot, that I already used the device_mouse_x_to _gui elsewhere.

    To answer the other questions: the "if ((mouse_check_button(mb_left)..." was in the object’s (obj_controller_gui_mine) Step event, the obj_controller_gui had only one line of code (draw_self() in the draw GUI event). Hopefully I used the parent-child relations correctly and none of the obj_controller_gui's child objects use the same (Draw GUI) event (the text and other drawings are handled by a separate object, that isn't a child of obj_controller_gui).
    I don’t plan to use the same HUD objects multiple times (so there is only one obj_controller_gui_mine, and one for each other obj_controller_gui_ objects, so thats why I used the object’s name, instead of ”id”.


    Regardless, thank you for taking the time to answer.
     

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