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

GML If obj.A is having collision with multiple instances of obj.B at same x&y location, single one out

Yavga

Member
Hello community,

Here's an issue I can't seem to figure out after searching for a while.
I gather that this issue could be solved with an instance function which calls for a unique id
but I can't wrap my head around it, here's the details:


In a physics world "obj.A" represents a hand, a hand which creates a "weld joint" on a specific button trigger being pressed with an "if place_meeting obj.B" statement.

Codesnippet from obj.B's (A tool) step event
Code:
if gamepad_button_check_pressed(global.player1,gp_stickr)
and place_meeting(x,y,obj_phy_front_hand)
and weld = false
    {
    weld = true
    self.phy_position_x = obj_phy_front_handhelper.phy_position_x
    self.phy_position_y = obj_phy_front_handhelper.phy_position_y
    self.x = obj_phy_front_handhelper.x
    self.y = obj_phy_front_handhelper.y
    frontweld = physics_joint_weld_create(self,obj_phy_front_handhelper,x,y,0,1,10,0)
    }
The issue I am having is that when there are 2 instances of obj.B in my map (on the same x and y location)
the event above triggers twice (which means my single hand holds 2 objects now)

to solve this I think I will have to do something with objects_place (because it returns an id)
but I cannot figure out the right syntax.


How can I make it so, that my hand only picks up one object?

Thanks!
 
You need to understand instances vs objects better. That code actually DOES function in a manner that makes it only activate on one instance, however, once activated, it then controls ALL instances (as you are using the object ID to set positions and stuff inside the if statement). The way to solve it is to (as you rightly surmised) get the instance ID of the object at the position. This is done using the instance_position function. Here's some code to show how it's done:

Code:
var inst = instance_position(x,y,obj_phy_front_hand);
if gamepad_button_check_pressed(global.player1,gp_stickr)
and instance_exists(inst)
and weld = false
   {
   weld = true
   self.phy_position_x = inst.phy_position_x
   self.phy_position_y = inst.phy_position_y
   self.x = inst.x
   self.y = inst.y
   frontweld = physics_joint_weld_create(self,inst,x,y,0,1,10,0)
   }
First, you grab the instance ID of the obj_phy_front_hand at the x and y position you want. This will return the special variable noone (or, in number terms, it returns -4) if there is no obj_phy_front_hand at that position. So then we do the normal if check you had, except we simply check to see if an instance_exists of whatever's stored in the inst variable returns true. If inst is storing noone, it will return false and the if will be cancelled, otherwise inst will have an instance ID of one of the obj_phy_front_hands and the if will continue. Then inside the if statement, we use inst.whatever for the Instance ID rather than the object index like obj_phy_front_hand.whatever and it will only apply to whatever instance has its ID stored in inst. Since you set weld from false to true once you've gotten a hand, the statement shouldn't run again so it won't grab multiple obj_phy_front_hand's.
 

Yavga

Member
This is really helpful, it makes me understand exactly the part where I misunderstood how instance_position works, I fidled around with it and decided the "noone" part I used earlier wasn't needed and thus I removed it but that thought was wrong now that I see what it does better. Thanks a lot! with your explanation I managed to make something that works the way I want!
 
Top