Position_meeting with instance?

Discussion in 'Programming' started by Schwee, Sep 14, 2019.

  1. Schwee

    Schwee Member

    Joined:
    Jul 6, 2016
    Posts:
    166
    Been at this long enough so I'm reaching out. I'm sure its stupid and obvious...

    Creating inventory slots:
    Code:
    for(var i=0; i<obj_ctrl.inventory_size; i++;) {
        slots[i] = instance_create_layer(curr_x, curr_y, "Inventory_slots", obj_inv_slot);
    Checking if slot is selected via mouse click:
    Code:
    for(var i=0; i < array_length_1d(slots); i++) {
        var slot = slots[i];
        if (
            mouse_check_button_pressed(mb_left) &&
            position_meeting(mouse_x, mouse_y, slot)
        ) {
    I've pretty much narrowed down the error down to the position_meeting check. GM documentation says position_meeting SHOULD accept this 'slot' but it does not seem to register. I have tried slot.id as well. Is there something else I am missing?
     
  2. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    111
    place meeting moves the object calling it and checks for a collision then moves back, so if a controller is using this without a sprite it will never be true, check for that.
     
  3. Schwee

    Schwee Member

    Joined:
    Jul 6, 2016
    Posts:
    166
    Well I am using position meeting but that does answer my question either way...I don't have any sprite set for the object I am looking at, and thus no collision mask. Thanks!
     
    ParodyKnaveBob likes this.
  4. nesrocks

    nesrocks Member

    Joined:
    Nov 6, 2016
    Posts:
    298
    Instead of position_meeting, try using point_in_rectangle. Because position_meeting will move the object running the code to the specified position and checking for collision with the object, but if you're on a controller object that doesn't make sense, especially if it has no sprite.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,800
    place_meeting() virtually moves instances. position_meeting() is just a boolean collision_point().
     
    ParodyKnaveBob and nesrocks like this.
  6. nesrocks

    nesrocks Member

    Joined:
    Nov 6, 2016
    Posts:
    298
    Oh yeah, you're right. I'm sleepy!

    I can't see the error, so to narrow down stuff, try putting this code in the draw event and see if it draws the circle at the top left corner when you mouse over the object that is supposed to be slot[0]:
    Code:
    if position_meeting(mouse_x, mouse_y, slot[0]) draw_circle(10,10,10,false);
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,470
    Each instance of obj_inv_slot must have a collision mask in order to be registered by position_meeting(), and that usually comes from its assigned sprite. Is that left unset? Remember: Things that are just drawn don't count for collisions.

    Also, for those of you who suggested place_meeting(), you need to read this article: What's the difference: Collision functions
     
  8. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,926
    I'm not entirely sure why'd you want inventory as a bunch of instances. I'd see it primarily as data with some draw loop to visualize. On a single-row inventory the click position would then be mouse x minus inventory area's left edge, then div with inventory slot pixel width. On a multi-row inventory, if your data is 2D grid you do the same with y, but if it is an array or list you multiply this y-position by number of slots per row and add to x-position.
     
    Schwee likes this.
  9. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    510
    Since you found the problem already, it'll be nice to hear how you solve it. Planning to give the object a mask? (Which, even if you super don't want to assign a sprite and need to draw something manually, you can always assign a transparent box just for collision purposes -- a little trick I do all the time, and handy because you can scale the box to any size you need. Also, instead of transparent, it can be some arbitrary marker that you just don't allow to auto-draw.) Some other plan?

    Regards, $:^ J
     
  10. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    For inventory/UI work, I almost always use point_in_* functions. Of course, I also don't use instances within my UI work, I'm always simply drawing it from a controller object, so there's that.
     
  11. Schwee

    Schwee Member

    Joined:
    Jul 6, 2016
    Posts:
    166
    @ParodyKnaveBob I solved it doing exactly that. I may end up changing this technique down the line but this will suffice for now so I can continue working with and testing other functionality. There's a few posts mentioning just drawing the inventory without an instance and I imagine that is the smarter way to go about it for scale.
     
    ParodyKnaveBob likes this.
  12. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    510
    It really does depend, re: smarter way. I've seen many a GM expert over the years say they used to get more and more into data-fying everything, removing object resources from their trees and handling everything through controllers, and then see various benefits to just using GM's own built-in stuff anyway and move back to using object instances for things. $:^ J After all, you clearly have not hardcoded your inventory, but rather soft-coded a slot object that can handle whatever instances of information (and existence) you wanna put there, thus hey, good job -- plus (within reason), "if it works, it works," right? $;^ b

    Just for example, in the past, I created a whole menu-constructing system which I was going to put on the Marketplace (until I batted major life curveballs) that handled all kinds of data in all kinds of ways -- but if I wanted, I could simply instance_create() some menu option into the room that allowed for easy GUI and pleasant UX. No shame in mixing and matching! (Like the difference between HTML and CSS, ha ha ha ha.)
     

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