"instance_nearest" and "instance_id" confusion (warning: noob question)

L

lonecandle

Guest
Hi all, I´m just starting out with GM:S and I´m almost sure I misunderstood the real meaning of these two lines of code.

I´ll just shortly describe what I´m trying to achieve:

Let´s say I have a group of instances - buttons - with one parent - the parent button - distributed in the room. (For clarity; the parent button is also present in the room, together with the others, which are just smaller buttons with "the parent button" assigned as a parent).

I then have a "global tap" event in the parent button, and I want to check its distance of the point of the tap (posX, posY). Since there are more "buttons" in the room, all of which I want this to be applied to, I thought it would be ellegant to do this only in the parent button, to have the possibility of placement of however many "buttons" I choose and this check being applied to all of them.
The result I aim for is that whenever I tap on the screen, the button which is closest to the tap will be pressed.

How do I check for this? I came up with this most probably nonsensical code:

GML:
if instance_id = instance_nearest(event_data[? "posX"], event_data[? "posY"] , self)
//PRESS ME (not coded yet)//
My logic was: If instance_id (=if this particular instance (?) ) is the nearest instance to the tap, then "press me" (=this particular button). Here´s where I need advice - have I misunderstood the function of the instance_id variable? How can I point to the instance itself through its own action and then check whether it's the nearest one to the position of the tap?

I sincerely hope I was clear enough with this and I hope there still are friendly souls lurking around forums like this who will find the time to answer this question. Thank you very much in advance, looking forward to seeing any replies!!
LC
 
well...hmmm...
instance_id = id of the instance running the code
instance_nearest = nearest instance of the instance running the code.

So your code reads like: If I am the nearest person near me, do this stuff.

I don't know about you, but I speak 3 languages (actual, not programming), and this makes sense in none of them. :)

What are you trying to do, exactly?
 
L

lonecandle

Guest
What are you trying to do, exactly?
Ah thank you for the reply! I see, so should I then ditch the "instance_nearest" altogether? As I was trying to explain, I want to have a set of buttons (different objects with one parent) being checked for their distance to the position of a global tap, and then press the one button nearest to that position.

With what you said, it sounds like if I want to use instance_nearest, I have to create a different instance on the position of the tap whenever it happens, and then check the distance of the button(s) to that new instance. That sounds a little clumsy to me, so is there a way with which creating a separate instance as kind of a "target" for the distance check of the buttons is not necessary?

Ah my brain is hurting rn :D hope this is clear.
 

FrostyCat

Redemption Seeker
First of all, the current instance's ID is id, not instance_id.
The Manual entry on id said:
This read-only variable holds the unique identifying number for the instance.
The Manual entry on instance_id said:
This read only array holds all the ids of every active instance within the room.
NEVER call the instance ID instance_id again. Before you use any more language elements you have not seen before, look it up in the Manual first. Guessing from the name alone is a bad idea.

When you specify an object ID as a target in instance_nearest and other similar collision functions, it counts instances of that object and all its descendants. Therefore your code should start like this:
GML:
if (instance_nearest(event_data[? "poxX"], event_data[? "posY"], parent_button) == id)
 
L

lonecandle

Guest
Therefore your code should start like this:
GML:
if (instance_nearest(event_data[? "poxX"], event_data[? "posY"], parent_button) == id)
Ahh that´s it bby! I love this, and now I get it, thank you very much. I was honestly trying to get it from the manual but I´m still very noobish in precisely interpreting all the content & I wasn´t aware that the id variable even existed - I for some reason had instance_id stuck in my head as the only way to point on a particular instance. I will continue with the code and see how it works. Thanks for your time!!
 
First of all, the current instance's ID is id, not instance_id.
Totally, I guess I'm relying on autocomplete too much and not enough from memory😅


Ah thank you for the reply! I see, so should I then ditch the "instance_nearest" altogether? As I was trying to explain, I want to have a set of buttons (different objects with one parent) being checked for their distance to the position of a global tap, and then press the one button nearest to that position.
Nah, you're on the right track, it really depends on what the event_data was containing. Makes perfect sense in this case.
 
Top